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