Преглед изворни кода

登录添加验证码验证

GaoYuPeng пре 4 година
родитељ
комит
cd10e48035
2 измењених фајлова са 231 додато и 164 уклоњено
  1. 10 2
      shanXiPlatform/src/api/user.js
  2. 221 162
      shanXiPlatform/src/views/login/index.vue

+ 10 - 2
shanXiPlatform/src/api/user.js

@@ -1,7 +1,7 @@
1 1
 /*
2 2
  * @Author: your name
3 3
  * @Date: 2020-12-17 13:59:37
4
- * @LastEditTime: 2021-03-29 14:44:39
4
+ * @LastEditTime: 2021-04-06 10:28:31
5 5
  * @LastEditors: Please set LastEditors
6 6
  * @Description: In User Settings Edity
7 7
  * @FilePath: \csc-szls-vue\shanXiPlatform\src\api\user.js
@@ -38,7 +38,15 @@ export function captcha() {
38 38
     method: 'get'
39 39
   })
40 40
 }
41
-
41
+// 验证码验证
42
+///public/verifyCode
43
+export function verifyCaptcha(params) {
44
+  return request({
45
+    url: '/csc-szls-system-manage/public/verifyCode',
46
+    method: 'get',
47
+    params
48
+  })
49
+}
42 50
 
43 51
 
44 52
 //用户字典列表

+ 221 - 162
shanXiPlatform/src/views/login/index.vue

@@ -65,10 +65,7 @@
65 65
                   name="password"
66 66
                   tabindex="2"
67 67
                   autocomplete="off"
68
-                  @keyup.native="checkCapslock"
69
-                  @blur="capsTooltip = false"
70 68
                   @keyup.enter.native="handleLogin()"
71
-
72 69
                 />
73 70
                 <span class="show-pwd" @click="showPwd">
74 71
                   <svg-icon
@@ -81,19 +78,24 @@
81 78
             </el-tooltip>
82 79
 
83 80
             <!-- <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button> -->
84
-           <!--  <el-form-item prop="validationCode" class="captchDiv">
85
-              
81
+            <el-form-item prop="captchaCode" class="captchDiv">
86 82
               <el-input
87
-                ref="validationCode"
88
-                v-model="loginForm.validationCode"
83
+                ref="captchaCode"
84
+                v-model="loginForm.captchaCode"
89 85
                 placeholder="验证码"
90
-                name="validationCode"
86
+                name="captchaCode"
91 87
                 type="text"
92
-                tabindex="1"
88
+                tabindex="3"
89
+                @blur="validationCaptcha()"
93 90
                 class="validationCode"
94 91
               />
95
-              <img :src="captcha" alt="验证码" class="captchImg">
96
-            </el-form-item> -->
92
+              <img
93
+                :src="captcha"
94
+                alt="验证码"
95
+                class="captchImg"
96
+                @click="getCaptcha()"
97
+              />
98
+            </el-form-item>
97 99
             <el-button
98 100
               :loading="loading"
99 101
               type="primary"
@@ -134,7 +136,16 @@
134 136
 <script>
135 137
 // import { validUsername } from "@/utils/validate"
136 138
 import SocialSign from "./components/SocialSignin"
137
-import { login, deportDic, hourseDic, personDic, departDic ,sidebarPermission, captcha} from "@/api/user"
139
+import {
140
+  login,
141
+  deportDic,
142
+  hourseDic,
143
+  personDic,
144
+  departDic,
145
+  sidebarPermission,
146
+  captcha,
147
+  verifyCaptcha
148
+} from "@/api/user"
138 149
 import {
139 150
   getToken,
140 151
   setToken,
@@ -156,8 +167,19 @@ export default {
156 167
       }
157 168
     }
158 169
     const validatePassword = (rule, value, callback) => {
159
-      if (value.length < 6) {
160
-        callback(new Error("密码输入错误"))
170
+      if (value !== "" && value.length < 6) {
171
+        callback(new Error("密码输入错误!"))
172
+      } else if (value === "") {
173
+        callback(new Error("请输入密码!"))
174
+      } else {
175
+        callback()
176
+      }
177
+    }
178
+    const validateCaptcha = (rule, value, callback) => {
179
+      if (value === "") {
180
+        callback(new Error("请输入验证码!"))
181
+      } else if (value.length != 5) {
182
+        callback(new Error("验证码输入错误"))
161 183
       } else {
162 184
         callback()
163 185
       }
@@ -166,7 +188,7 @@ export default {
166 188
       loginForm: {
167 189
         username: "",
168 190
         password: "",
169
-        validationCode:""
191
+        captchaCode: "" //验证码输入
170 192
       },
171 193
       loginRules: {
172 194
         username: [
@@ -177,17 +199,21 @@ export default {
177 199
         password: [
178 200
           { required: true, trigger: "blur", validator: validatePassword }
179 201
         ],
180
-        // validationCode:[
181
-        //   { required: true, trigger: "blur", validator: validationCode }
182
-        // ]
202
+        captchaCode: [
203
+          { required: true, message: "请输入验证码", trigger: "blur" },
204
+          { validator: validateCaptcha, trigger: "blur" }
205
+        ]
183 206
       },
184 207
       // disabled:true,
208
+
209
+      key: "", //验证码绑定key
210
+      flag: false,
185 211
       passwordType: "password",
186 212
       capsTooltip: false,
187 213
       loading: false,
188 214
       showDialog: false,
189 215
       redirect: undefined,
190
-      captcha:'',
216
+      captcha: "",
191 217
       otherQuery: {}
192 218
     }
193 219
   },
@@ -211,7 +237,9 @@ export default {
211 237
       this.$refs.username.focus()
212 238
     } else if (this.loginForm.password === "") {
213 239
       this.$refs.password.focus()
214
-    }
240
+    } /* else if (this.validationCode === '') {
241
+      this.$refs.validationCode.focus()
242
+    } */
215 243
     this.getCaptcha()
216 244
   },
217 245
   destroyed() {
@@ -234,15 +262,37 @@ export default {
234 262
     },
235 263
     // 获取验证码
236 264
     getCaptcha() {
237
-      captcha().then(res =>{
238
-        if(res.code ==200){
239
-          // this.loginForm.validationCode = res.data
240
-          this.captcha = res.data.image
241
-          console.log(res,'获取验证码成功')
242
-        }
243
-      }).catch(err =>{
244
-        console.log(err)
245
-      })
265
+      captcha()
266
+        .then(res => {
267
+          if (res.code == 200) {
268
+            // this.validationCode = res.data
269
+            this.captcha = res.data.image
270
+            this.key = res.data.key
271
+          }
272
+        })
273
+        .catch(err => {
274
+          console.log(err)
275
+        })
276
+    },
277
+    // 验证码验证
278
+    validationCaptcha() {
279
+      let params = {
280
+        key: this.key,
281
+        verCode: this.loginForm.captchaCode
282
+      }
283
+      verifyCaptcha(params)
284
+        .then(res => {
285
+          if (res.code == 200) {
286
+            this.flag = true
287
+          } else {
288
+            this.flag = false
289
+
290
+            this.getCaptcha()
291
+          }
292
+        })
293
+        .catch(err => {
294
+          console.log(err, "err验证码")
295
+        })
246 296
     },
247 297
     handleLogin() {
248 298
       this.$refs.loginForm.validate(valid => {
@@ -252,113 +302,124 @@ export default {
252 302
             uName: this.loginForm.username,
253 303
             uPassword: this.loginForm.password
254 304
           }
255
-          login(loginInfo)
256
-            .then(res => {
257
-              if (res.code == 200) {
258
-                this.$message({
259
-                  message: "登录成功",
260
-                  type: "success"
261
-                })
262
-                localStorage.setItem("userInfo", JSON.stringify(res.data.user))
263
-                // console.log(res.data.user.homeType, "homeType")
264
-                var Authorization =
265
-                  res.data.tokenInfo.token_type +
266
-                  " " +
267
-                  res.data.tokenInfo.access_token
268
-                setToken(Authorization)
269
-                setUser(res.data.user)
270
-
271
-                this.getDeportDic()
272
-                this.getHourseDic()
273
-                this.getPersonDic()
274
-                this.getDepartDic()
275
-                this.getpermission(res.data.user.homeType)
276
-                this.$store.state.isAuthority = true
277
-                this.depotId = res.data.user.depotId
278
-                this.$store.commit({
279
-                  type: "changeDepartmentName",
280
-                  departmentName: res.data.user.departmentName
281
-                })
282
-
283
-                this.$store.commit({
284
-                  type: "changeppost",
285
-                  ppost: res.data.user.ppost
286
-                })
287
-
288
-                this.$store.commit({
289
-                  type: "changeDepotId",
290
-                  depotId: res.data.user.depotId
291
-                })
292
-
293
-                this.$store.commit({
294
-                  type: "changeDepotName",
295
-                  depotName: res.data.user.depotName
296
-                })
297
-                if (
298
-                  res.data.user.storehouseIds &&
299
-                  res.data.user.storehouseIds.length > 0
300
-                ) {
305
+          if (this.flag) {
306
+            login(loginInfo)
307
+              .then(res => {
308
+                if (res.code == 200) {
309
+                  this.$message({
310
+                    message: "登录成功",
311
+                    type: "success"
312
+                  })
313
+                  localStorage.setItem(
314
+                    "userInfo",
315
+                    JSON.stringify(res.data.user)
316
+                  )
317
+                  // console.log(res.data.user.homeType, "homeType")
318
+                  var Authorization =
319
+                    res.data.tokenInfo.token_type +
320
+                    " " +
321
+                    res.data.tokenInfo.access_token
322
+                  setToken(Authorization)
323
+                  setUser(res.data.user)
324
+
325
+                  this.getDeportDic()
326
+                  this.getHourseDic()
327
+                  this.getPersonDic()
328
+                  this.getDepartDic()
329
+                  this.getpermission(res.data.user.homeType)
330
+                  this.$store.state.isAuthority = true
331
+                  this.depotId = res.data.user.depotId
301 332
                   this.$store.commit({
302
-                    type: "changeHouseId",
303
-                    houseId: res.data.user.storehouseIds.split(",")[0]
333
+                    type: "changeDepartmentName",
334
+                    departmentName: res.data.user.departmentName
304 335
                   })
305
-                }
306
-                if (
307
-                  res.data.user.storehouseNames &&
308
-                  res.data.user.storehouseNames.length > 0
309
-                ) {
336
+
310 337
                   this.$store.commit({
311
-                    type: "changeHouseName",
312
-                    houseName: res.data.user.storehouseNames.split(",")[0]
338
+                    type: "changeppost",
339
+                    ppost: res.data.user.ppost
313 340
                   })
314
-                }
315
-                this.$store.commit({
316
-                  type: "changeuId",
317
-                  uid: res.data.user.uid
318
-                })
319
-                // sessionStorage.setItem(
320
-                //   "store",
321
-                //   JSON.stringify(this.$store.state)
322
-                // )
323
-                //把用户信息存入缓存
324
-                // console.log(res.data.user.depotId, "5665564433224556533")
325
-                // console.log(res.data.user.homeType,"查看用户类型,1-分公司首页,2-库及以上领导首页,3-员工首页" )
326
-                // if (res.data.user.homeType === 1) {
327
-                //   // 跳转分公司首页
328
-                //   this.$router.push({ path: "/dashboard/dashboard" })
329
-                //   // this.$router.push({path:'daiban'})
330
-                //   // this.$router.push({ path: "/" })
331
-                // } else if (res.data.user.homeType === 2) {
332
-                //   // 跳转库及以上领导首页
333
-                //   this.$router.push({ path: "/dashboard/dashboard" })
334
-                //   // this.$router.push({ path: "/" })
335
-                // } else {
336
-                //   // 跳转员工首页(当前员工首页未定,暂跳转待办页)
337
-                //   // this.$router.push({ path: "daiban" })
338
-                //   this.$router.push({ path: "/home" })
339
-                // }
340
-
341
-                /* if (res.data.user.depotId == 1) {
341
+
342
+                  this.$store.commit({
343
+                    type: "changeDepotId",
344
+                    depotId: res.data.user.depotId
345
+                  })
346
+
347
+                  this.$store.commit({
348
+                    type: "changeDepotName",
349
+                    depotName: res.data.user.depotName
350
+                  })
351
+                  if (
352
+                    res.data.user.storehouseIds &&
353
+                    res.data.user.storehouseIds.length > 0
354
+                  ) {
355
+                    this.$store.commit({
356
+                      type: "changeHouseId",
357
+                      houseId: res.data.user.storehouseIds.split(",")[0]
358
+                    })
359
+                  }
360
+                  if (
361
+                    res.data.user.storehouseNames &&
362
+                    res.data.user.storehouseNames.length > 0
363
+                  ) {
364
+                    this.$store.commit({
365
+                      type: "changeHouseName",
366
+                      houseName: res.data.user.storehouseNames.split(",")[0]
367
+                    })
368
+                  }
369
+                  this.$store.commit({
370
+                    type: "changeuId",
371
+                    uid: res.data.user.uid
372
+                  })
373
+                  // sessionStorage.setItem(
374
+                  //   "store",
375
+                  //   JSON.stringify(this.$store.state)
376
+                  // )
377
+                  //把用户信息存入缓存
378
+                  // console.log(res.data.user.depotId, "5665564433224556533")
379
+                  // console.log(res.data.user.homeType,"查看用户类型,1-分公司首页,2-库及以上领导首页,3-员工首页" )
380
+                  // if (res.data.user.homeType === 1) {
381
+                  //   // 跳转分公司首页
382
+                  //   this.$router.push({ path: "/dashboard/dashboard" })
383
+                  //   // this.$router.push({path:'daiban'})
384
+                  //   // this.$router.push({ path: "/" })
385
+                  // } else if (res.data.user.homeType === 2) {
386
+                  //   // 跳转库及以上领导首页
387
+                  //   this.$router.push({ path: "/dashboard/dashboard" })
388
+                  //   // this.$router.push({ path: "/" })
389
+                  // } else {
390
+                  //   // 跳转员工首页(当前员工首页未定,暂跳转待办页)
391
+                  //   // this.$router.push({ path: "daiban" })
392
+                  //   this.$router.push({ path: "/home" })
393
+                  // }
394
+
395
+                  /* if (res.data.user.depotId == 1) {
342 396
                   this.$router.push({ path: "/dashboard" })
343 397
                 } else {
344 398
                   this.$router.push({ path: "/" })
345 399
 
346 400
                 } */
347
-                this.getStateAll()
348
-              } else {
401
+                  this.getStateAll()
402
+                } else {
403
+                  this.loading = false
404
+                  // console.log(res.msg, "res.mag...")
405
+                  this.$message({
406
+                    message: res.msg,
407
+                    type: "error"
408
+                  })
409
+                }
410
+              })
411
+              .catch(err => {
412
+                // console.log(err,'err信息')
413
+                console.log(err)
349 414
                 this.loading = false
350
-                // console.log(res.msg, "res.mag...")
351
-                this.$message({
352
-                  message: res.msg,
353
-                  type: "error"
354
-                })
355
-              }
356
-            })
357
-            .catch(err => {
358
-              // console.log(err,'err信息')
359
-              // console.log(err.msg)
360
-              this.loading = false
415
+              })
416
+          } else {
417
+            this.loading = false
418
+            this.$message({
419
+              message: "验证码错误,请重新输入",
420
+              type: "err"
361 421
             })
422
+          }
362 423
         } else {
363 424
           console.log("error submit!!")
364 425
           return false
@@ -369,7 +430,6 @@ export default {
369 430
     getDeportDic() {
370 431
       deportDic()
371 432
         .then(res => {
372
-          console.log(res, "mmmmmmm")
373 433
           if (res.code == 200) {
374 434
             // this.deportDic=res.data;
375 435
             localStorage.setItem("deportDic", JSON.stringify(res.data))
@@ -383,7 +443,7 @@ export default {
383 443
     getHourseDic() {
384 444
       hourseDic()
385 445
         .then(res => {
386
-          console.log(res, "仓房")
446
+          // console.log(res, "仓房")
387 447
           if (res.code == 200) {
388 448
             // this.deportDic=res.data;
389 449
             localStorage.setItem("hourseDic", JSON.stringify(res.data))
@@ -401,7 +461,7 @@ export default {
401 461
       // }
402 462
       personDic()
403 463
         .then(res => {
404
-          console.log(res, "人员")
464
+          // console.log(res, "人员")
405 465
           if (res.code == 200) {
406 466
             // this.deportDic=res.data;
407 467
             localStorage.setItem("perList", JSON.stringify(res.data))
@@ -415,7 +475,7 @@ export default {
415 475
     getDepartDic() {
416 476
       departDic()
417 477
         .then(res => {
418
-          console.log(res, "mmmmmmm")
478
+          // console.log(res, "mmmmmmm")
419 479
           if (res.code == 200) {
420 480
             // this.deportDic=res.data;
421 481
             localStorage.setItem("departDic", JSON.stringify(res.data))
@@ -474,40 +534,38 @@ export default {
474 534
     //   }
475 535
     // }
476 536
 
477
-    getpermission(homeType){
478
-
479
-      var userInfo= JSON.parse(localStorage.getItem("userInfo"));
480
-      console.log(userInfo,"当前用户信息")
481
-      var params={
482
-        id:userInfo.roleIds
537
+    getpermission(homeType) {
538
+      var userInfo = JSON.parse(localStorage.getItem("userInfo"))
539
+      // console.log(userInfo,"当前用户信息")
540
+      var params = {
541
+        id: userInfo.roleIds
483 542
       }
484 543
       sidebarPermission(params)
485
-      .then(res=>{
486
-        if(res.code == 200){
487
-
488
-             var permissions = res.data;
489
-             console.log(permissions,"当前用户权限。。。。。。")
490
-
491
-            localStorage.setItem("permission",JSON.stringify(permissions));
492
-             if (homeType === 1) {
493
-                  // 跳转分公司首页
494
-                  this.$router.push({ path: "/dashboard/dashboard" })
495
-                  // this.$router.push({path:'daiban'})
496
-                  // this.$router.push({ path: "/" })
497
-                } else if (homeType === 2) {
498
-                  // 跳转库及以上领导首页
499
-                  this.$router.push({ path: "/dashboard/dashboard" })
500
-                  // this.$router.push({ path: "/" })
501
-                } else {
502
-                  // 跳转员工首页(当前员工首页未定,暂跳转待办页)
503
-                  // this.$router.push({ path: "daiban" })
504
-                  this.$router.push({ path: "/home" })
505
-                }
506
-        }
507
-      })
508
-      .catch(err=>{
509
-        console.log(err)
510
-      })
544
+        .then(res => {
545
+          if (res.code == 200) {
546
+            var permissions = res.data
547
+            //  console.log(permissions,"当前用户权限。。。。。。")
548
+
549
+            localStorage.setItem("permission", JSON.stringify(permissions))
550
+            if (homeType === 1) {
551
+              // 跳转分公司首页
552
+              this.$router.push({ path: "/dashboard/dashboard" })
553
+              // this.$router.push({path:'daiban'})
554
+              // this.$router.push({ path: "/" })
555
+            } else if (homeType === 2) {
556
+              // 跳转库及以上领导首页
557
+              this.$router.push({ path: "/dashboard/dashboard" })
558
+              // this.$router.push({ path: "/" })
559
+            } else {
560
+              // 跳转员工首页(当前员工首页未定,暂跳转待办页)
561
+              // this.$router.push({ path: "daiban" })
562
+              this.$router.push({ path: "/home" })
563
+            }
564
+          }
565
+        })
566
+        .catch(err => {
567
+          console.log(err)
568
+        })
511 569
     }
512 570
   }
513 571
 }
@@ -822,12 +880,13 @@ input:-webkit-autofill {
822 880
   flex-direction:row
823 881
 } */
824 882
 .validationCode {
825
-  width:60%
883
+  width: 60%;
826 884
 }
827 885
 .captchImg {
828 886
   width: 130px;
829 887
   background: cadetblue;
830 888
   vertical-align: middle;
889
+  cursor: pointer;
831 890
   // display: inline-block;
832 891
 }
833 892
 </style>