login.vue 19 KB

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