hookehuyr

feat(router): 在路由守卫中添加用户信息接口请求

在`checkAuth`函数中增加对用户信息接口的请求,以更新`currentUser`并优化登录权限检查逻辑。如果接口请求失败或返回401,则继续使用原有的本地存储判断逻辑。
/*
* @Date: 2025-03-20 20:36:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-26 08:04:21
* @LastEditTime: 2025-03-26 09:45:57
* @FilePath: /mlaj/src/router/guards.js
* @Description: 路由守卫逻辑
*/
import { getAuthInfoAPI } from '@/api/auth'
import { getUserInfoAPI } from '@/api/user'
import { wxInfo } from "@/utils/tools"
// 需要登录才能访问的路由
......@@ -52,7 +53,20 @@ export const checkWxAuth = async () => {
}
// 登录权限检查
export const checkAuth = (to) => {
export const checkAuth = async (to) => {
try {
// 先请求用户信息接口
const { code, data } = await getUserInfoAPI();
if (code) {
// 如果成功获取用户信息,更新currentUser并允许访问
localStorage.setItem('currentUser', JSON.stringify(data));
return true;
}
} catch (error) {
console.error('获取用户信息失败:', error);
}
// 如果接口请求失败或返回401,继续原有的本地存储判断逻辑
const currentUser = JSON.parse(localStorage.getItem('currentUser'))
// 检查当前路由是否需要认证
......