sanmu
authored
2 years ago
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import type { UserInfo } from '/#/store';
import type { ErrorMessageMode } from '/#/axios';
import { defineStore } from 'pinia';
import { store } from '/@/store';
import { RoleEnum } from '/@/enums/roleEnum';
import { PageEnum } from '/@/enums/pageEnum';
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel';
import { doLogout, getUserInfo, loginApi, getImageCaptcha } from '/@/api/sys/user';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
import { usePermissionStore } from '/@/store/modules/permission';
import { RouteRecordRaw } from 'vue-router';
import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
import { h } from 'vue';
import { getInitDictData } from '/@/api/project/order';
import { groupBy } from 'lodash-es';
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
interface QueryVO {
checkEndTime?: string;
checkNo?: string;
checkNoStatus?: number;
checkStartTime?: string;
createEndTime?: string;
createStartTime?: string;
customerCode?: string[];
innerNo?: string[];
invoiceEndTime?: string;
invoiceNo?: string;
invoiceStartTime?: string;
invoiceStatus?: number;
orderHodEndTime?: string;
orderHodStartTime?: string;
page: number;
pageSize: number;
productionDepartment?: string[];
productionDepartmentConsignEndTime?: string;
productionDepartmentConsignStartTime?: string;
projectNo?: string[];
}
sanmu
authored
2 years ago
44
45
46
47
48
49
interface UserState {
userInfo: Nullable<UserInfo>;
token?: string;
roleList: RoleEnum[];
sessionTimeout?: boolean;
lastUpdateTime: number;
50
queryVO: QueryVO[];
sanmu
authored
2 years ago
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
}
export const useOrderStore = defineStore({
id: 'app-order',
state: (): UserState => ({
// user info
dicts: {},
// token
total: 0,
// roleList
roleList: [],
// Whether the login expired
sessionTimeout: false,
// Last fetch time
lastUpdateTime: 0,
66
queryVO: [],
sanmu
authored
2 years ago
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
}),
getters: {
getDictInfo(state): UserInfo {
return state.dicts;
},
getTotal(state) {
return state.total;
},
getToken(state): string {
return state.token || getAuthCache<string>(TOKEN_KEY);
},
getRoleList(state): RoleEnum[] {
return state.roleList.length > 0 ? state.roleList : getAuthCache<RoleEnum[]>(ROLES_KEY);
},
getSessionTimeout(state): boolean {
return !!state.sessionTimeout;
},
getLastUpdateTime(state): number {
return state.lastUpdateTime;
},
87
88
89
getQueryVO(state) {
return state.queryVO; // 新增 getter 返回 queryVO
},
sanmu
authored
2 years ago
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
},
actions: {
setToken(info: string | undefined) {
this.token = info ? info : ''; // for null or undefined value
setAuthCache(TOKEN_KEY, info);
},
setTotal(total) {
this.total = total;
},
setRoleList(roleList: RoleEnum[]) {
this.roleList = roleList;
setAuthCache(ROLES_KEY, roleList);
},
setUserInfo(info: UserInfo | null) {
this.userInfo = info;
this.lastUpdateTime = new Date().getTime();
setAuthCache(USER_INFO_KEY, info);
},
setSessionTimeout(flag: boolean) {
this.sessionTimeout = flag;
},
111
112
113
114
115
setQueryVO(params: QueryVO[]) {
params.page = undefined;
params.pageSize = undefined;
this.queryVO = params;
},
sanmu
authored
2 years ago
116
117
118
119
120
121
122
123
124
resetState() {
this.userInfo = null;
this.token = '';
this.roleList = [];
this.sessionTimeout = false;
},
/**
* @description: login
*/
sanmu
authored
2 years ago
125
async getDict() {
sanmu
authored
2 years ago
126
127
try {
const data = await getInitDictData();
sanmu
authored
2 years ago
128
129
130
131
132
133
134
135
const newData = groupBy(data, 'dictCode');
// 遍历对象的每个键
Object.keys(newData).forEach((key) => {
// 对每个数组按照字典序排序
newData[key].sort((x, y) => {
return x.dictValue.localeCompare(y.dictValue);
});
});
sanmu
authored
2 years ago
136
sanmu
authored
2 years ago
137
this.dicts = newData;
sanmu
authored
2 years ago
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
} catch (error) {
return Promise.reject(error);
}
},
async afterLoginAction(userInfo): Promise<GetUserInfoModel | null> {
if (!this.getToken) return null;
// get user info
// const userInfo = await this.getUserInfoAction();
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);
}
await router.replace('home');
}
return userInfo;
},
async getUserInfoAction(): Promise<UserInfo | null> {
// 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 {};
},
/**
* @description: logout
*/
async logout(goLogin = false) {
if (this.getToken) {
try {
await doLogout();
} catch {
console.log('注销Token失败');
}
}
this.setToken(undefined);
this.setSessionTimeout(false);
this.setUserInfo(null);
goLogin && router.push(PageEnum.BASE_LOGIN);
},
/**
* @description: Confirm before logging out
*/
confirmLoginOut() {
const { createConfirm } = useMessage();
const { t } = useI18n();
createConfirm({
iconType: 'warning',
title: () => h('span', t('sys.app.logoutTip')),
content: () => h('span', t('sys.app.logoutMessage')),
onOk: async () => {
await this.logout(true);
},
});
},
/**
* 获取图片验证码
*/
async getImageCaptcha() {
return await getImageCaptcha();
},
},
});
// Need to be used outside the setup
export function useOrderStoreWithOut() {
return useOrderStore(store);
}