login.vue 13 KB

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