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