feat(auth): 实现用户认证状态持久化及权限控制优化

- 新增 pinia-plugin-persistedstate 实现用户状态本地存储
- 重构 API 响应类型为统一模块管理
- 优化路由守卫逻辑,支持 token 自动登录
- 修复权限检查逻辑错误,调整 AI 聊天页访问权限
- 新增用户信息获取接口及类型定义
This commit is contained in:
2026-01-07 20:14:40 +08:00
parent 8e5558d9a2
commit fad13e00e5
13 changed files with 2810 additions and 21 deletions

View File

@@ -1,6 +1,7 @@
import { defineStore } from "pinia";
import ACCESS_ENUM from "../access/accessEnum";
import type { LoginUesr } from "../store/types";
import { getUserInfoByToken } from "@/api/auth/user";
/**
*
*/
@@ -11,12 +12,20 @@ export const useUserStore = defineStore("user", {
userRole: ACCESS_ENUM.NOT_LOGIN,
} as LoginUesr,
}),
persist: {
key: "user-store",
storage: localStorage,
pick: ["loginUser"], // 只持久化 loginUser
},
actions: {
// 获取登录用户
async getLoginUser() {
try {
// 从后端获取当前登录用户信息
const res = await getUserInfoByToken();
console.log("获取登录用户成功", res);
if(res.data.success === true){
this.updateUserLoginStatus(res.data.data);
}
}catch(e) {
console.error("获取登录用户失败", e);
// 网络错误情况也视为未登录