login.vue 20 KB

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