login.vue 13 KB

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