Vben
authored
|
1
2
|
import type { UserInfo } from '/#/store';
import type { ErrorMessageMode } from '/@/utils/http/axios/types';
|
|
3
|
|
Vben
authored
|
4
5
|
import { defineStore } from 'pinia';
import { store } from '/@/store';
|
|
6
7
|
import { RoleEnum } from '/@/enums/roleEnum';
|
Vben
authored
|
8
|
import { PageEnum } from '/@/enums/pageEnum';
|
Vben
authored
|
9
|
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
|
|
10
|
|
Vben
authored
|
11
12
13
14
15
16
|
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import {
GetUserInfoByUserIdModel,
GetUserInfoByUserIdParams,
LoginParams,
} from '/@/api/sys/model/userModel';
|
|
17
|
|
Vben
authored
|
18
|
import { getUserInfoById, loginApi } from '/@/api/sys/user';
|
|
19
|
|
vben
authored
|
20
|
import { useI18n } from '/@/hooks/web/useI18n';
|
Vben
authored
|
21
22
|
import { useMessage } from '/@/hooks/web/useMessage';
import router from '/@/router';
|
|
23
|
|
Vben
authored
|
24
25
26
27
|
interface UserState {
userInfo: Nullable<UserInfo>;
token?: string;
roleList: RoleEnum[];
|
vben
authored
|
28
|
sessionTimeout?: boolean;
|
Vben
authored
|
29
|
}
|
vben
authored
|
30
|
|
Vben
authored
|
31
32
33
34
35
36
37
38
39
|
export const useUserStore = defineStore({
id: 'app-user',
state: (): UserState => ({
// user info
userInfo: null,
// token
token: undefined,
// roleList
roleList: [],
|
vben
authored
|
40
41
|
// Whether the login expired
sessionTimeout: false,
|
Vben
authored
|
42
43
|
}),
getters: {
|
vben
authored
|
44
|
getUserInfo(): UserInfo {
|
Vben
authored
|
45
46
|
return this.userInfo || getAuthCache<UserInfo>(USER_INFO_KEY) || {};
},
|
vben
authored
|
47
|
getToken(): string {
|
Vben
authored
|
48
49
|
return this.token || getAuthCache<string>(TOKEN_KEY);
},
|
vben
authored
|
50
|
getRoleList(): RoleEnum[] {
|
Vben
authored
|
51
52
|
return this.roleList.length > 0 ? this.roleList : getAuthCache<RoleEnum[]>(ROLES_KEY);
},
|
vben
authored
|
53
54
55
|
getSessionTimeout(): boolean {
return !!this.sessionTimeout;
},
|
Vben
authored
|
56
57
|
},
actions: {
|
vben
authored
|
58
|
setToken(info: string | undefined) {
|
Vben
authored
|
59
60
61
62
63
64
65
66
67
68
69
|
this.token = info;
setAuthCache(TOKEN_KEY, info);
},
setRoleList(roleList: RoleEnum[]) {
this.roleList = roleList;
setAuthCache(ROLES_KEY, roleList);
},
setUserInfo(info: UserInfo) {
this.userInfo = info;
setAuthCache(USER_INFO_KEY, info);
},
|
vben
authored
|
70
71
72
|
setSessionTimeout(flag: boolean) {
this.sessionTimeout = flag;
},
|
Vben
authored
|
73
74
75
76
|
resetState() {
this.userInfo = null;
this.token = '';
this.roleList = [];
|
vben
authored
|
77
|
this.sessionTimeout = false;
|
Vben
authored
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
},
/**
* @description: login
*/
async login(
params: LoginParams & {
goHome?: boolean;
mode?: ErrorMessageMode;
}
): Promise<GetUserInfoByUserIdModel | null> {
try {
const { goHome = true, mode, ...loginParams } = params;
const data = await loginApi(loginParams, mode);
const { token, userId } = data;
// save token
this.setToken(token);
// get user info
const userInfo = await this.getUserInfoAction({ userId });
|
vben
authored
|
98
99
100
|
const sessionTimeout = this.sessionTimeout;
sessionTimeout && this.setSessionTimeout(false);
!sessionTimeout && goHome && (await router.replace(PageEnum.BASE_HOME));
|
Vben
authored
|
101
102
103
104
105
106
107
108
109
110
111
|
return userInfo;
} catch (error) {
return null;
}
},
async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) {
const userInfo = await getUserInfoById({ userId });
const { roles } = userInfo;
const roleList = roles.map((item) => item.value) as RoleEnum[];
this.setUserInfo(userInfo);
this.setRoleList(roleList);
|
|
112
|
return userInfo;
|
Vben
authored
|
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
},
/**
* @description: logout
*/
logout(goLogin = false) {
goLogin && router.push(PageEnum.BASE_LOGIN);
},
/**
* @description: Confirm before logging out
*/
confirmLoginOut() {
const { createConfirm } = useMessage();
const { t } = useI18n();
createConfirm({
iconType: 'warning',
title: t('sys.app.logoutTip'),
content: t('sys.app.logoutMessage'),
onOk: async () => {
await this.logout(true);
},
});
},
},
});
// Need to be used outside the setup
export function useUserStoreWidthOut() {
return useUserStore(store);
|
|
142
|
}
|