user.ts
1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { store } from '../index'
import { defineStore } from 'pinia'
import { getAccessToken, removeToken } from '@/utils/auth'
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
interface UserVO {
id: number
avatar: string
nickname: string
}
interface UserInfoVO {
permissions: string[]
roles: string[]
user: UserVO
}
export const useUserStore = defineStore('admin-user', {
state: (): UserInfoVO => ({
permissions: [],
roles: [],
user: {
id: 0,
avatar: '',
nickname: ''
}
}),
getters: {
getPermissions(): string[] {
return this.permissions
},
getRoles(): string[] {
return this.roles
},
getUser(): UserVO {
return this.user
}
},
actions: {
async setUserInfoAction(userInfo: UserInfoVO) {
if (!getAccessToken()) {
this.resetState()
return null
}
this.permissions = userInfo.permissions
this.roles = userInfo.roles
this.user = userInfo.user
wsCache.set('user', userInfo)
},
loginOut() {
removeToken()
wsCache.clear()
this.resetState()
},
resetState() {
this.permissions = []
this.roles = []
this.user = {
id: 0,
avatar: '',
nickname: ''
}
}
}
})
export const useUserStoreWithOut = () => {
return useUserStore(store)
}