提交 | 用户 | age
|
2f6253
|
1 |
import type { |
陈 |
2 |
LoginParams, |
|
3 |
GetUserInfoByUserIdModel, |
|
4 |
GetUserInfoByUserIdParams, |
|
5 |
} from '/@/api/sys/model/userModel'; |
|
6 |
|
|
7 |
import store from '/@/store/index'; |
|
8 |
import { VuexModule, Module, getModule, Mutation, Action } from 'vuex-module-decorators'; |
|
9 |
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper'; |
|
10 |
|
|
11 |
import { PageEnum } from '/@/enums/pageEnum'; |
|
12 |
import { RoleEnum } from '/@/enums/roleEnum'; |
26b610
|
13 |
import { CacheTypeEnum, ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum'; |
2f6253
|
14 |
|
陈 |
15 |
import { useMessage } from '/@/hooks/web/useMessage'; |
|
16 |
|
737b1b
|
17 |
import router from '/@/router'; |
2f6253
|
18 |
|
陈 |
19 |
import { loginApi, getUserInfoById } from '/@/api/sys/user'; |
|
20 |
|
8a9ca4
|
21 |
import { setLocal, getLocal, getSession, setSession } from '/@/utils/cache/persistent'; |
26b610
|
22 |
import { useProjectSetting } from '/@/hooks/setting'; |
190112
|
23 |
import { useI18n } from '/@/hooks/web/useI18n'; |
4ce1d5
|
24 |
import { ErrorMessageMode } from '/@/utils/http/axios/types'; |
2f6253
|
25 |
|
陈 |
26 |
export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>; |
|
27 |
|
|
28 |
const NAME = 'user'; |
|
29 |
hotModuleUnregisterModule(NAME); |
26b610
|
30 |
|
V |
31 |
const { permissionCacheType } = useProjectSetting(); |
190112
|
32 |
|
26b610
|
33 |
function getCache<T>(key: string) { |
V |
34 |
const fn = permissionCacheType === CacheTypeEnum.LOCAL ? getLocal : getSession; |
|
35 |
return fn(key) as T; |
|
36 |
} |
|
37 |
|
|
38 |
function setCache(USER_INFO_KEY: string, info: any) { |
|
39 |
if (!info) return; |
|
40 |
setLocal(USER_INFO_KEY, info, true); |
|
41 |
// TODO |
|
42 |
setSession(USER_INFO_KEY, info, true); |
|
43 |
} |
|
44 |
|
2f6253
|
45 |
@Module({ namespaced: true, name: NAME, dynamic: true, store }) |
陈 |
46 |
class User extends VuexModule { |
|
47 |
// user info |
|
48 |
private userInfoState: UserInfo | null = null; |
|
49 |
|
|
50 |
// token |
|
51 |
private tokenState = ''; |
|
52 |
|
|
53 |
// roleList |
|
54 |
private roleListState: RoleEnum[] = []; |
|
55 |
|
|
56 |
get getUserInfoState(): UserInfo { |
26b610
|
57 |
return this.userInfoState || getCache<UserInfo>(USER_INFO_KEY) || {}; |
2f6253
|
58 |
} |
陈 |
59 |
|
|
60 |
get getTokenState(): string { |
26b610
|
61 |
return this.tokenState || getCache<string>(TOKEN_KEY); |
2f6253
|
62 |
} |
陈 |
63 |
|
|
64 |
get getRoleListState(): RoleEnum[] { |
26b610
|
65 |
return this.roleListState.length > 0 ? this.roleListState : getCache<RoleEnum[]>(ROLES_KEY); |
2f6253
|
66 |
} |
陈 |
67 |
|
|
68 |
@Mutation |
737b1b
|
69 |
commitResetState(): void { |
2f6253
|
70 |
this.userInfoState = null; |
陈 |
71 |
this.tokenState = ''; |
|
72 |
this.roleListState = []; |
|
73 |
} |
|
74 |
|
|
75 |
@Mutation |
|
76 |
commitUserInfoState(info: UserInfo): void { |
|
77 |
this.userInfoState = info; |
26b610
|
78 |
setCache(USER_INFO_KEY, info); |
2f6253
|
79 |
} |
陈 |
80 |
|
|
81 |
@Mutation |
|
82 |
commitRoleListState(roleList: RoleEnum[]): void { |
|
83 |
this.roleListState = roleList; |
26b610
|
84 |
setCache(ROLES_KEY, roleList); |
2f6253
|
85 |
} |
陈 |
86 |
|
|
87 |
@Mutation |
|
88 |
commitTokenState(info: string): void { |
|
89 |
this.tokenState = info; |
26b610
|
90 |
setCache(TOKEN_KEY, info); |
2f6253
|
91 |
} |
陈 |
92 |
|
|
93 |
/** |
|
94 |
* @description: login |
|
95 |
*/ |
|
96 |
@Action |
4ce1d5
|
97 |
async login( |
V |
98 |
params: LoginParams & { |
|
99 |
goHome?: boolean; |
|
100 |
mode?: ErrorMessageMode; |
|
101 |
} |
|
102 |
): Promise<GetUserInfoByUserIdModel | null> { |
2f6253
|
103 |
try { |
4ce1d5
|
104 |
const { goHome = true, mode, ...loginParams } = params; |
V |
105 |
const data = await loginApi(loginParams, mode); |
|
106 |
|
2f6253
|
107 |
const { token, userId } = data; |
陈 |
108 |
|
|
109 |
// save token |
|
110 |
this.commitTokenState(token); |
|
111 |
|
116a1f
|
112 |
// get user info |
V |
113 |
const userInfo = await this.getUserInfoAction({ userId }); |
|
114 |
|
043403
|
115 |
goHome && (await router.replace(PageEnum.BASE_HOME)); |
2f6253
|
116 |
return userInfo; |
陈 |
117 |
} catch (error) { |
|
118 |
return null; |
|
119 |
} |
|
120 |
} |
|
121 |
|
|
122 |
@Action |
|
123 |
async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) { |
|
124 |
const userInfo = await getUserInfoById({ userId }); |
589409
|
125 |
const { roles } = userInfo; |
V |
126 |
const roleList = roles.map((item) => item.value) as RoleEnum[]; |
2f6253
|
127 |
this.commitUserInfoState(userInfo); |
陈 |
128 |
this.commitRoleListState(roleList); |
|
129 |
return userInfo; |
|
130 |
} |
|
131 |
|
|
132 |
/** |
da0491
|
133 |
* @description: logout |
2f6253
|
134 |
*/ |
陈 |
135 |
@Action |
da0491
|
136 |
async logout(goLogin = false) { |
2f6253
|
137 |
goLogin && router.push(PageEnum.BASE_LOGIN); |
陈 |
138 |
} |
|
139 |
|
|
140 |
/** |
|
141 |
* @description: Confirm before logging out |
|
142 |
*/ |
|
143 |
@Action |
|
144 |
async confirmLoginOut() { |
|
145 |
const { createConfirm } = useMessage(); |
962f90
|
146 |
const { t } = useI18n(); |
2f6253
|
147 |
createConfirm({ |
陈 |
148 |
iconType: 'warning', |
da0491
|
149 |
title: t('sys.app.logoutTip'), |
V |
150 |
content: t('sys.app.logoutMessage'), |
2f6253
|
151 |
onOk: async () => { |
da0491
|
152 |
await this.logout(true); |
2f6253
|
153 |
}, |
陈 |
154 |
}); |
|
155 |
} |
|
156 |
} |
|
157 |
export const userStore = getModule<User>(User); |