login.vue 17 KB

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