login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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: 1,
  114. codeTips: '',
  115. isUse:false,
  116. scanCode: '', // pc端扫码登录获取的标志码
  117. newUser_sc: '', // 用户扫码进来详情页获取的sc
  118. isBack:false
  119. };
  120. },
  121. onLoad(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. }
  127. console.log('登录后的this.isBack:', option.isBack, typeof this.isBack);
  128. // else if (option.scanCode) {
  129. // this.scanCode = option.scanCode
  130. // }
  131. // this.from = option.from;
  132. if (uni.getStorageSync('newUser_sc')) {
  133. this.newUser_sc = uni.getStorageSync('newUser_sc')
  134. }
  135. console.log('---this.newUser_sc:', this.newUser_sc);
  136. },
  137. onShow() {
  138. // 获取code
  139. this.getwxCode()
  140. },
  141. methods: {
  142. getwxCode() {
  143. uni.login({
  144. provider: 'weixin',
  145. success: (loginRes) => {
  146. this.code = loginRes.code
  147. console.log('获取的code:', this.code)
  148. }
  149. })
  150. },
  151. wxLogin(e) {
  152. this.$api.wxLogin({
  153. code: this.code,
  154. encryptedData: e.detail.encryptedData,
  155. iv: e.detail.iv,
  156. shareCode: this.newUser_sc
  157. }).then(res => {
  158. if (res.data.code == 200) {
  159. this.loginCallback(res, 'wxlogin')
  160. } else {
  161. this.getwxCode() // code用完一次就会过期,防止用户点击取消后再点微信登录后code过期
  162. // this.$u.toast(res.data.msg);
  163. }
  164. })
  165. },
  166. canLogin() {
  167. if(this.current == 0) {
  168. if(this.form.account && this.form.pwd) {
  169. return true;
  170. }
  171. return false;
  172. } else if(this.current == 1) {
  173. if(this.form.tel && this.form.code) {
  174. return true;
  175. }
  176. return false;
  177. }
  178. },
  179. sms_login(){
  180. this.$refs.uForm2.validate(valid => {
  181. if(valid) {
  182. let that = this
  183. if(!this.form.tel){
  184. this.$u.toast('请输入手机号码');
  185. return
  186. }
  187. if(!this.form.code){
  188. this.$u.toast('请输入验证码');
  189. return
  190. }
  191. that.isUse = true
  192. let datas = {
  193. tel:this.form.tel,
  194. code:this.form.code
  195. }
  196. that.$api.smsLogin(datas).then(
  197. res => {
  198. that.isUse = false
  199. if (res.data.code == 200) {
  200. this.loginCallback(res, 'smslogin')
  201. } else {
  202. that.$u.toast(res.data.msg);
  203. }
  204. },
  205. err => {
  206. that.isUse = false
  207. }
  208. );
  209. }
  210. })
  211. },
  212. fakeLogin(){
  213. uni.setStorageSync('user_account', '123');
  214. uni.setStorageSync('token', '123');
  215. uni.switchTab({
  216. url:'/pages/index/index'
  217. })
  218. },
  219. /**
  220. *
  221. * @param {*} data
  222. * @returns
  223. * 加密
  224. */
  225. encryptor(data) {
  226. const encrypt =new JSEncrypt();
  227. encrypt.setPublicKey(PUBLIC_KEY);
  228. return encrypt.encrypt(data);
  229. },
  230. pwlogin(){
  231. let that = this
  232. if(!this.form.account){
  233. this.$u.toast('请输入手机号码/身份证号');
  234. return
  235. }
  236. if(!this.form.pwd){
  237. this.$u.toast('请输入密码');
  238. return
  239. }
  240. //虚拟登录
  241. /* that.fakeLogin()
  242. return */
  243. that.isUse = true
  244. let form = JSON.parse(JSON.stringify(this.form))
  245. form.pwd = this.encryptor(form.pwd)
  246. // 账号登录用户 /app/common/account_login
  247. that.$api.accountLogin(form).then(
  248. res => {
  249. that.isUse = false
  250. if (res.data.code == 200) {
  251. this.loginCallback(res, 'pwlogin')
  252. } else {
  253. that.$u.toast(res.data.msg);
  254. }
  255. },
  256. err => {
  257. that.isUse = false
  258. }
  259. );
  260. },
  261. codeChange(text) {
  262. this.codeTips = text;
  263. },
  264. // 获取验证码
  265. getCode() {
  266. let that = this
  267. if(that.$refs.uCode.canGetCode) {
  268. if(that.$refs.tel.validateState == 'success') {
  269. let datas = {tel:this.form.tel}
  270. that.$api.loginSms(datas).then(
  271. res => {
  272. if (res.data.code == 200) {
  273. that.$u.toast('验证码已发送');
  274. // 通知验证码组件内部开始倒计时
  275. that.$refs.uCode.start();
  276. } else {
  277. that.$u.toast(res.data.msg);
  278. }
  279. },
  280. err => {
  281. console.log(err);
  282. }
  283. );
  284. } else {
  285. this.$refs.tel.onFieldChange()
  286. }
  287. }
  288. },
  289. sectionChange(index) {
  290. this.current = index;
  291. },
  292. getPhoneNumber(e) {
  293. let that = this;
  294. uni.checkSession({
  295. success () {
  296. //session_key 未过期,并且在本生命周期一直有效
  297. that.putInfo(e)
  298. },
  299. fail () {
  300. // session_key 已经失效,需要重新执行登录流程
  301. uni.login({
  302. provider: 'weixin',
  303. success: function(loginRes) {
  304. that.code = loginRes.code;
  305. that.putInfo(e)
  306. }
  307. });
  308. }
  309. })
  310. },
  311. putInfo(e){
  312. let that = this;
  313. if (e.detail.encryptedData) {
  314. let inviteCode = uni.getStorageSync("inviteCode")
  315. //用户同意授权
  316. var datas = {
  317. iv: e.detail.iv,
  318. encryptedData: e.detail.encryptedData,
  319. code: that.code
  320. };
  321. if(inviteCode){
  322. datas.inviteCode = inviteCode
  323. }
  324. that.$api.login(datas).then(
  325. res => {
  326. if (res.data.code == 200) {
  327. uni.setStorageSync('union_id', res.data.data.union_id);
  328. uni.setStorageSync('token', res.data.data.token);
  329. that.$api.getInfo({ fromPlat: 1 }).then(resdata => {
  330. if(resdata.data.code == 200){
  331. uni.navigateBack();
  332. that.$store.state.userInfo = resdata.data.data;
  333. }
  334. });
  335. } else {
  336. uni.showModal({
  337. title: '提示',
  338. content: res.data.msg,
  339. showCancel: false
  340. });
  341. }
  342. },
  343. err => {
  344. console.log(err);
  345. }
  346. );
  347. }
  348. },
  349. // 微信、密码、验证码 登录成功的回调
  350. loginCallback(res, types) {
  351. console.log('登录页面:', this.isBack)
  352. // if(res.data.data && res.data.data.full_info){
  353. uni.removeStorageSync('newUser_sc')
  354. //信息完善,直接进入页面
  355. uni.setStorageSync('user_account', res.data.data.user_account);
  356. uni.setStorageSync('token', res.data.data.token);
  357. // /app/user/getInfo 登录用户信息// fromPlat来源平台 1小程序 2PC网站
  358. this.$api.getInfo({ fromPlat: 1 }).then(resdata => {
  359. if(resdata.data.code == 200){
  360. this.$store.state.userInfo = resdata.data.data;
  361. if(!this.isBack){
  362. let goPath = ''
  363. if (types == 'wxlogin' || types == 'smslogin') { //密码登录不用判断电脑goPath, 直接到首页
  364. goPath = uni.getStorageSync('goPath')
  365. }
  366. if(goPath == 'course') {
  367. uni.redirectTo({
  368. url:'/pages2/wd/class'
  369. })
  370. types == 'wxlogin' && this.$method.setUuid(new Date().valueOf() + "")
  371. } else if(goPath == 'bank') {
  372. uni.redirectTo({
  373. url:'/pages2/wd/question_bank'
  374. })
  375. types == 'wxlogin' && this.$method.setUuid(new Date().valueOf() + "")
  376. } else {
  377. uni.reLaunch({
  378. url:'/pages/index/index'
  379. })
  380. types == 'wxlogin' && this.$method.setUuid(new Date().valueOf() + "")
  381. }
  382. }else{
  383. uni.navigateBack();
  384. }
  385. }
  386. });
  387. // } else {
  388. // //未完善信息,存为临时信息
  389. // uni.setStorageSync('user_account_temp', res.data.data.user_account);
  390. // uni.setStorageSync('token_temp', res.data.data.token);
  391. // this.$navTo.togo('/pages2/register/bind');
  392. // }
  393. },
  394. // scanLoginCheck小程序校验PC登录二维码,执行登录获取到令牌,然后把扫码的路径最后面的6位标识码提交给后台就行
  395. // submitCode() {
  396. // console.log('跳转请求提交code到接口', this.scanCode)
  397. // this.$api.scanLoginCheck({
  398. // scanCode: this.scanCode
  399. // }).then((res) => {
  400. // if (res.data.code == 200) {
  401. // uni.navigateTo({
  402. // url: '/pages4/login/pcLoginSuccess'
  403. // })
  404. // } else {
  405. // this.$u.toast(res.data.msg)
  406. // }
  407. // })
  408. // }
  409. }
  410. };
  411. </script>
  412. <style scoped lang="scss">
  413. .wxBtn{
  414. position: fixed;
  415. bottom: 10%;
  416. width: 100%;
  417. left: 0;
  418. }
  419. /deep/ .wxBtn button::after{
  420. border: none;
  421. }
  422. .loginBtn{
  423. width: 526rpx;
  424. height: 80rpx;
  425. background: linear-gradient(90deg, #015EEA, #00C0FA);
  426. box-shadow: 0rpx 10rpx 16rpx 4rpx rgba(1, 99, 235, 0.04);
  427. opacity: 0.6;
  428. border-radius: 40rpx;
  429. color: #FFFFFF;
  430. text-align: center;
  431. line-height: 80rpx;
  432. margin: 40rpx auto;
  433. &.able {
  434. opacity: 1;
  435. }
  436. }
  437. .wxloginBtn{
  438. margin:0 auto;
  439. background: url("/static/loginBtn.png") no-repeat;
  440. background-size:100% 100%;
  441. border:none;
  442. width: 100rpx;
  443. height: 100rpx;
  444. }
  445. /deep/page {
  446. background-color: #FFFFFF;
  447. height: 100%;
  448. width: 100%;
  449. }
  450. .login_box{
  451. width: 100%;
  452. height: 360rpx;
  453. background: #FFFFFF;
  454. box-shadow: 0rpx 0rpx 16rpx 4rpx rgba(1, 99, 235, 0.1);
  455. border-radius: 24rpx;
  456. margin-top: 30rpx;
  457. padding:40rpx 35rpx;
  458. .password {
  459. /deep/.uicon-eye-fill {
  460. &::before{
  461. color:#007AFF;
  462. content:"\e613";
  463. }
  464. }
  465. }
  466. }
  467. /deep/ .u-item-bg{
  468. border-radius: 32px !important;
  469. }
  470. /deep/ .u-subsection{
  471. border-radius: 32px !important;
  472. }
  473. .full_img {
  474. position: absolute;
  475. left: 0;
  476. display: block;
  477. width: 100%;
  478. z-index: -999;
  479. top: 0;
  480. }
  481. .head {
  482. height: 96rpx;
  483. width: 100%;
  484. line-height: 96rpx;
  485. margin-top: 40rpx;
  486. text-align: center;
  487. display: flex;
  488. position: relative;
  489. justify-content: center;
  490. }
  491. .icon {
  492. position: absolute;
  493. left: 30rpx;
  494. }
  495. </style>