Vben
authored
|
1
|
import type { UserInfo } from '/#/store';
|
Vben
authored
|
2
|
import type { ErrorMessageMode } from '/#/axios';
|
Vben
authored
|
3
4
|
import { defineStore } from 'pinia';
import { store } from '/@/store';
|
|
5
|
import { RoleEnum } from '/@/enums/roleEnum';
|
Vben
authored
|
6
|
import { PageEnum } from '/@/enums/pageEnum';
|
Vben
authored
|
7
|
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
|
Vben
authored
|
8
|
import { getAuthCache, setAuthCache } from '/@/utils/auth';
|
|
9
|
import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel';
|
|
10
|
import { doLogout, getUserInfo, loginApi } from '/@/api/sys/user';
|
vben
authored
|
11
|
import { useI18n } from '/@/hooks/web/useI18n';
|
Vben
authored
|
12
|
import { useMessage } from '/@/hooks/web/useMessage';
|
Vben
authored
|
13
|
import { router } from '/@/router';
|
|
14
15
|
import { usePermissionStore } from '/@/store/modules/permission';
import { RouteRecordRaw } from 'vue-router';
|
|
16
|
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
|
|
17
|
|
Vben
authored
|
18
19
20
21
|
interface UserState {
userInfo: Nullable<UserInfo>;
token?: string;
roleList: RoleEnum[];
|
vben
authored
|
22
|
sessionTimeout?: boolean;
|
Vben
authored
|
23
|
}
|
vben
authored
|
24
|
|
Vben
authored
|
25
26
27
28
29
30
31
32
33
|
export const useUserStore = defineStore({
id: 'app-user',
state: (): UserState => ({
// user info
userInfo: null,
// token
token: undefined,
// roleList
roleList: [],
|
vben
authored
|
34
35
|
// Whether the login expired
sessionTimeout: false,
|
Vben
authored
|
36
37
|
}),
getters: {
|
vben
authored
|
38
|
getUserInfo(): UserInfo {
|
Vben
authored
|
39
40
|
return this.userInfo || getAuthCache<UserInfo>(USER_INFO_KEY) || {};
},
|
vben
authored
|
41
|
getToken(): string {
|
Vben
authored
|
42
43
|
return this.token || getAuthCache<string>(TOKEN_KEY);
},
|
vben
authored
|
44
|
getRoleList(): RoleEnum[] {
|
Vben
authored
|
45
46
|
return this.roleList.length > 0 ? this.roleList : getAuthCache<RoleEnum[]>(ROLES_KEY);
},
|
vben
authored
|
47
48
49
|
getSessionTimeout(): boolean {
return !!this.sessionTimeout;
},
|
Vben
authored
|
50
51
|
},
actions: {
|
vben
authored
|
52
|
setToken(info: string | undefined) {
|
Vben
authored
|
53
54
55
56
57
58
59
60
61
62
63
|
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
|
64
65
66
|
setSessionTimeout(flag: boolean) {
this.sessionTimeout = flag;
},
|
Vben
authored
|
67
68
69
70
|
resetState() {
this.userInfo = null;
this.token = '';
this.roleList = [];
|
vben
authored
|
71
|
this.sessionTimeout = false;
|
Vben
authored
|
72
73
74
75
76
77
78
79
80
|
},
/**
* @description: login
*/
async login(
params: LoginParams & {
goHome?: boolean;
mode?: ErrorMessageMode;
}
|
|
81
|
): Promise<GetUserInfoModel | null> {
|
Vben
authored
|
82
83
84
|
try {
const { goHome = true, mode, ...loginParams } = params;
const data = await loginApi(loginParams, mode);
|
|
85
|
const { token } = data;
|
Vben
authored
|
86
87
88
89
|
// save token
this.setToken(token);
// get user info
|
|
90
|
const userInfo = await this.getUserInfoAction();
|
Vben
authored
|
91
|
|
vben
authored
|
92
|
const sessionTimeout = this.sessionTimeout;
|
|
93
94
95
96
97
98
99
100
101
|
if (sessionTimeout) {
this.setSessionTimeout(false);
} else if (goHome) {
const permissionStore = usePermissionStore();
if (!permissionStore.isDynamicAddedRoute) {
const routes = await permissionStore.buildRoutesAction();
routes.forEach((route) => {
router.addRoute(route as unknown as RouteRecordRaw);
});
|
|
102
|
router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
|
|
103
104
105
106
|
permissionStore.setDynamicAddedRoute(true);
}
await router.replace(userInfo.homePath || PageEnum.BASE_HOME);
}
|
Vben
authored
|
107
108
|
return userInfo;
} catch (error) {
|
|
109
|
return Promise.reject(error);
|
Vben
authored
|
110
111
|
}
},
|
|
112
|
async getUserInfoAction(): Promise<UserInfo> {
|
|
113
|
const userInfo = await getUserInfo();
|
Vben
authored
|
114
115
116
117
|
const { roles } = userInfo;
const roleList = roles.map((item) => item.value) as RoleEnum[];
this.setUserInfo(userInfo);
this.setRoleList(roleList);
|
|
118
|
return userInfo;
|
Vben
authored
|
119
120
121
122
|
},
/**
* @description: logout
*/
|
|
123
124
125
126
127
128
129
130
|
async logout(goLogin = false) {
try {
await doLogout();
} catch {
console.log('注销Token失败');
}
this.setToken(undefined);
this.setSessionTimeout(false);
|
Vben
authored
|
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
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
|
vben
authored
|
153
|
export function useUserStoreWithOut() {
|
Vben
authored
|
154
|
return useUserStore(store);
|
|
155
|
}
|