login.vue 19 KB

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