AI_Painting_V2.0/src/stores/user.js
WangLeo 5da5496492 重构 API 层架构:统一 HTTP 请求、新增算力调度后端路由
- 请求拦截器统一 Auth 头不带 Bearer 前缀,新增 /suanli 前缀路由到算力调度后端
- 任务创建/轮询/历史接口迁移至 apis/display,改为 axios 调用替代原始 fetch
- 模型 API 分离为两层:apis 纯 HTTP 调用 + utils 缓存业务逻辑
- 新增历史任务列表接口 requestTaskHistory(支持 user_id/platform_code 筛选和分页)
- 响应拦截器兼容 status/code 双字段,用户信息兼容新旧 data 格式
- 移除免费次数(freeTimes)体系
- 更新 CLAUDE.md 文档
2026-06-02 18:05:55 +08:00

142 lines
3.5 KiB
JavaScript

import {
accountLogin as accountLoginApi,
checkUsertoken as checkUsertokenApi,
getUserInfo as getUserInfoApi,
logout as logoutApi
} from '@/apis/auth'
import { clearToken, getToken, setToken } from '@/utils/auth'
const storeSetup = () => {
const userInfo = reactive({
id: '',
username: '',
nickname: '',
gender: 0,
email: '',
phone: '',
avatar: '',
pwdResetTime: '',
pwdExpired: false,
registrationDate: '',
deptName: '',
roles: [],
permissions: [],
routers: []
})
const name = computed(() => userInfo.nickname)
const username = computed(() => userInfo.username)
const token = ref(getToken() || '')
const pwdExpiredShow = ref(true)
const roles = ref([]) // 当前用户角色
const permissions = ref([]) // 当前角色权限标识集合
const dept = ref({}) // 当前用户所在部门集合
const isLogin = ref(false)
// 重置token
const resetToken = () => {
token.value = ''
clearToken()
}
// 检查token有效性
const checkTokenValid = async () => {
const res = await checkUsertokenApi()
console.log('checkTokenValid:', res)
if (res.code === '401' || res.status === '401' || res.success === false) {
console.error('Token is invalid:', res.message)
return false
}
console.log('Token is valid')
return true
}
// 获取用户信息
const getInfo = async () => {
const res = await getUserInfoApi()
// 兼容新旧格式:新格式 data.userInfo 嵌套,旧格式 data 扁平
const u = res.data.userInfo || res.data
Object.assign(userInfo, u)
userInfo.id = u.userId || u.id
userInfo.username = u.userName || u.username
if (typeof u.routers === 'string' && u.routers.trim() !== '') {
userInfo.routers = u.routers.split(',').map((item) => item.trim())
} else {
userInfo.routers = []
}
// 角色和权限在 data 层级(非 userInfo 内)
const roleList = res.data.roles || u.roles
if (roleList?.length) {
roles.value = roleList
permissions.value = res.data.permissions || u.permissions || []
}
}
// 登录
const accountLogin = async (req) => {
const res = await accountLoginApi(req)
if (res.data == null || res.code === '500' || res.status === 500 || res.success === false) {
// eslint-disable-next-line no-undef
ElMessage({
title: '提示',
message: res.msg || '操作失败,请稍后重试'
})
isLogin.value = false
return false
}
setToken(res.data.token) // res.data.generateToken
token.value = res.data.token
getInfo()
// isLogin.value = true
return true
}
// 退出登录回调
const logoutCallBack = async () => {
roles.value = []
permissions.value = []
pwdExpiredShow.value = true
isLogin.value = false
resetToken()
}
// 退出登录
const logout = async () => {
try {
await logoutApi()
await logoutCallBack()
return true
} catch (error) {
console.error('Logout failed:', error.message) // 处理错误
return false
}
}
return {
userInfo,
name,
token,
roles,
permissions,
pwdExpiredShow,
dept,
username,
isLogin,
accountLogin,
logout,
logoutCallBack,
getInfo,
resetToken,
checkTokenValid
}
}
// eslint-disable-next-line no-undef
export const useUserStore = defineStore('user', storeSetup, {
persist: {
paths: ['token', 'roles', 'permissions', 'pwdExpiredShow', 'username'],
storage: localStorage
}
})