feat(router): 在路由守卫中添加用户信息接口请求
在`checkAuth`函数中增加对用户信息接口的请求,以更新`currentUser`并优化登录权限检查逻辑。如果接口请求失败或返回401,则继续使用原有的本地存储判断逻辑。
Showing
1 changed file
with
16 additions
and
2 deletions
| 1 | /* | 1 | /* |
| 2 | * @Date: 2025-03-20 20:36:36 | 2 | * @Date: 2025-03-20 20:36:36 |
| 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com | 3 | * @LastEditors: hookehuyr hookehuyr@gmail.com |
| 4 | - * @LastEditTime: 2025-03-26 08:04:21 | 4 | + * @LastEditTime: 2025-03-26 09:45:57 |
| 5 | * @FilePath: /mlaj/src/router/guards.js | 5 | * @FilePath: /mlaj/src/router/guards.js |
| 6 | * @Description: 路由守卫逻辑 | 6 | * @Description: 路由守卫逻辑 |
| 7 | */ | 7 | */ |
| 8 | import { getAuthInfoAPI } from '@/api/auth' | 8 | import { getAuthInfoAPI } from '@/api/auth' |
| 9 | +import { getUserInfoAPI } from '@/api/user' | ||
| 9 | import { wxInfo } from "@/utils/tools" | 10 | import { wxInfo } from "@/utils/tools" |
| 10 | 11 | ||
| 11 | // 需要登录才能访问的路由 | 12 | // 需要登录才能访问的路由 |
| ... | @@ -52,7 +53,20 @@ export const checkWxAuth = async () => { | ... | @@ -52,7 +53,20 @@ export const checkWxAuth = async () => { |
| 52 | } | 53 | } |
| 53 | 54 | ||
| 54 | // 登录权限检查 | 55 | // 登录权限检查 |
| 55 | -export const checkAuth = (to) => { | 56 | +export const checkAuth = async (to) => { |
| 57 | + try { | ||
| 58 | + // 先请求用户信息接口 | ||
| 59 | + const { code, data } = await getUserInfoAPI(); | ||
| 60 | + if (code) { | ||
| 61 | + // 如果成功获取用户信息,更新currentUser并允许访问 | ||
| 62 | + localStorage.setItem('currentUser', JSON.stringify(data)); | ||
| 63 | + return true; | ||
| 64 | + } | ||
| 65 | + } catch (error) { | ||
| 66 | + console.error('获取用户信息失败:', error); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + // 如果接口请求失败或返回401,继续原有的本地存储判断逻辑 | ||
| 56 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) | 70 | const currentUser = JSON.parse(localStorage.getItem('currentUser')) |
| 57 | 71 | ||
| 58 | // 检查当前路由是否需要认证 | 72 | // 检查当前路由是否需要认证 | ... | ... |
-
Please register or login to post a comment