71 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-07-18 19:06:37 +08:00
import { store } from '../index'
import { defineStore } from 'pinia'
2022-08-03 12:38:58 +08:00
import { getAccessToken, removeToken } from '@/utils/auth'
2022-07-18 19:06:37 +08:00
import { useCache } from '@/hooks/web/useCache'
2022-07-19 22:33:54 +08:00
const { wsCache } = useCache()
2022-08-03 12:38:58 +08:00
interface UserVO {
id: number
avatar: string
nickname: string
}
2022-07-18 19:06:37 +08:00
interface UserInfoVO {
2022-08-03 12:38:58 +08:00
permissions: string[]
roles: string[]
user: UserVO
2022-07-18 19:06:37 +08:00
}
2022-10-08 18:51:50 +08:00
export const useUserStore = defineStore('admin-user', {
2022-07-18 19:06:37 +08:00
state: (): UserInfoVO => ({
permissions: [],
roles: [],
user: {
id: 0,
avatar: '',
nickname: ''
}
}),
2022-08-03 12:38:58 +08:00
getters: {
getPermissions(): string[] {
return this.permissions
},
getRoles(): string[] {
return this.roles
},
getUser(): UserVO {
return this.user
}
},
2022-07-18 19:06:37 +08:00
actions: {
2022-08-03 13:00:42 +08:00
async setUserInfoAction(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
},
2022-08-03 12:38:58 +08:00
loginOut() {
removeToken()
wsCache.clear()
this.resetState()
},
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)
}