login.vue 17 KB

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