login.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. isAct: undefined,
  232. };
  233. },
  234. onLoad(option) {
  235. if (option.isBack) {
  236. this.isBack = option.isBack;
  237. }
  238. this.isAct = option.isAct;
  239. if (uni.getStorageSync("newUser_sc")) {
  240. this.newUser_sc = uni.getStorageSync("newUser_sc");
  241. }
  242. if (uni.getStorageSync("shareActivityCode")) {
  243. this.shareActivityCode = uni.getStorageSync("shareActivityCode");
  244. }
  245. this.getDualAuth();
  246. },
  247. onShow() {
  248. // 获取code
  249. // #ifdef MP-WEIXIN
  250. this.getwxCode();
  251. // #endif
  252. },
  253. onReady() {
  254. this.$refs.uForm1.setRules(this.rules);
  255. this.$refs.uForm2.setRules(this.rules);
  256. },
  257. methods: {
  258. getwxCode() {
  259. uni.login({
  260. provider: "weixin",
  261. success: (loginRes) => {
  262. this.code = loginRes.code;
  263. console.log("获取的code:", this.code);
  264. },
  265. });
  266. },
  267. toRegister() {
  268. if (this.isBack) {
  269. uni.redirectTo({
  270. url:
  271. "/pages2/register/register?isBack=" +
  272. this.isBack +
  273. "&isAct=" +
  274. this.isAct,
  275. });
  276. } else {
  277. uni.navigateTo({
  278. url: "/pages2/register/register?isAct=" + this.isAct,
  279. });
  280. }
  281. },
  282. getDualAuth() {
  283. this.$api.getDualAuth().then((res) => {
  284. if (res.data.code == 200) {
  285. this.isDualAuth = res.data.data * 1;
  286. if (this.isDualAuth === 1) {
  287. this.rules.account = [
  288. {
  289. required: true,
  290. message: "请输入手机号",
  291. trigger: ["change"],
  292. },
  293. {
  294. validator: (rule, value, callback) => {
  295. return this.$u.test.mobile(value);
  296. },
  297. message: "手机号码格式不正确",
  298. trigger: ["change"],
  299. },
  300. ];
  301. }
  302. }
  303. });
  304. },
  305. wxLogin(e) {
  306. this.$api
  307. .wxLogin({
  308. code: this.code,
  309. encryptedData: e.detail.encryptedData,
  310. iv: e.detail.iv,
  311. shareCode: this.newUser_sc,
  312. shareActivityCode: this.shareActivityCode,
  313. })
  314. .then((res) => {
  315. if (res.data.code == 200) {
  316. this.loginCallback(res, "wxlogin");
  317. } else {
  318. if (res.data.code == 666) {
  319. this.$u.toast(res.data.msg);
  320. setTimeout(() => {
  321. this.toRegister();
  322. }, 500);
  323. return;
  324. }
  325. this.getwxCode(); // code用完一次就会过期,防止用户点击取消后再点微信登录后code过期
  326. // this.$u.toast(res.data.msg);
  327. }
  328. });
  329. },
  330. canLogin() {
  331. if (this.current == 0) {
  332. if (this.form.account && this.form.pwd) {
  333. return true;
  334. }
  335. return false;
  336. } else if (this.current == 1) {
  337. if (this.form.tel && this.form.code) {
  338. return true;
  339. }
  340. return false;
  341. }
  342. },
  343. sms_login() {
  344. this.$refs.uForm2.validate((valid) => {
  345. if (valid) {
  346. let that = this;
  347. if (!this.form.tel) {
  348. this.$u.toast("请输入手机号码");
  349. return;
  350. }
  351. if (!this.form.code) {
  352. this.$u.toast("请输入验证码");
  353. return;
  354. }
  355. that.isUse = true;
  356. let datas = {
  357. shareActivityCode: this.shareActivityCode,
  358. tel: this.form.tel,
  359. code: this.form.code,
  360. };
  361. that.$api.smsLogin(datas).then(
  362. (res) => {
  363. that.isUse = false;
  364. if (res.data.code == 200) {
  365. this.loginCallback(res, "smslogin");
  366. } else {
  367. that.$u.toast(res.data.msg);
  368. }
  369. },
  370. (err) => {
  371. that.isUse = false;
  372. }
  373. );
  374. }
  375. });
  376. },
  377. fakeLogin() {
  378. uni.setStorageSync("user_account", "123");
  379. uni.setStorageSync("token", "123");
  380. uni.switchTab({
  381. url: "/pages/index/index",
  382. });
  383. },
  384. /**
  385. *
  386. * @param {*} data
  387. * @returns
  388. * 加密
  389. */
  390. encryptor(data) {
  391. const encrypt = new JSEncrypt();
  392. encrypt.setPublicKey(PUBLIC_KEY);
  393. return encrypt.encrypt(data);
  394. },
  395. pwlogin() {
  396. let that = this;
  397. if (!this.form.account) {
  398. this.$u.toast("请输入手机号码/身份证号");
  399. return;
  400. }
  401. if (!this.form.pwd) {
  402. this.$u.toast("请输入密码");
  403. return;
  404. }
  405. if (this.isDualAuth) {
  406. if (!this.form.code) {
  407. this.$u.toast("请输入验证码");
  408. return;
  409. }
  410. this.form.tel = this.form.account;
  411. }
  412. //虚拟登录
  413. /* that.fakeLogin()
  414. return */
  415. that.isUse = true;
  416. let form = JSON.parse(JSON.stringify(this.form));
  417. form.pwd = this.encryptor(form.pwd);
  418. form["shareActivityCode"] = this.shareActivityCode;
  419. // 账号登录用户 /app/common/account_login
  420. that.$api.accountLogin(form).then(
  421. (res) => {
  422. that.isUse = false;
  423. if (res.data.code == 200) {
  424. this.loginCallback(res, "pwlogin");
  425. } else {
  426. that.$u.toast(res.data.msg);
  427. }
  428. },
  429. (err) => {
  430. that.isUse = false;
  431. }
  432. );
  433. },
  434. codeChange(text) {
  435. this.codeTips = text;
  436. },
  437. // 获取验证码
  438. getCode(key) {
  439. let that = this;
  440. if (that.$refs.uCode.canGetCode) {
  441. if (that.$refs[key].validateState == "success") {
  442. let datas = { tel: this.form[key] };
  443. that.$api.loginSms(datas).then(
  444. (res) => {
  445. if (res.data.code == 200) {
  446. that.$u.toast("验证码已发送");
  447. // 通知验证码组件内部开始倒计时
  448. that.$refs.uCode.start();
  449. } else {
  450. that.$u.toast(res.data.msg);
  451. }
  452. },
  453. (err) => {
  454. console.log(err);
  455. }
  456. );
  457. } else {
  458. this.$refs[key].onFieldChange();
  459. }
  460. }
  461. },
  462. sectionChange(index) {
  463. this.current = index;
  464. },
  465. getPhoneNumber(e) {
  466. let that = this;
  467. uni.checkSession({
  468. success() {
  469. //session_key 未过期,并且在本生命周期一直有效
  470. that.putInfo(e);
  471. },
  472. fail() {
  473. // session_key 已经失效,需要重新执行登录流程
  474. uni.login({
  475. provider: "weixin",
  476. success: function (loginRes) {
  477. that.code = loginRes.code;
  478. that.putInfo(e);
  479. },
  480. });
  481. },
  482. });
  483. },
  484. putInfo(e) {
  485. let that = this;
  486. if (e.detail.encryptedData) {
  487. let inviteCode = uni.getStorageSync("inviteCode");
  488. //用户同意授权
  489. var datas = {
  490. iv: e.detail.iv,
  491. encryptedData: e.detail.encryptedData,
  492. code: that.code,
  493. };
  494. if (inviteCode) {
  495. datas.inviteCode = inviteCode;
  496. }
  497. that.$api.login(datas).then(
  498. (res) => {
  499. if (res.data.code == 200) {
  500. uni.setStorageSync("union_id", res.data.data.union_id);
  501. uni.setStorageSync("token", res.data.data.token);
  502. that.$api.getInfo({ fromPlat: 1 }).then((resdata) => {
  503. if (resdata.data.code == 200) {
  504. uni.navigateBack();
  505. that.$store.state.userInfo = resdata.data.data;
  506. }
  507. });
  508. } else {
  509. uni.showModal({
  510. title: "提示",
  511. content: res.data.msg,
  512. showCancel: false,
  513. });
  514. }
  515. },
  516. (err) => {
  517. console.log(err);
  518. }
  519. );
  520. }
  521. },
  522. // 微信、密码、验证码 登录成功的回调
  523. loginCallback(res, types) {
  524. // if(res.data.data && res.data.data.full_info){
  525. uni.removeStorageSync("newUser_sc");
  526. uni.removeStorageSync("shareActivityCode");
  527. //信息完善,直接进入页面
  528. uni.setStorageSync("user_account", res.data.data.user_account);
  529. uni.setStorageSync("token", res.data.data.token);
  530. // 修改邀请码
  531. let sac = this.$store.getters.sac;
  532. sac &&
  533. this.$api.shareActivityCode({ shareActivityCode: sac }).then((res) => {
  534. // if (res.data.code == 200) {
  535. // uni.removeStorageSync("sac");
  536. // }
  537. });
  538. // /app/user/getInfo 登录用户信息// fromPlat来源平台 1小程序 2PC网站
  539. this.$api.getInfo({ fromPlat: 1 }).then((resdata) => {
  540. if (resdata.data.code == 200) {
  541. this.$store.state.userInfo = resdata.data.data;
  542. if (!this.isBack) {
  543. let goPath = "";
  544. if (types == "wxlogin" || types == "smslogin") {
  545. //密码登录不用判断电脑goPath, 直接到首页
  546. goPath = uni.getStorageSync("goPath");
  547. }
  548. if (goPath == "course") {
  549. uni.redirectTo({
  550. url: "/pages2/wd/class",
  551. });
  552. types == "wxlogin" &&
  553. this.$method.setUuid(new Date().valueOf() + "");
  554. } else if (goPath == "bank") {
  555. uni.redirectTo({
  556. url: "/pages2/wd/question_bank",
  557. });
  558. types == "wxlogin" &&
  559. this.$method.setUuid(new Date().valueOf() + "");
  560. } else {
  561. console.log("登录后跳转");
  562. uni.removeStorageSync("h5_code");
  563. uni.reLaunch({
  564. url: "/pages/index/index",
  565. });
  566. types == "wxlogin" &&
  567. this.$method.setUuid(new Date().valueOf() + "");
  568. }
  569. } else {
  570. uni.navigateBack();
  571. }
  572. }
  573. });
  574. // } else {
  575. // //未完善信息,存为临时信息
  576. // uni.setStorageSync('user_account_temp', res.data.data.user_account);
  577. // uni.setStorageSync('token_temp', res.data.data.token);
  578. // this.$navTo.togo('/pages2/register/bind');
  579. // }
  580. },
  581. // scanLoginCheck小程序校验PC登录二维码,执行登录获取到令牌,然后把扫码的路径最后面的6位标识码提交给后台就行
  582. // submitCode() {
  583. // console.log('跳转请求提交code到接口', this.scanCode)
  584. // this.$api.scanLoginCheck({
  585. // scanCode: this.scanCode
  586. // }).then((res) => {
  587. // if (res.data.code == 200) {
  588. // uni.navigateTo({
  589. // url: '/pages4/login/pcLoginSuccess'
  590. // })
  591. // } else {
  592. // this.$u.toast(res.data.msg)
  593. // }
  594. // })
  595. // }
  596. },
  597. };
  598. </script>
  599. <style scoped lang="scss">
  600. .wxBtn {
  601. position: fixed;
  602. bottom: 10%;
  603. width: 100%;
  604. left: 0;
  605. }
  606. /deep/ .wxBtn button::after {
  607. border: none;
  608. }
  609. .loginBtn {
  610. width: 526rpx;
  611. height: 80rpx;
  612. background: linear-gradient(90deg, #015eea, #00c0fa);
  613. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  614. opacity: 0.6;
  615. border-radius: 40rpx;
  616. color: #ffffff;
  617. text-align: center;
  618. line-height: 80rpx;
  619. margin: 40rpx auto;
  620. &.able {
  621. opacity: 1;
  622. }
  623. }
  624. .wxloginBtn {
  625. margin: 0 auto;
  626. background: url("/static/loginBtn.png") no-repeat;
  627. background-size: 100% 100%;
  628. border: none;
  629. width: 100rpx;
  630. height: 100rpx;
  631. }
  632. /deep/page {
  633. background-color: #ffffff;
  634. height: 100%;
  635. width: 100%;
  636. }
  637. .login_box {
  638. width: 100%;
  639. height: 360rpx;
  640. background: #ffffff;
  641. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  642. border-radius: 24rpx;
  643. margin-top: 30rpx;
  644. padding: 40rpx 35rpx;
  645. .password {
  646. /deep/.uicon-eye-fill {
  647. &::before {
  648. color: #007aff;
  649. content: "\e613";
  650. }
  651. }
  652. }
  653. }
  654. /deep/ .u-item-bg {
  655. border-radius: 32px !important;
  656. }
  657. /deep/ .u-subsection {
  658. border-radius: 32px !important;
  659. }
  660. .full_img {
  661. position: absolute;
  662. left: 0;
  663. display: block;
  664. width: 100%;
  665. z-index: -999;
  666. top: 0;
  667. }
  668. .head {
  669. height: 96rpx;
  670. width: 100%;
  671. line-height: 96rpx;
  672. margin-top: 40rpx;
  673. text-align: center;
  674. display: flex;
  675. position: relative;
  676. justify-content: center;
  677. }
  678. .icon {
  679. position: absolute;
  680. left: 30rpx;
  681. }
  682. </style>