Prechádzať zdrojové kódy

feat: 登录逻辑中新增首次登录逻辑判断

zhouyuhao 11 mesiacov pred
rodič
commit
59d0fbfdfd
2 zmenil súbory, kde vykonal 60 pridanie a 93 odobranie
  1. 7 3
      src/stores/modules/user.ts
  2. 53 90
      src/views/Login/src/loginView.vue

+ 7 - 3
src/stores/modules/user.ts

@@ -16,8 +16,10 @@ export const useUserStore = defineStore('user', {
     setUsername(username: any, isFirstLogin?: boolean) {
       localStorage.setItem('username', username)
       this.username = username
-      this.isFirstLogin = isFirstLogin
-      localStorage.setItem('isFirstLogin', JSON.stringify(isFirstLogin))
+      if (isFirstLogin !== undefined) {
+        localStorage.setItem('isFirstLogin', JSON.stringify(isFirstLogin))
+        this.isFirstLogin = isFirstLogin
+      }
     },
     async logout(isNeedLogout: boolean = true) {
       if (isNeedLogout) {
@@ -25,7 +27,9 @@ export const useUserStore = defineStore('user', {
       }
       localStorage.removeItem('username')
       this.username = ''
-      localStorage.removeItem('isFirstLogin')
+      if (localStorage.getItem('isFirstLogin')) {
+        localStorage.removeItem('isFirstLogin')
+      }
       this.isFirstLogin = false
     }
   }

+ 53 - 90
src/views/Login/src/loginView.vue

@@ -31,51 +31,7 @@ const handleTokenRedirect = () => {
         token: obj.token
       })
       .then((res: any) => {
-        if (res.code === 200) {
-          const { data } = res
-          if (data.msg === 'today' || data.msg === 'last') {
-            const expiredDays = data.msg === 'today' ? 'today' : `in ${data.data} days`
-            ElMessageBox.confirm(
-              `Your password will expire ${expiredDays}, please reset your password`,
-              'Prompt',
-              {
-                confirmButtonText: 'OK',
-                cancelButtonText: 'Change Password',
-                type: 'warning',
-                confirmButtonClass: 'el-button--dark',
-                cancelButtonClass: 'el-button--main',
-                distinguishCancelAndClose: true
-              }
-            ).catch((action: any) => {
-              if (action === 'cancel') {
-                router.push({
-                  name: 'Reset Password'
-                })
-              }
-            })
-          }
-          userStore.setUsername(res.data.uname || '')
-          router.push('/')
-        } else if (res.code === 400) {
-          const { data } = res
-          if (data.msg === 'Invalid token') {
-            ElMessageBox.alert('Invalid token', 'Prompt', {
-              confirmButtonText: 'OK',
-              type: 'warning',
-              confirmButtonClass: 'el-button--dark'
-            })
-          } else if (data.msg === 'passwordExpires') {
-            ElMessageBox.alert('Password expired, please change your password', 'Prompt', {
-              confirmButtonText: 'OK',
-              type: 'warning',
-              confirmButtonClass: 'el-button--dark'
-            })
-            userStore.setUsername(res.data.uname || '')
-            router.push({
-              name: 'Reset Password'
-            })
-          }
-        }
+        handleResult(res)
       })
   }
 }
@@ -216,6 +172,57 @@ const confirmVerification = () => {
   }
 }
 
+const handleResult = (res: any) => {
+  if (res.code === 200) {
+    const { data } = res
+    if (data.msg === 'today' || data.msg === 'last') {
+      const expiredDays = data.msg === 'today' ? 'today' : `in ${data.data} days`
+      ElMessageBox.confirm(
+        `Your password will expire ${expiredDays}, please reset your password`,
+        'Prompt',
+        {
+          confirmButtonText: 'OK',
+          cancelButtonText: 'Change Password',
+          type: 'warning',
+          confirmButtonClass: 'el-button--dark',
+          cancelButtonClass: 'el-button--main',
+          distinguishCancelAndClose: true
+        }
+      ).catch((action: any) => {
+        if (action === 'cancel') {
+          router.push({
+            name: 'Reset Password'
+          })
+        }
+      })
+    }
+    userStore.setUsername(res.data.uname || '')
+    router.push('/')
+  } else if (res.code === 400) {
+    const { data } = res
+    if (data.msg === 'Invalid token') {
+      ElMessageBox.alert('Invalid token', 'Prompt', {
+        confirmButtonText: 'OK',
+        type: 'warning',
+        confirmButtonClass: 'el-button--dark'
+      })
+    } else if (data.msg === 'passwordExpires') {
+      ElMessageBox.alert('Please change your password for security.', 'Prompt', {
+        confirmButtonText: 'Change Password',
+        type: 'warning',
+        confirmButtonClass: 'el-button--dark'
+      })
+      userStore.setUsername(res.data.uname || '')
+      router.push({
+        name: 'Reset Password'
+      })
+    } else if (data.msg === 'First login, please change your password') {
+      userStore.setUsername(res.data.uname, true)
+      firstLoginTipsRef.value.openDialog()
+    }
+  }
+}
+
 // 验证结束后调用登录接口
 const handleLoginAfterVerify = () => {
   $api
@@ -226,57 +233,13 @@ const handleLoginAfterVerify = () => {
     })
     .then((res: any) => {
       if (res.code === 200) {
-        const { data } = res
         if (isRememerPwd.value) {
           saveCredentials()
         } else {
           clearCredentials()
         }
-        if (data.msg === 'today' || data.msg === 'last') {
-          const expiredDays = data.msg === 'today' ? 'today' : `in ${data.data} days`
-          ElMessageBox.confirm(
-            `Your password will expire ${expiredDays}, please reset your password`,
-            'Prompt',
-            {
-              confirmButtonText: 'OK',
-              cancelButtonText: 'Change Password',
-              type: 'warning',
-              confirmButtonClass: 'el-button--dark',
-              cancelButtonClass: 'el-button--main',
-              distinguishCancelAndClose: true
-            }
-          ).catch((action: any) => {
-            if (action === 'cancel') {
-              router.push({
-                name: 'Reset Password'
-              })
-            }
-          })
-        }
-        userStore.setUsername(res.data.uname || '')
-        router.push('/')
-      } else if (res.code === 400) {
-        const { data } = res
-        if (data.msg === 'password_error') {
-          loginError.value.password = true
-        } else if (data.msg === 'error_times') {
-          errorTipsRef.value.openDialog()
-        } else if (data.msg === 'passwordExpires') {
-          ElMessageBox.alert('Please change your password for security.', 'Prompt', {
-            confirmButtonText: 'Change Password',
-            type: 'warning',
-            confirmButtonClass: 'el-button--dark'
-          }).then(() => {
-            userStore.setUsername(res.data.uname)
-            router.push({
-              name: 'Reset Password'
-            })
-          })
-        } else if (data.msg === 'First login, please change your password') {
-          userStore.setUsername(res.data.uname, true)
-          firstLoginTipsRef.value.openDialog()
-        }
       }
+      handleResult(res)
     })
 }