提交 | 用户 | 时间
|
820397
|
1 |
import { store } from '@/store' |
H |
2 |
import { defineStore } from 'pinia' |
|
3 |
import { getAccessToken, removeToken } from '@/utils/auth' |
271781
|
4 |
import { |
H |
5 |
CACHE_KEY, |
|
6 |
useCache, |
|
7 |
deleteUserCache, |
|
8 |
useSessionCache, |
|
9 |
deleteUserSessionCache |
|
10 |
} from '@/hooks/web/useCache' |
820397
|
11 |
import { getInfo, loginOut } from '@/api/login' |
H |
12 |
|
|
13 |
const { wsCache } = useCache() |
271781
|
14 |
const { wsSessionCache } = useSessionCache() |
820397
|
15 |
|
H |
16 |
interface UserVO { |
|
17 |
id: number |
|
18 |
avatar: string |
|
19 |
nickname: string |
|
20 |
deptId: number |
|
21 |
} |
|
22 |
|
|
23 |
interface UserInfoVO { |
|
24 |
// USER 缓存 |
|
25 |
permissions: string[] |
|
26 |
roles: string[] |
|
27 |
isSetUser: boolean |
|
28 |
user: UserVO |
|
29 |
} |
|
30 |
|
|
31 |
export const useUserStore = defineStore('admin-user', { |
|
32 |
state: (): UserInfoVO => ({ |
|
33 |
permissions: [], |
|
34 |
roles: [], |
|
35 |
isSetUser: false, |
|
36 |
user: { |
|
37 |
id: 0, |
|
38 |
avatar: '', |
|
39 |
nickname: '', |
|
40 |
deptId: 0 |
|
41 |
} |
|
42 |
}), |
|
43 |
getters: { |
|
44 |
getPermissions(): string[] { |
|
45 |
return this.permissions |
|
46 |
}, |
|
47 |
getRoles(): string[] { |
|
48 |
return this.roles |
|
49 |
}, |
|
50 |
getIsSetUser(): boolean { |
|
51 |
return this.isSetUser |
|
52 |
}, |
|
53 |
getUser(): UserVO { |
|
54 |
return this.user |
|
55 |
} |
|
56 |
}, |
|
57 |
actions: { |
|
58 |
async setUserInfoAction() { |
|
59 |
if (!getAccessToken()) { |
|
60 |
this.resetState() |
|
61 |
return null |
|
62 |
} |
|
63 |
let userInfo = wsCache.get(CACHE_KEY.USER) |
|
64 |
if (!userInfo) { |
|
65 |
userInfo = await getInfo() |
|
66 |
} |
|
67 |
this.permissions = userInfo.permissions |
|
68 |
this.roles = userInfo.roles |
|
69 |
this.user = userInfo.user |
|
70 |
this.isSetUser = true |
|
71 |
wsCache.set(CACHE_KEY.USER, userInfo) |
271781
|
72 |
if(!wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS)) { |
H |
73 |
wsSessionCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus) |
|
74 |
} |
820397
|
75 |
}, |
H |
76 |
async setUserAvatarAction(avatar: string) { |
|
77 |
const userInfo = wsCache.get(CACHE_KEY.USER) |
|
78 |
// NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null` |
|
79 |
this.user.avatar = avatar |
|
80 |
userInfo.user.avatar = avatar |
|
81 |
wsCache.set(CACHE_KEY.USER, userInfo) |
|
82 |
}, |
|
83 |
async setUserNicknameAction(nickname: string) { |
|
84 |
const userInfo = wsCache.get(CACHE_KEY.USER) |
|
85 |
// NOTE: 是否需要像`setUserInfoAction`一样判断`userInfo != null` |
|
86 |
this.user.nickname = nickname |
|
87 |
userInfo.user.nickname = nickname |
|
88 |
wsCache.set(CACHE_KEY.USER, userInfo) |
|
89 |
}, |
|
90 |
async loginOut() { |
|
91 |
await loginOut() |
|
92 |
removeToken() |
|
93 |
deleteUserCache() // 删除用户缓存 |
271781
|
94 |
deleteUserSessionCache() //删除路由缓存 |
820397
|
95 |
this.resetState() |
H |
96 |
}, |
|
97 |
resetState() { |
|
98 |
this.permissions = [] |
|
99 |
this.roles = [] |
|
100 |
this.isSetUser = false |
|
101 |
this.user = { |
|
102 |
id: 0, |
|
103 |
avatar: '', |
|
104 |
nickname: '', |
|
105 |
deptId: 0 |
|
106 |
} |
|
107 |
} |
|
108 |
} |
|
109 |
}) |
|
110 |
|
|
111 |
export const useUserStoreWithOut = () => { |
|
112 |
return useUserStore(store) |
|
113 |
} |