login.vue 17 KB

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