Jelajahi Sumber

修复登录出错没有错误提示

Yue 2 tahun lalu
induk
melakukan
9c474291cc
2 mengubah file dengan 54 tambahan dan 58 penghapusan
  1. 21 23
      src/stores/_auth.ts
  2. 33 35
      src/views/account/SignIn.vue

+ 21 - 23
src/stores/_auth.ts

@@ -14,27 +14,20 @@ export interface User {
 
 export const useAuthStore = defineStore("auth", () => {
   const USER_KEY = "user"
-  const errors = ref({})
   const user = ref<User | null>(JSON.parse(storages.getItem(USER_KEY) ?? "{}"))
   const isAuthenticated = ref(!!JwtService.getToken())
 
   function setAuth(authUser: User) {
     isAuthenticated.value = true
-    errors.value = {}
     user.value = authUser
     console.log("AUTHUSER", authUser)
     storages.setItem(USER_KEY, JSON.stringify(authUser))
     JwtService.saveToken(authUser.token)
   }
 
-  function setError(error: any) {
-    errors.value = { ...error }
-  }
-
   function purgeAuth() {
     isAuthenticated.value = false
     user.value = null
-    errors.value = []
     storages.removeItem(USER_KEY)
     appStore.menuStore.removeMenus()
     JwtService.destroyToken()
@@ -42,22 +35,28 @@ export const useAuthStore = defineStore("auth", () => {
 
   function login(userName: string, password: string) {
     purgeAuth()
+    //console.log("LOGIN")
     return new Promise<void>((resolve, reject) => {
-      apis.authApi.login({ userName: userName, password: password }).then(({ data }) => {
-        if (data) {
-          const result = data
-          setAuth({
-            userName: result.authName || "",
-            userType: result.userType || "0",
-            orgId: result.enterpriseId || "",
-            token: result.auth,
-          })
-          resolve(result.loginMsg)
-        } else {
-          setError(data.msg)
-          reject(data)
-        }
-      })
+      apis.authApi
+        .login({ userName: userName, password: password })
+        .then(({ data }) => {
+          if (data) {
+            const result = data
+            setAuth({
+              userName: result.authName || "",
+              userType: result.userType || "0",
+              orgId: result.enterpriseId || "",
+              token: result.auth,
+            })
+            resolve(result.loginMsg)
+          } else {
+            reject(data.msg)
+          }
+        })
+        .catch(() => {
+          //console.log("ERROR_LOGIN", e)
+          reject("用户名或密码错误!")
+        })
     })
   }
 
@@ -74,7 +73,6 @@ export const useAuthStore = defineStore("auth", () => {
   }
 
   return {
-    errors,
     user,
     isAuthenticated,
     login,

+ 33 - 35
src/views/account/SignIn.vue

@@ -32,44 +32,42 @@ const onSubmitLogin = async (values: any) => {
   }
 
   // Send login request
-  await store.login(values.userName, values.password)
-  const error = Object.values(store.errors)
-
-  if (error.length === 0) {
-    Swal.fire({
-      text: "登录成功",
-      icon: "success",
-      buttonsStyling: false,
-      confirmButtonText: "确定",
-      heightAuto: false,
-      customClass: {
-        confirmButton: "btn fw-semobold btn-light-primary",
-      },
-    }).then(() => {
-      // Go to page after successfully login
-      //router.push({ name: "dashboard" })
-      appStore.menuStore.reLoadMenus()?.then(() => {
-        let queryRedirectPath = "/"
-        if (router.currentRoute.value.redirectedFrom?.fullPath) {
-          queryRedirectPath = router.currentRoute.value.redirectedFrom?.fullPath
-        }
-        router.push({ path: queryRedirectPath })
+  store
+    .login(values.userName, values.password)
+    .then(() => {
+      Swal.fire({
+        text: "登录成功",
+        icon: "success",
+        buttonsStyling: false,
+        confirmButtonText: "确定",
+        heightAuto: false,
+        customClass: {
+          confirmButton: "btn fw-semobold btn-light-primary",
+        },
+      }).then(() => {
+        // Go to page after successfully login
+        //router.push({ name: "dashboard" })
+        appStore.menuStore.reLoadMenus()?.then(() => {
+          let queryRedirectPath = "/"
+          if (router.currentRoute.value.redirectedFrom?.fullPath) {
+            queryRedirectPath = router.currentRoute.value.redirectedFrom?.fullPath
+          }
+          router.push({ path: queryRedirectPath })
+        })
       })
     })
-  } else {
-    Swal.fire({
-      text: error[0] as string,
-      icon: "error",
-      buttonsStyling: false,
-      confirmButtonText: "再试一次!",
-      heightAuto: false,
-      customClass: {
-        confirmButton: "btn fw-semobold btn-light-danger",
-      },
-    }).then(() => {
-      store.errors = {}
+    .catch((msg: string) => {
+      Swal.fire({
+        text: msg,
+        icon: "error",
+        buttonsStyling: false,
+        confirmButtonText: "再试一次!",
+        heightAuto: false,
+        customClass: {
+          confirmButton: "btn fw-semobold btn-light-danger",
+        },
+      })
     })
-  }
 
   //Deactivate indicator
   submitButton.value?.removeAttribute("data-kt-indicator")