Browse Source

登录优化

wuyi 5 ngày trước cách đây
mục cha
commit
69783b6efd
2 tập tin đã thay đổi với 18 bổ sung8 xóa
  1. 11 6
      src/router/index.js
  2. 7 2
      src/stores/user.js

+ 11 - 6
src/router/index.js

@@ -65,13 +65,18 @@ router.beforeEach(async (to, from, next) => {
       }
       next()
     } else if (userStore.token) {
-      await userStore.sync()
-      const userRole = userStore.userInfo.role
-      if (!hasRoutePermission(to, userRole)) {
-        next('/main/dashboard')
-        return
+      try {
+        await userStore.sync()
+        const userRole = userStore.userInfo.role
+        if (!hasRoutePermission(to, userRole)) {
+          next('/main/dashboard')
+          return
+        }
+        next()
+      } catch (error) {
+        console.error('获取用户信息失败:', error)
+        next('/login')
       }
-      next()
     } else {
       next('/login')
     }

+ 7 - 2
src/stores/user.js

@@ -25,8 +25,13 @@ export const useUserStore = defineStore('user', () => {
   }
 
   const sync = async () => {
-    const response = await profile()
-    setUserInfo(response)
+    try {
+      const response = await profile()
+      setUserInfo(response)
+    } catch (error) {
+      logout()
+      throw error
+    }
   }
 
   const logout = () => {