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';
|
sanmu
authored
|
10
|
import { doLogout, getUserInfo, loginApi, getImageCaptcha } 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
|
import { isArray } from '/@/utils/is';
|
|
18
|
import { h } from 'vue';
|
|
19
|
|
Vben
authored
|
20
21
22
23
|
interface UserState {
userInfo: Nullable<UserInfo>;
token?: string;
roleList: RoleEnum[];
|
vben
authored
|
24
|
sessionTimeout?: boolean;
|
|
25
|
lastUpdateTime: number;
|
Vben
authored
|
26
|
}
|
vben
authored
|
27
|
|
Vben
authored
|
28
29
30
31
32
33
34
35
36
|
export const useUserStore = defineStore({
id: 'app-user',
state: (): UserState => ({
// user info
userInfo: null,
// token
token: undefined,
// roleList
roleList: [],
|
vben
authored
|
37
38
|
// Whether the login expired
sessionTimeout: false,
|
|
39
40
|
// Last fetch time
lastUpdateTime: 0,
|
Vben
authored
|
41
42
|
}),
getters: {
|
|
43
44
|
getUserInfo(state): UserInfo {
return state.userInfo || getAuthCache<UserInfo>(USER_INFO_KEY) || {};
|
Vben
authored
|
45
|
},
|
|
46
47
|
getToken(state): string {
return state.token || getAuthCache<string>(TOKEN_KEY);
|
Vben
authored
|
48
|
},
|
|
49
50
|
getRoleList(state): RoleEnum[] {
return state.roleList.length > 0 ? state.roleList : getAuthCache<RoleEnum[]>(ROLES_KEY);
|
Vben
authored
|
51
|
},
|
|
52
53
|
getSessionTimeout(state): boolean {
return !!state.sessionTimeout;
|
vben
authored
|
54
|
},
|
|
55
56
|
getLastUpdateTime(state): number {
return state.lastUpdateTime;
|
|
57
|
},
|
Vben
authored
|
58
59
|
},
actions: {
|
vben
authored
|
60
|
setToken(info: string | undefined) {
|
|
61
|
this.token = info ? info : ''; // for null or undefined value
|
Vben
authored
|
62
63
64
65
66
67
|
setAuthCache(TOKEN_KEY, info);
},
setRoleList(roleList: RoleEnum[]) {
this.roleList = roleList;
setAuthCache(ROLES_KEY, roleList);
},
|
|
68
|
setUserInfo(info: UserInfo | null) {
|
Vben
authored
|
69
|
this.userInfo = info;
|
|
70
|
this.lastUpdateTime = new Date().getTime();
|
Vben
authored
|
71
72
|
setAuthCache(USER_INFO_KEY, info);
},
|
vben
authored
|
73
74
75
|
setSessionTimeout(flag: boolean) {
this.sessionTimeout = flag;
},
|
Vben
authored
|
76
77
78
79
|
resetState() {
this.userInfo = null;
this.token = '';
this.roleList = [];
|
vben
authored
|
80
|
this.sessionTimeout = false;
|
Vben
authored
|
81
82
83
84
85
86
87
88
|
},
/**
* @description: login
*/
async login(
params: LoginParams & {
goHome?: boolean;
mode?: ErrorMessageMode;
|
vben
authored
|
89
|
},
|
|
90
|
): Promise<GetUserInfoModel | null> {
|
Vben
authored
|
91
|
try {
|
sanmu
authored
|
92
|
const { mode, ...loginParams } = params;
|
Vben
authored
|
93
|
const data = await loginApi(loginParams, mode);
|
sanmu
authored
|
94
|
const { token, user } = data;
|
Vben
authored
|
95
96
|
this.setToken(token);
|
sanmu
authored
|
97
98
|
this.setUserInfo(user);
|
sanmu
authored
|
99
100
101
102
103
|
// this.setToken(
// 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImF1dGgiOiJkZXB0OmVkaXQsdXNlcjpsaXN0LHJvbGVzOmFkZCx1c2VyOmFkZCxkZXB0OmFkZCxtZW51OmRlbCxyb2xlczpkZWwsbWVudTplZGl0LG1lbnU6bGlzdCxzdG9yYWdlOmxpc3Qsam9iOmVkaXQscm9sZXM6bGlzdCx1c2VyOmRlbCxkaWN0OmFkZCxkZXB0Omxpc3QsbWVudTphZGQsam9iOmRlbCxqb2I6bGlzdCx1c2VyOmVkaXQscm9sZXM6ZWRpdCxkaWN0OmRlbCxqb2I6YWRkLGRpY3Q6ZWRpdCxkZXB0OmRlbCIsImV4cCI6MTY5NjE0NTUzN30.Q9hcwnG9uJP3HHjwpd_3ZrV7LrZlMIL6JJBe0rcfqldE8SPq8JkBKElrGeu7GoCAc5EyN6wUUYcriwV5VhpZqQ',
// );
// 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ4bXMiLCJhdXRoIjoiZGVwdDplZGl0LHVzZXI6bGlzdCxyb2xlczphZGQsdXNlcjphZGQsZGVwdDphZGQsbWVudTpkZWwscm9sZXM6ZGVsLG1lbnU6ZWRpdCxtZW51Omxpc3Qsc3RvcmFnZTpsaXN0LGpvYjplZGl0LHJvbGVzOmxpc3QsdXNlcjpkZWwsZGljdDphZGQsZGVwdDpsaXN0LG1lbnU6YWRkLGpvYjpkZWwsam9iOmxpc3QsdXNlcjplZGl0LHJvbGVzOmVkaXQsZGljdDpkZWwsam9iOmFkZCxkaWN0OmVkaXQsZGVwdDpkZWwiLCJleHAiOjE2OTUxNDIwNDF9.Wj-CyJGbMps_9a84TQkJgBSNnj22OVAEVLsEKvVHdyxkQORIwvxpOzT7gr2l-8VbUJ3wLatG0OUe-6smoaoTbA',
return this.afterLoginAction(user);
|
Vben
authored
|
104
|
} catch (error) {
|
|
105
|
return Promise.reject(error);
|
Vben
authored
|
106
107
|
}
},
|
sanmu
authored
|
108
|
async afterLoginAction(userInfo): Promise<GetUserInfoModel | null> {
|
|
109
110
|
if (!this.getToken) return null;
// get user info
|
sanmu
authored
|
111
|
// const userInfo = await this.getUserInfoAction();
|
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
const sessionTimeout = this.sessionTimeout;
if (sessionTimeout) {
this.setSessionTimeout(false);
} else {
const permissionStore = usePermissionStore();
if (!permissionStore.isDynamicAddedRoute) {
const routes = await permissionStore.buildRoutesAction();
routes.forEach((route) => {
router.addRoute(route as unknown as RouteRecordRaw);
});
router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);
permissionStore.setDynamicAddedRoute(true);
}
|
sanmu
authored
|
126
|
await router.replace('order');
|
|
127
128
129
|
}
return userInfo;
},
|
|
130
|
async getUserInfoAction(): Promise<UserInfo | null> {
|
sanmu
authored
|
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// if (!this.getToken) return null;
// const userInfo = await getUserInfo();
// const { roles = [] } = userInfo;
// if (isArray(roles)) {
// const roleList = roles.map((item) => item.value) as RoleEnum[];
// this.setRoleList(roleList);
// } else {
// userInfo.roles = [];
// this.setRoleList([]);
// }
// this.setUserInfo(userInfo);
// return userInfo;
return {};
|
Vben
authored
|
144
145
146
147
|
},
/**
* @description: logout
*/
|
|
148
|
async logout(goLogin = false) {
|
sanmu
authored
|
149
150
151
152
153
154
155
|
// if (this.getToken) {
// try {
// await doLogout();
// } catch {
// console.log('注销Token失败');
// }
// }
|
|
156
157
|
this.setToken(undefined);
this.setSessionTimeout(false);
|
|
158
|
this.setUserInfo(null);
|
sanmu
authored
|
159
|
router.push(PageEnum.BASE_LOGIN);
|
Vben
authored
|
160
161
162
163
164
165
166
167
168
169
|
},
/**
* @description: Confirm before logging out
*/
confirmLoginOut() {
const { createConfirm } = useMessage();
const { t } = useI18n();
createConfirm({
iconType: 'warning',
|
|
170
171
|
title: () => h('span', t('sys.app.logoutTip')),
content: () => h('span', t('sys.app.logoutMessage')),
|
Vben
authored
|
172
173
174
175
176
|
onOk: async () => {
await this.logout(true);
},
});
},
|
sanmu
authored
|
177
178
179
180
181
182
183
|
/**
* 获取图片验证码
*/
async getImageCaptcha() {
return await getImageCaptcha();
},
|
Vben
authored
|
184
185
186
187
|
},
});
// Need to be used outside the setup
|
vben
authored
|
188
|
export function useUserStoreWithOut() {
|
Vben
authored
|
189
|
return useUserStore(store);
|
|
190
|
}
|