login.vue 14 KB

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