55 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-07-18 19:06:37 +08:00
import { store } from '../index'
import { defineStore } from 'pinia'
import { getAccessToken } from '@/utils/auth'
import { useCache } from '@/hooks/web/useCache'
2022-07-19 22:33:54 +08:00
const { wsCache } = useCache()
2022-07-18 19:06:37 +08:00
interface UserInfoVO {
permissions: []
roles: []
user: {
avatar: string
id: number
nickname: string
}
}
export const useUserStore = defineStore({
id: 'admin-user',
state: (): UserInfoVO => ({
permissions: [],
roles: [],
user: {
id: 0,
avatar: '',
nickname: ''
}
}),
actions: {
2022-07-19 22:33:54 +08:00
async getUserInfoAction(userInfo: UserInfoVO) {
2022-07-18 19:06:37 +08:00
if (!getAccessToken()) {
this.resetState()
return null
}
2022-07-19 22:33:54 +08:00
this.permissions = userInfo.permissions
this.roles = userInfo.roles
this.user = userInfo.user
wsCache.set('user', userInfo)
2022-07-18 19:06:37 +08:00
},
resetState() {
this.permissions = []
this.roles = []
this.user = {
id: 0,
avatar: '',
nickname: ''
}
}
}
})
export const useUserStoreWithOut = () => {
return useUserStore(store)
}