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