login.vue 17 KB

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