login.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <template>
  2. <view style="height: 100%">
  3. <image
  4. mode="widthFix"
  5. src="/pages4/static/login_bg.jpg"
  6. class="full_img"
  7. ></image>
  8. <!-- #ifdef MP-WEIXIN -->
  9. <u-navbar
  10. title="登录"
  11. :border-bottom="false"
  12. background="{ background: '#ffffff',opacity:0.4; }"
  13. title-color="#ffffff"
  14. back-icon-color="#ffffff"
  15. ></u-navbar>
  16. <!-- #endif -->
  17. <view style="padding: 30rpx">
  18. <view style="width: 400rpx">
  19. <u-subsection
  20. @change="sectionChange"
  21. :list="list"
  22. :current="current"
  23. active-color="#007AFF"
  24. inactive-color="#ffffff"
  25. bg-color="rgba(255,255,255,0.52)"
  26. ></u-subsection>
  27. </view>
  28. <view class="login_box">
  29. <u-form :model="form" ref="uForm1" v-show="current == 0">
  30. <u-form-item prop="account" v-if="!isDualAuth"
  31. ><u-input
  32. type="idcard"
  33. v-model="form.account"
  34. placeholder-style="color:#999999"
  35. placeholder="手机号/学员身份证"
  36. /></u-form-item>
  37. <u-form-item prop="account" ref="account" v-else
  38. ><u-input
  39. type="number"
  40. maxlength="11"
  41. placeholder-style="color:#999999"
  42. v-model="form.account"
  43. placeholder="手机号"
  44. /></u-form-item>
  45. <u-form-item prop="pwd"
  46. ><u-input
  47. class="password"
  48. v-model="form.pwd"
  49. placeholder-style="color:#999999"
  50. type="password"
  51. placeholder="登录密码"
  52. /></u-form-item>
  53. <u-form-item prop="code" v-if="isDualAuth">
  54. <u-input
  55. v-model="form.code"
  56. type="number"
  57. placeholder-style="color:#999999"
  58. placeholder="验证码"
  59. />
  60. <u-button slot="right" size="mini" @click="getCode('account')">{{
  61. codeTips
  62. }}</u-button>
  63. </u-form-item>
  64. </u-form>
  65. <u-form :model="form" ref="uForm2" v-show="current == 1">
  66. <u-form-item prop="tel" ref="tel"
  67. ><u-input
  68. type="number"
  69. maxlength="11"
  70. placeholder-style="color:#999999"
  71. v-model="form.tel"
  72. placeholder="手机号"
  73. /></u-form-item>
  74. <u-form-item prop="code">
  75. <u-input
  76. v-model="form.code"
  77. type="number"
  78. placeholder-style="color:#999999"
  79. placeholder="验证码"
  80. />
  81. <u-button slot="right" size="mini" @click="getCode('tel')">{{
  82. codeTips
  83. }}</u-button>
  84. </u-form-item>
  85. </u-form>
  86. </view>
  87. <button
  88. class="loginBtn"
  89. :class="{ able: canLogin() }"
  90. @click="pwlogin"
  91. v-if="current == 0"
  92. >
  93. 登录
  94. </button>
  95. <button
  96. :disabled="isUse"
  97. :class="{ able: canLogin() }"
  98. class="loginBtn"
  99. @click="sms_login"
  100. v-if="current == 1"
  101. >
  102. 登录
  103. </button>
  104. <view style="width: 100%; margin: 40rpx auto">
  105. <view
  106. style="
  107. display: flex;
  108. align-items: center;
  109. color: #007aff;
  110. font-size: 24rpx;
  111. justify-content: center;
  112. "
  113. >
  114. <!-- <navigator hover-class="none" :url="'/pages2/register/register?scanCode=' + scanCode" style="margin: 0 40rpx;">立即注册</navigator> -->
  115. <view style="margin: 0 40rpx" @click="toRegister">立即注册</view>
  116. <view
  117. style="width: 3rpx; height: 20rpx; background-color: #007aff"
  118. ></view>
  119. <navigator
  120. hover-class="none"
  121. url="/pages2/register/forget"
  122. style="margin: 0 40rpx"
  123. >忘记密码</navigator
  124. >
  125. </view>
  126. </view>
  127. <!-- #ifdef MP-WEIXIN -->
  128. <view class="wxBtn">
  129. <button
  130. type="default"
  131. open-type="getPhoneNumber"
  132. @getphonenumber="wxLogin"
  133. class="wxloginBtn"
  134. ></button>
  135. <view
  136. style="
  137. text-align: center;
  138. color: #999999;
  139. font-size: 24rpx;
  140. margin-top: 10rpx;
  141. "
  142. >微信快捷登录</view
  143. >
  144. </view>
  145. <!-- #endif -->
  146. </view>
  147. <u-verification-code
  148. seconds="60"
  149. ref="uCode"
  150. @change="codeChange"
  151. ></u-verification-code>
  152. </view>
  153. </template>
  154. <script>
  155. const PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----
  156. MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4qkbwIKErstK1sFESPEhOShpR
  157. pj4+sOVpJHxl5r/2xLBfA/MrXcAEra5Ro9cXNQSqmLLt8wecoLk/glfa5IdhXV0h
  158. RVQplIVs5z3MxcUa9ptKPHUTgh8xMCBvl8sUJKwkmn4vYWeDfHT22EL7Hr1pTMwU
  159. hF6WiNlWfQTVoF1rhwIDAQAB
  160. -----END PUBLIC KEY-----`;
  161. import JSEncrypt from "@/pages4/static/jsencrypt.min.js";
  162. export default {
  163. data() {
  164. return {
  165. code: "",
  166. form: {
  167. tel: "",
  168. code: "",
  169. account: "",
  170. pwd: "",
  171. },
  172. list: [
  173. {
  174. name: "密码登录",
  175. },
  176. {
  177. name: "短信登录",
  178. },
  179. ],
  180. rules: {
  181. tel: [
  182. {
  183. required: true,
  184. message: "请输入手机号",
  185. // 可以单个或者同时写两个触发验证方式
  186. trigger: ["change"],
  187. },
  188. {
  189. validator: (rule, value, callback) => {
  190. // 上面有说,返回true表示校验通过,返回false表示不通过
  191. // this.$u.test.mobile()就是返回true或者false的
  192. return this.$u.test.mobile(value);
  193. },
  194. message: "手机号码格式不正确",
  195. // 触发器可以同时用blur和change
  196. trigger: ["change"],
  197. },
  198. ],
  199. account: [
  200. {
  201. required: true,
  202. message: "请输入手机号/学员身份证",
  203. trigger: ["change"],
  204. },
  205. ],
  206. pwd: [
  207. {
  208. required: true,
  209. message: "请输入密码",
  210. // 可以单个或者同时写两个触发验证方式
  211. trigger: ["change"],
  212. },
  213. ],
  214. code: [
  215. {
  216. required: true,
  217. message: "请输入验证码",
  218. // 可以单个或者同时写两个触发验证方式
  219. trigger: ["change"],
  220. },
  221. ],
  222. },
  223. current: 1,
  224. codeTips: "",
  225. isUse: false,
  226. scanCode: "", // pc端扫码登录获取的标志码
  227. newUser_sc: "", // 用户扫码进来详情页获取的sc
  228. shareActivityCode: "", // 用户扫码进来详情页获取的
  229. isBack: false,
  230. isDualAuth: false,
  231. };
  232. },
  233. onLoad(option) {
  234. if (option.isBack) {
  235. this.isBack = option.isBack;
  236. }
  237. if (uni.getStorageSync("newUser_sc")) {
  238. this.newUser_sc = uni.getStorageSync("newUser_sc");
  239. }
  240. if (uni.getStorageSync("shareActivityCode")) {
  241. this.shareActivityCode = uni.getStorageSync("shareActivityCode");
  242. }
  243. this.getDualAuth();
  244. },
  245. onShow() {
  246. // 获取code
  247. // #ifdef MP-WEIXIN
  248. this.getwxCode();
  249. // #endif
  250. },
  251. onReady() {
  252. this.$refs.uForm1.setRules(this.rules);
  253. this.$refs.uForm2.setRules(this.rules);
  254. },
  255. methods: {
  256. getwxCode() {
  257. uni.login({
  258. provider: "weixin",
  259. success: (loginRes) => {
  260. this.code = loginRes.code;
  261. console.log("获取的code:", this.code);
  262. },
  263. });
  264. },
  265. toRegister() {
  266. if (this.isBack) {
  267. uni.redirectTo({
  268. url: "/pages2/register/register?isBack=" + this.isBack,
  269. });
  270. } else {
  271. uni.navigateTo({
  272. url: "/pages2/register/register?isBack=" + false,
  273. });
  274. }
  275. },
  276. getDualAuth() {
  277. this.$api.getDualAuth().then((res) => {
  278. if (res.data.code == 200) {
  279. this.isDualAuth = res.data.data * 1;
  280. if (this.isDualAuth === 1) {
  281. this.rules.account = [
  282. {
  283. required: true,
  284. message: "请输入手机号",
  285. trigger: ["change"],
  286. },
  287. {
  288. validator: (rule, value, callback) => {
  289. return this.$u.test.mobile(value);
  290. },
  291. message: "手机号码格式不正确",
  292. trigger: ["change"],
  293. },
  294. ];
  295. }
  296. }
  297. });
  298. },
  299. wxLogin(e) {
  300. this.$api
  301. .wxLogin({
  302. code: this.code,
  303. encryptedData: e.detail.encryptedData,
  304. iv: e.detail.iv,
  305. shareCode: this.newUser_sc,
  306. shareActivityCode: this.shareActivityCode,
  307. })
  308. .then((res) => {
  309. if (res.data.code == 200) {
  310. this.loginCallback(res, "wxlogin");
  311. } else {
  312. this.getwxCode(); // code用完一次就会过期,防止用户点击取消后再点微信登录后code过期
  313. // this.$u.toast(res.data.msg);
  314. }
  315. });
  316. },
  317. canLogin() {
  318. if (this.current == 0) {
  319. if (this.form.account && this.form.pwd) {
  320. return true;
  321. }
  322. return false;
  323. } else if (this.current == 1) {
  324. if (this.form.tel && this.form.code) {
  325. return true;
  326. }
  327. return false;
  328. }
  329. },
  330. sms_login() {
  331. this.$refs.uForm2.validate((valid) => {
  332. if (valid) {
  333. let that = this;
  334. if (!this.form.tel) {
  335. this.$u.toast("请输入手机号码");
  336. return;
  337. }
  338. if (!this.form.code) {
  339. this.$u.toast("请输入验证码");
  340. return;
  341. }
  342. that.isUse = true;
  343. let datas = {
  344. shareActivityCode: this.shareActivityCode,
  345. tel: this.form.tel,
  346. code: this.form.code,
  347. };
  348. that.$api.smsLogin(datas).then(
  349. (res) => {
  350. that.isUse = false;
  351. if (res.data.code == 200) {
  352. this.loginCallback(res, "smslogin");
  353. } else {
  354. that.$u.toast(res.data.msg);
  355. }
  356. },
  357. (err) => {
  358. that.isUse = false;
  359. }
  360. );
  361. }
  362. });
  363. },
  364. fakeLogin() {
  365. uni.setStorageSync("user_account", "123");
  366. uni.setStorageSync("token", "123");
  367. uni.switchTab({
  368. url: "/pages/index/index",
  369. });
  370. },
  371. /**
  372. *
  373. * @param {*} data
  374. * @returns
  375. * 加密
  376. */
  377. encryptor(data) {
  378. const encrypt = new JSEncrypt();
  379. encrypt.setPublicKey(PUBLIC_KEY);
  380. return encrypt.encrypt(data);
  381. },
  382. pwlogin() {
  383. let that = this;
  384. if (!this.form.account) {
  385. this.$u.toast("请输入手机号码/身份证号");
  386. return;
  387. }
  388. if (!this.form.pwd) {
  389. this.$u.toast("请输入密码");
  390. return;
  391. }
  392. if (this.isDualAuth) {
  393. if (!this.form.code) {
  394. this.$u.toast("请输入验证码");
  395. return;
  396. }
  397. this.form.tel = this.form.account;
  398. }
  399. //虚拟登录
  400. /* that.fakeLogin()
  401. return */
  402. that.isUse = true;
  403. let form = JSON.parse(JSON.stringify(this.form));
  404. form.pwd = this.encryptor(form.pwd);
  405. form["shareActivityCode"] = this.shareActivityCode;
  406. // 账号登录用户 /app/common/account_login
  407. that.$api.accountLogin(form).then(
  408. (res) => {
  409. that.isUse = false;
  410. if (res.data.code == 200) {
  411. this.loginCallback(res, "pwlogin");
  412. } else {
  413. that.$u.toast(res.data.msg);
  414. }
  415. },
  416. (err) => {
  417. that.isUse = false;
  418. }
  419. );
  420. },
  421. codeChange(text) {
  422. this.codeTips = text;
  423. },
  424. // 获取验证码
  425. getCode(key) {
  426. let that = this;
  427. if (that.$refs.uCode.canGetCode) {
  428. if (that.$refs[key].validateState == "success") {
  429. let datas = { tel: this.form[key] };
  430. that.$api.loginSms(datas).then(
  431. (res) => {
  432. if (res.data.code == 200) {
  433. that.$u.toast("验证码已发送");
  434. // 通知验证码组件内部开始倒计时
  435. that.$refs.uCode.start();
  436. } else {
  437. that.$u.toast(res.data.msg);
  438. }
  439. },
  440. (err) => {
  441. console.log(err);
  442. }
  443. );
  444. } else {
  445. this.$refs[key].onFieldChange();
  446. }
  447. }
  448. },
  449. sectionChange(index) {
  450. this.current = index;
  451. },
  452. getPhoneNumber(e) {
  453. let that = this;
  454. uni.checkSession({
  455. success() {
  456. //session_key 未过期,并且在本生命周期一直有效
  457. that.putInfo(e);
  458. },
  459. fail() {
  460. // session_key 已经失效,需要重新执行登录流程
  461. uni.login({
  462. provider: "weixin",
  463. success: function (loginRes) {
  464. that.code = loginRes.code;
  465. that.putInfo(e);
  466. },
  467. });
  468. },
  469. });
  470. },
  471. putInfo(e) {
  472. let that = this;
  473. if (e.detail.encryptedData) {
  474. let inviteCode = uni.getStorageSync("inviteCode");
  475. //用户同意授权
  476. var datas = {
  477. iv: e.detail.iv,
  478. encryptedData: e.detail.encryptedData,
  479. code: that.code,
  480. };
  481. if (inviteCode) {
  482. datas.inviteCode = inviteCode;
  483. }
  484. that.$api.login(datas).then(
  485. (res) => {
  486. if (res.data.code == 200) {
  487. uni.setStorageSync("union_id", res.data.data.union_id);
  488. uni.setStorageSync("token", res.data.data.token);
  489. that.$api.getInfo({ fromPlat: 1 }).then((resdata) => {
  490. if (resdata.data.code == 200) {
  491. uni.navigateBack();
  492. that.$store.state.userInfo = resdata.data.data;
  493. }
  494. });
  495. } else {
  496. uni.showModal({
  497. title: "提示",
  498. content: res.data.msg,
  499. showCancel: false,
  500. });
  501. }
  502. },
  503. (err) => {
  504. console.log(err);
  505. }
  506. );
  507. }
  508. },
  509. // 微信、密码、验证码 登录成功的回调
  510. loginCallback(res, types) {
  511. console.log("登录页面:", this.isBack);
  512. // if(res.data.data && res.data.data.full_info){
  513. uni.removeStorageSync("newUser_sc");
  514. uni.removeStorageSync("shareActivityCode");
  515. //信息完善,直接进入页面
  516. uni.setStorageSync("user_account", res.data.data.user_account);
  517. uni.setStorageSync("token", res.data.data.token);
  518. // 修改邀请码
  519. let sac = this.$store.getters.sac;
  520. sac &&
  521. this.$api.shareActivityCode({ shareActivityCode: sac }).then((res) => {
  522. // if (res.data.code == 200) {
  523. // uni.removeStorageSync("sac");
  524. // }
  525. });
  526. // /app/user/getInfo 登录用户信息// fromPlat来源平台 1小程序 2PC网站
  527. this.$api.getInfo({ fromPlat: 1 }).then((resdata) => {
  528. if (resdata.data.code == 200) {
  529. this.$store.state.userInfo = resdata.data.data;
  530. if (!this.isBack) {
  531. let goPath = "";
  532. if (types == "wxlogin" || types == "smslogin") {
  533. //密码登录不用判断电脑goPath, 直接到首页
  534. goPath = uni.getStorageSync("goPath");
  535. }
  536. if (goPath == "course") {
  537. uni.redirectTo({
  538. url: "/pages2/wd/class",
  539. });
  540. types == "wxlogin" &&
  541. this.$method.setUuid(new Date().valueOf() + "");
  542. } else if (goPath == "bank") {
  543. uni.redirectTo({
  544. url: "/pages2/wd/question_bank",
  545. });
  546. types == "wxlogin" &&
  547. this.$method.setUuid(new Date().valueOf() + "");
  548. } else {
  549. console.log("登录后跳转");
  550. uni.removeStorageSync("h5_code");
  551. uni.reLaunch({
  552. url: "/pages/index/index",
  553. });
  554. types == "wxlogin" &&
  555. this.$method.setUuid(new Date().valueOf() + "");
  556. }
  557. } else {
  558. uni.navigateBack();
  559. }
  560. }
  561. });
  562. // } else {
  563. // //未完善信息,存为临时信息
  564. // uni.setStorageSync('user_account_temp', res.data.data.user_account);
  565. // uni.setStorageSync('token_temp', res.data.data.token);
  566. // this.$navTo.togo('/pages2/register/bind');
  567. // }
  568. },
  569. // scanLoginCheck小程序校验PC登录二维码,执行登录获取到令牌,然后把扫码的路径最后面的6位标识码提交给后台就行
  570. // submitCode() {
  571. // console.log('跳转请求提交code到接口', this.scanCode)
  572. // this.$api.scanLoginCheck({
  573. // scanCode: this.scanCode
  574. // }).then((res) => {
  575. // if (res.data.code == 200) {
  576. // uni.navigateTo({
  577. // url: '/pages4/login/pcLoginSuccess'
  578. // })
  579. // } else {
  580. // this.$u.toast(res.data.msg)
  581. // }
  582. // })
  583. // }
  584. },
  585. };
  586. </script>
  587. <style scoped lang="scss">
  588. .wxBtn {
  589. position: fixed;
  590. bottom: 10%;
  591. width: 100%;
  592. left: 0;
  593. }
  594. /deep/ .wxBtn button::after {
  595. border: none;
  596. }
  597. .loginBtn {
  598. width: 526rpx;
  599. height: 80rpx;
  600. background: linear-gradient(90deg, #015eea, #00c0fa);
  601. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  602. opacity: 0.6;
  603. border-radius: 40rpx;
  604. color: #ffffff;
  605. text-align: center;
  606. line-height: 80rpx;
  607. margin: 40rpx auto;
  608. &.able {
  609. opacity: 1;
  610. }
  611. }
  612. .wxloginBtn {
  613. margin: 0 auto;
  614. background: url("/static/loginBtn.png") no-repeat;
  615. background-size: 100% 100%;
  616. border: none;
  617. width: 100rpx;
  618. height: 100rpx;
  619. }
  620. /deep/page {
  621. background-color: #ffffff;
  622. height: 100%;
  623. width: 100%;
  624. }
  625. .login_box {
  626. width: 100%;
  627. height: 360rpx;
  628. background: #ffffff;
  629. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  630. border-radius: 24rpx;
  631. margin-top: 30rpx;
  632. padding: 40rpx 35rpx;
  633. .password {
  634. /deep/.uicon-eye-fill {
  635. &::before {
  636. color: #007aff;
  637. content: "\e613";
  638. }
  639. }
  640. }
  641. }
  642. /deep/ .u-item-bg {
  643. border-radius: 32px !important;
  644. }
  645. /deep/ .u-subsection {
  646. border-radius: 32px !important;
  647. }
  648. .full_img {
  649. position: absolute;
  650. left: 0;
  651. display: block;
  652. width: 100%;
  653. z-index: -999;
  654. top: 0;
  655. }
  656. .head {
  657. height: 96rpx;
  658. width: 100%;
  659. line-height: 96rpx;
  660. margin-top: 40rpx;
  661. text-align: center;
  662. display: flex;
  663. position: relative;
  664. justify-content: center;
  665. }
  666. .icon {
  667. position: absolute;
  668. left: 30rpx;
  669. }
  670. </style>