import { store } from '@/store'
|
import { defineStore } from 'pinia'
|
import { getAccessToken, removeToken } from '@/utils/auth'
|
import {
|
CACHE_KEY,
|
useCache,
|
deleteUserCache,
|
useSessionCache,
|
deleteUserSessionCache
|
} from '@/hooks/web/useCache'
|
import {getAppInfo, getInfo, loginOut} from '@/api/login'
|
import * as AppApi from '@/api/system/app'
|
|
const { wsCache } = useCache()
|
const { wsSessionCache } = useSessionCache()
|
|
interface UserVO {
|
id: number
|
avatar: string
|
nickname: string
|
deptId: number
|
}
|
|
interface UserInfoVO {
|
// USER 缓存
|
permissions: string[]
|
roles: string[]
|
isSetUser: boolean
|
user: UserVO
|
}
|
|
export const useUserStore = defineStore('admin-user', {
|
state: (): UserInfoVO => ({
|
permissions: [],
|
roles: [],
|
isSetUser: false,
|
user: {
|
id: 0,
|
avatar: '',
|
nickname: '',
|
deptId: 0
|
}
|
}),
|
getters: {
|
getPermissions(): string[] {
|
return this.permissions
|
},
|
getRoles(): string[] {
|
return this.roles
|
},
|
getIsSetUser(): boolean {
|
return this.isSetUser
|
},
|
getUser(): UserVO {
|
return this.user
|
}
|
},
|
actions: {
|
async setUserInfoAction() {
|
if (!getAccessToken()) {
|
this.resetState()
|
return null
|
}
|
const userInfo = await getInfo()
|
const appInfo = await getAppInfo()
|
this.permissions = userInfo.permissions
|
this.roles = userInfo.roles
|
this.user = userInfo.user
|
this.isSetUser = true
|
wsCache.set(CACHE_KEY.USER, userInfo)
|
//如果localStorage中有应用code,说明是从平台点击应用跳转过来
|
const appId = localStorage.getItem(import.meta.env.VITE_APP_CODE)
|
if(appId) {
|
const data = await AppApi.getAppMenuList(appId)
|
wsSessionCache.set(CACHE_KEY.ROLE_ROUTERS, data)
|
} else {
|
wsSessionCache.set(CACHE_KEY.ROLE_ROUTERS, appInfo.menus)
|
}
|
},
|
async setUserAvatarAction(avatar: string) {
|
const userInfo = wsCache.get(CACHE_KEY.USER)
|
// NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
|
this.user.avatar = avatar
|
userInfo.user.avatar = avatar
|
wsCache.set(CACHE_KEY.USER, userInfo)
|
},
|
async setUserNicknameAction(nickname: string) {
|
const userInfo = wsCache.get(CACHE_KEY.USER)
|
// NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null`
|
this.user.nickname = nickname
|
userInfo.user.nickname = nickname
|
wsCache.set(CACHE_KEY.USER, userInfo)
|
},
|
async loginOut() {
|
await loginOut()
|
removeToken()
|
deleteUserCache() // 删除用户缓存
|
deleteUserSessionCache()
|
localStorage.clear()
|
this.resetState()
|
},
|
resetState() {
|
this.permissions = []
|
this.roles = []
|
this.isSetUser = false
|
this.user = {
|
id: 0,
|
avatar: '',
|
nickname: '',
|
deptId: 0
|
}
|
}
|
}
|
})
|
|
export const useUserStoreWithOut = () => {
|
return useUserStore(store)
|
}
|