@utogether/udp-core 1.0.1-beta.1 → 1.0.1-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/build/plugins.ts +39 -32
  2. package/dist/{403-Dp617CWX.js → 403-Dsgsr2M-.js} +1 -1
  3. package/dist/{404-Cz_Axb6Y.js → 404-DmPHEfqM.js} +1 -1
  4. package/dist/{500-BGCtRNse.js → 500-OYEgHR2a.js} +1 -1
  5. package/dist/{AuthorityInfo-DGGfm7IS.js → AuthorityInfo-B1H1txG-.js} +1 -1
  6. package/dist/AuthorityInfo.vue_vue_type_style_index_0_lang-BUlmQtjQ.js +100 -0
  7. package/dist/{Company-IV3GTnzY.js → Company-C8ojgx93.js} +3 -3
  8. package/dist/{CompanyPanel-qV-_VtoL.js → CompanyPanel-Dageer1t.js} +16 -16
  9. package/dist/{Department-B3W-OxW8.js → Department-Dr94_85I.js} +3 -3
  10. package/dist/{DepartmentPanel-Cw3OWxE7.js → DepartmentPanel-Bc5r1HpT.js} +1 -1
  11. package/dist/{DesignPanel-BFxR2fHJ.js → DesignPanel-CcWt1Myp.js} +1 -1
  12. package/dist/{DesignPanel.vue_vue_type_style_index_0_lang-DljbeFba.js → DesignPanel.vue_vue_type_style_index_0_lang-CKA6nx1l.js} +24 -26
  13. package/dist/DictView-BHj6wexC.js +110 -0
  14. package/dist/InvOrganization-DP3Le9g-.js +72 -0
  15. package/dist/Org-BRGHTwsw.js +39 -0
  16. package/dist/{Preview-BlDMmpdR.js → Preview-CqMaUD_3.js} +1 -1
  17. package/dist/{ReportDefine-Cub_85LA.js → ReportDefine-PEQdTCOY.js} +1 -1
  18. package/dist/{ReportDesign-hFhq5UVE.js → ReportDesign-CB4bv8ba.js} +43 -43
  19. package/dist/{ReportQuery-ChkWEyxT.js → ReportQuery-B7t4lMK0.js} +1 -1
  20. package/dist/{ReportQueryFrom-KVyD_8Dj.js → ReportQueryFrom-VBxUIMRJ.js} +1 -1
  21. package/dist/{ReportQueryFrom.vue_vue_type_style_index_0_lang-CLNODquq.js → ReportQueryFrom.vue_vue_type_style_index_0_lang-HeLA4Qs5.js} +1 -1
  22. package/dist/{ReportTemplate-ag9NDvh2.js → ReportTemplate-DNkv7F5p.js} +28 -28
  23. package/dist/{Role-_q3lQ8CZ.js → Role-BGfTpZK4.js} +6 -6
  24. package/dist/{RoleAssign-DZb9IRsm.js → RoleAssign-DrKFWZzA.js} +8 -8
  25. package/dist/{RolePanel-CsLsz-Ds.js → RolePanel-3lvRh7aS.js} +1 -1
  26. package/dist/{RolePanel-BQb1LlhD.js → RolePanel-BRI5QYe4.js} +1 -1
  27. package/dist/RolePanel.vue_vue_type_script_setup_true_lang-BXKZb7yt.js +132 -0
  28. package/dist/{RolePanel.vue_vue_type_script_setup_true_lang-BrnRgHEk.js → RolePanel.vue_vue_type_script_setup_true_lang-Cci_Ysty.js} +44 -38
  29. package/dist/{ScrollPanel.vue_vue_type_style_index_0_lang-CaFKRwXu.js → ScrollPanel.vue_vue_type_style_index_0_lang-BmId6zzA.js} +21 -21
  30. package/dist/{Staff-BSf9Ypbk.js → Staff-Dr0dq3fW.js} +3 -3
  31. package/dist/{StaffInfo-BNKasyMF.js → StaffInfo-_N9g2hPa.js} +1 -1
  32. package/dist/{StaffInfo.vue_vue_type_script_setup_true_lang-DDZ7ukd0.js → StaffInfo.vue_vue_type_script_setup_true_lang-DiTJY2ug.js} +11 -11
  33. package/dist/{StaffPanel-Bpq0WVlH.js → StaffPanel-mxJQiW3G.js} +1 -1
  34. package/dist/{StaffPanel.vue_vue_type_script_setup_true_lang-BhiJ0Q-Q.js → StaffPanel.vue_vue_type_script_setup_true_lang-GRGn-5t6.js} +2 -2
  35. package/dist/{SysUser-FAABuNti.js → SysUser-DgA19hZE.js} +2 -2
  36. package/dist/{SysUserPanel-PxJeOgHm.js → SysUserPanel-Bc85z5_K.js} +1 -1
  37. package/dist/SysUserPanel.vue_vue_type_script_setup_true_lang-BHDrP1Pk.js +294 -0
  38. package/dist/{SystemMenu-C-7NAGon.js → SystemMenu-Be-gFIie.js} +26 -26
  39. package/dist/{UserInfo-ClXKtyGo.js → UserInfo-BCYuwFmr.js} +1 -1
  40. package/dist/{UserInfo.vue_vue_type_style_index_0_lang-8N7P4Hl7.js → UserInfo.vue_vue_type_style_index_0_lang-B1lgokO6.js} +39 -37
  41. package/dist/{childView-C_HmDQNd.js → childView-DhcBr1UA.js} +1 -1
  42. package/dist/{childView-uUlBcTza.js → childView-Dk4zZoXd.js} +1 -1
  43. package/dist/{childView.vue_vue_type_style_index_0_lang-Ckjmw6wJ.js → childView.vue_vue_type_style_index_0_lang-CVnLnl03.js} +40 -40
  44. package/dist/{childView.vue_vue_type_style_index_0_lang-y0sDvYx5.js → childView.vue_vue_type_style_index_0_lang-D4GYVhOc.js} +1 -1
  45. package/dist/{code-rule-AgCVDKFy.js → code-rule-BSwPwuAL.js} +9 -10
  46. package/dist/core.es.js +14 -11
  47. package/dist/{cron-task-C6FgQxTi.js → cron-task-T1ztyra7.js} +1 -1
  48. package/dist/{frameView-BDgISK7N.js → frameView-BDgngiBq.js} +1 -1
  49. package/dist/img/l_img.svg +1 -1
  50. package/dist/img/minicolors.png +0 -0
  51. package/dist/img/v_img.svg +1 -1
  52. package/dist/index-BSj2AtVL.js +4468 -0
  53. package/dist/{layoutView-yb3DV2DQ.js → layoutView-i7dLLBzJ.js} +1181 -1176
  54. package/dist/{login-RRpljbkm.js → login-K5Ludp8x.js} +105 -113
  55. package/dist/{login-log-CvVnyGi3.js → login-log-kqKzKTto.js} +1 -1
  56. package/dist/{lov-view-C9-rjzZR.js → lov-view-lAexiucu.js} +2 -2
  57. package/dist/{menuInfo-CzPQyFhp.js → menuInfo-BaN5YZPp.js} +1 -1
  58. package/dist/{menuInfo.vue_vue_type_style_index_0_lang-BumXunCg.js → menuInfo.vue_vue_type_style_index_0_lang-yI4QOYxG.js} +100 -97
  59. package/dist/{pda-app-Dvy3U-b6.js → pda-app-DEtDXFvM.js} +211 -197
  60. package/dist/{resource-Fy0lFkSV.js → resource-Bztmz7pn.js} +15 -15
  61. package/dist/{su-welcome-DYvSCUST.js → su-welcome-Bx8nBPi9.js} +109 -110
  62. package/dist/sys-config-nvrtYGgi.js +290 -0
  63. package/dist/udp-core.css +2 -2
  64. package/dist/{utogether-CjmJiHoE.js → utogether-yPnwDAIH.js} +1 -1
  65. package/index.ts +40 -36
  66. package/package.json +18 -18
  67. package/src/App.vue +65 -70
  68. package/src/api/index.ts +31 -31
  69. package/src/components/SuCharts/src/UserInfo.vue +78 -78
  70. package/src/components/SuScrollTree/ScrollPanel.vue +3 -3
  71. package/src/components/udp/count-down.vue +536 -0
  72. package/src/components/udp/flip-down/FlipCard/flip-card.vue +251 -0
  73. package/src/components/udp/flip-down/FlipCard/interfaces.ts +4 -0
  74. package/src/components/udp/flip-down/FlipClock/flip-clock.vue +113 -0
  75. package/src/components/udp/form-upload.vue +414 -64
  76. package/src/components/udp/form.vue +112 -0
  77. package/src/components/udp/grid.vue +495 -0
  78. package/src/components/udp/index.ts +10 -4
  79. package/src/components/udp/lov.vue +388 -0
  80. package/src/components/udp/modal-form.vue +13 -3
  81. package/src/components/udp/modal-grid.vue +298 -0
  82. package/src/components/udp/upload.vue +423 -0
  83. package/src/components/udp/utils.ts +447 -40
  84. package/src/directives/permission/index.ts +1 -1
  85. package/src/layout/components/lay-navbar/index.vue +239 -237
  86. package/src/layout/components/lay-panel/index.vue +150 -150
  87. package/src/layout/components/lay-search/index.vue +25 -25
  88. package/src/layout/components/lay-select-org/index.vue +64 -69
  89. package/src/layout/components/lay-sidebar/breadCrumb.vue +1 -1
  90. package/src/layout/components/lay-sidebar/horizontal.vue +190 -191
  91. package/src/layout/components/lay-sidebar/mixNav.vue +260 -258
  92. package/src/layout/components/lay-sidebar/sidebar-logo.vue +92 -98
  93. package/src/layout/components/lay-tag/index.vue +625 -625
  94. package/src/layout/hooks/useNav.ts +173 -173
  95. package/src/layout/layoutView.vue +215 -215
  96. package/src/main.ts +119 -109
  97. package/src/plugins/i18n/en.ts +302 -289
  98. package/src/plugins/i18n/zh.ts +349 -337
  99. package/src/plugins/vxe-table/index.ts +53 -46
  100. package/src/plugins/vxe-table/render.tsx +956 -817
  101. package/src/router/index.ts +177 -183
  102. package/src/router/modules/remaining.ts +58 -83
  103. package/src/store/modules/permission.ts +1 -4
  104. package/src/style/button.scss +85 -78
  105. package/src/style/tailwind.css +1 -68
  106. package/src/style/vxetable.scss +44 -11
  107. package/src/utils/authority/index.ts +1 -1
  108. package/src/utils/{http → udp/http}/index.ts +283 -297
  109. package/src/utils/{http → udp/http}/types.d.ts +8 -5
  110. package/src/utils/udp/useRender.ts +17 -6
  111. package/src/views/login/login-view.vue +300 -314
  112. package/src/views/organization/company/CompanyPanel.vue +259 -259
  113. package/src/views/organization/inv-org/InvOrganization.vue +68 -54
  114. package/src/views/organization/org/Org.vue +9 -5
  115. package/src/views/system/menu/SystemMenu.vue +183 -197
  116. package/src/views/system/menu/menuInfo.vue +363 -371
  117. package/src/views/system/role/AuthorityInfo.vue +19 -15
  118. package/src/views/system/role/Role.vue +1 -5
  119. package/src/views/system/role/RolePanel.vue +11 -2
  120. package/src/views/system/role/UserInfo.vue +195 -193
  121. package/src/views/system/role-assign/RoleAssign.vue +57 -57
  122. package/src/views/system/role-assign/RolePanel.vue +139 -136
  123. package/src/views/system/sys/sys-config.vue +287 -291
  124. package/src/views/system/sysUser/SysUserPanel.vue +278 -278
  125. package/src/views/uapp/pda/pda-app.vue +208 -176
  126. package/src/views/udev/dict/DictView.vue +118 -106
  127. package/src/views/udev/dict/childView.vue +7 -7
  128. package/src/views/udev/lov/lov-view.vue +91 -91
  129. package/src/views/ufile/aggregation/File.vue +5 -5
  130. package/src/views/ufile/file/water-mark.vue +14 -14
  131. package/src/views/uhome/su-welcome.vue +3 -3
  132. package/src/views/ulogin/login.vue +12 -8
  133. package/src/views/upms/user/login-log.vue +1 -1
  134. package/src/views/urpt/design/DesignPanel.vue +507 -526
  135. package/src/views/urpt/design/ReportDesign.vue +15 -19
  136. package/src/views/urpt/static-resource/resource.vue +3 -3
  137. package/src/views/urpt/template/ReportTemplate.vue +7 -7
  138. package/types/global.d.ts +2 -1
  139. package/vite.config.ts +6 -2
  140. package/dist/AuthorityInfo.vue_vue_type_style_index_0_lang-BqccGW7v.js +0 -102
  141. package/dist/DictView-C-i7e4hZ.js +0 -95
  142. package/dist/InvOrganization-cfT6riGU.js +0 -260
  143. package/dist/Org-CA7vTDIF.js +0 -35
  144. package/dist/RolePanel.vue_vue_type_script_setup_true_lang-cmW7zBLu.js +0 -126
  145. package/dist/SysUserPanel.vue_vue_type_script_setup_true_lang-njefUln5.js +0 -288
  146. package/dist/index-DzOzUkf6.js +0 -3388
  147. package/dist/sys-config-DJ1vNQTy.js +0 -277
@@ -1,297 +1,283 @@
1
- import Axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios';
2
- import type { SuHttpError, RequestMethods, SuHttpResponse, SuHttpRequestConfig } from './types.d';
3
- import { has } from 'xe-utils';
4
- import { NProgress, cookies, errorMessage } from '@utogether/utils';
5
- import { isRef } from 'vue';
6
-
7
- const kTOKENKEY = 'authorized-token';
8
- const defaultConfig: AxiosRequestConfig = {
9
- timeout: 180000,
10
- headers: {
11
- Accept: 'application/json, text/plain, */*',
12
- 'Content-Type': 'application/json',
13
- 'X-Requested-With': 'XMLHttpRequest'
14
- }
15
- };
16
- class SuHttp {
17
- constructor() {
18
- this.httpInterceptorsRequest();
19
- this.httpInterceptorsResponse();
20
- }
21
- baseUrl = null;
22
- router = null;
23
- /** token过期后,暂存待执行的请求 */
24
- private static requests = [];
25
-
26
- /** 防止重复刷新token */
27
- private static isRefreshing = false;
28
-
29
- /** 设置router调整 */
30
- setRouter(router) {
31
- this.router = router;
32
- }
33
-
34
- setBaseUrl(baseUrl) {
35
- this.baseUrl = baseUrl;
36
- }
37
- // 初始化配置对象
38
- private static initConfig: SuHttpRequestConfig = {};
39
-
40
- // 保存当前Axios实例对象
41
- private static axiosInstance: AxiosInstance = Axios.create(defaultConfig);
42
-
43
- /** 重连原始请求 */
44
- private static retryOriginalRequest(config: SuHttpRequestConfig) {
45
- return new Promise(resolve => {
46
- // @ts-ignore
47
- SuHttp.requests.push((token: string) => {
48
- config.headers!['Authorization'] = 'Bearer ' + token;
49
- resolve(config);
50
- });
51
- });
52
- }
53
-
54
- // 请求拦截
55
- private httpInterceptorsRequest(): void {
56
- SuHttp.axiosInstance.interceptors.request.use(
57
- (config: any) => {
58
- if (has(config, 'loading') && isRef(config.loading)) {
59
- config.loading.value = true;
60
- }
61
- // 开启进度条动画
62
- NProgress.start();
63
- // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
64
- if (typeof config.beforeRequestCallback === 'function') {
65
- config.beforeRequestCallback(config);
66
- return config;
67
- }
68
- if (SuHttp.initConfig.beforeRequestCallback) {
69
- SuHttp.initConfig.beforeRequestCallback(config);
70
- return config;
71
- }
72
- const whiteList = ['/refreshToken', '/login'];
73
- if (whiteList.some(v => config.url.indexOf(v) > -1)) return config;
74
- return new Promise(resolve => {
75
- const token = cookies.get(kTOKENKEY);
76
- if (token) {
77
- const data = JSON.parse(token);
78
- const now = new Date().getTime();
79
- const expired = data.expires - now <= 0;
80
- if (expired) {
81
- if (!SuHttp.isRefreshing) {
82
- SuHttp.isRefreshing = true;
83
- // requests.push(() => SuHttp.axiosInstance(config));
84
-
85
- this.get(this.baseUrl + '/uath/refreshToken', {
86
- refreshToken: data.refreshToken
87
- })
88
- .then((res: any) => {
89
- // 替换token
90
- this.setToken(res);
91
- config.headers['Authorization'] = 'Bearer ' + res.access_token;
92
- // token 刷新后将数组的方法重新执行
93
- // @ts-ignore
94
- SuHttp.requests.forEach(cb => cb(res.access_token));
95
- SuHttp.requests = []; // 重新请求完清空
96
- })
97
- .finally(() => {
98
- SuHttp.isRefreshing = false;
99
- });
100
- }
101
- // @ts-ignore
102
- resolve(SuHttp.retryOriginalRequest(config));
103
- } else {
104
- config.headers['Authorization'] = 'Bearer ' + data.accessToken;
105
- resolve(config);
106
- }
107
- } else {
108
- const sessionKey = cookies.get('kCookies_token') as string;
109
- if (sessionKey) {
110
- config.headers['X-Token'] = sessionKey;
111
- }
112
- resolve(config);
113
- }
114
- });
115
- },
116
- error => {
117
- return Promise.reject(error);
118
- }
119
- );
120
- }
121
-
122
- private setToken(token) {
123
- const { access_token, expires_in, refresh_token } = token;
124
- const cache = {
125
- accessToken: access_token,
126
- refreshToken: refresh_token,
127
- expires: Date.now() + expires_in * 1000
128
- };
129
- cookies.set(kTOKENKEY, JSON.stringify(cache));
130
- // cookies.set(kTOKENKEY, JSON.stringify(cache), { expires: expires_in });
131
- }
132
-
133
- // 响应拦截
134
- private httpInterceptorsResponse(): void {
135
- const instance = SuHttp.axiosInstance;
136
- instance.interceptors.response.use(
137
- (response: SuHttpResponse) => {
138
- const $config = response.config;
139
- if (has($config, 'loading') && isRef($config.loading)) {
140
- $config.loading.value = false;
141
- }
142
- // 关闭进度条动画
143
- NProgress.done();
144
- // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
145
- if (typeof $config.beforeResponseCallback === 'function') {
146
- $config.beforeResponseCallback(response);
147
- return response.data;
148
- }
149
- if (SuHttp.initConfig.beforeResponseCallback) {
150
- SuHttp.initConfig.beforeResponseCallback(response);
151
- return response.data;
152
- }
153
- return response.data;
154
- },
155
- (error: SuHttpError) => {
156
- // @ts-ignore
157
- if (has(error.config, 'loading') && isRef(error.config.loading)) {
158
- // @ts-ignore
159
- error.config.loading.value = false;
160
- }
161
- const $error = error;
162
- $error.isCancelRequest = Axios.isCancel($error);
163
- // 关闭进度条动画
164
- NProgress.done();
165
- // 所有的响应异常 区分来源为取消请求/非取消请求
166
- return Promise.reject($error);
167
- }
168
- );
169
- }
170
-
171
- private transformConfigByMethod(params: any, config: SuHttpRequestConfig): SuHttpRequestConfig {
172
- const { method } = config;
173
- const methods = ['get'];
174
- const m = method!.toLocaleLowerCase();
175
- const props = methods.includes(m) ? 'params' : 'data';
176
- return {
177
- ...config,
178
- [props]: params
179
- };
180
- }
181
-
182
- // 通用请求工具函数
183
- public request<T>(
184
- method: RequestMethods,
185
- url: string,
186
- param?: AxiosRequestConfig,
187
- axiosConfig?: SuHttpRequestConfig
188
- ): Promise<T> {
189
- const config = this.transformConfigByMethod(param, {
190
- method,
191
- url,
192
- ...axiosConfig
193
- } as SuHttpRequestConfig);
194
- // 单独处理自定义请求/响应回掉
195
- return new Promise((resolve, reject) => {
196
- SuHttp.axiosInstance
197
- .request(config)
198
- .then((res: any) => {
199
- if (res && res?.code === '0') {
200
- resolve(res.data);
201
- } else if (res.code === '500') {
202
- errorMessage(res.msg, { duration: 8000 });
203
- if (config.method === 'get') {
204
- resolve(res);
205
- } else {
206
- reject(res.msg);
207
- }
208
- } else if (res?.code !== '-1') {
209
- resolve(res);
210
- } else {
211
- const msg = res?.msg || '服务异常';
212
- errorMessage(msg, { duration: 8000 });
213
- // const code = res.code;
214
- // if (
215
- // code === "000001" ||
216
- // (code === "-1" && msg.includes("请重新登陆"))
217
- // ) {
218
- // // session过期
219
- // cookies.remove("kCookies_token");
220
- // // @ts-ignore
221
- // this.router?.push({
222
- // path: "/login",
223
- // query: { tokenExpire: "Y" },
224
- // });
225
- // }
226
- reject(msg);
227
- }
228
- })
229
- .catch(e => {
230
- debugger;
231
- if (e?.response?.data?.path === '/refreshToken') {
232
- this.router?.push('/login');
233
- } else if (e?.code) {
234
- if (e?.response?.status === 401) {
235
- // @ts-ignore
236
- return this.router?.push({ path: '/login' });
237
- }
238
- errorMessage(this.getNetworkError(e?.response?.status) || e?.message, { duration: 8000 });
239
- }
240
- if (e.config.method === 'get') {
241
- resolve(e);
242
- } else {
243
- reject(e);
244
- }
245
- });
246
- });
247
- }
248
-
249
- getNetworkError = code => {
250
- if (!code) return null;
251
- const msgMap = {
252
- 400: '错误的请求',
253
- 401: '服务未授权,请重新登录',
254
- 403: '拒绝访问',
255
- 404: '请求错误,未找到该资源',
256
- 408: '请求超时',
257
- 500: '服务器端出差',
258
- 502: '网络超时',
259
- 503: '服务不可用',
260
- 504: '网络超时'
261
- };
262
- return msgMap[code];
263
- };
264
-
265
- // 单独抽离的post工具函数
266
- public post<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
267
- // @ts-ignore
268
- return this.request<P>('post', url, params, config);
269
- }
270
-
271
- // 单独抽离的delete工具函数
272
- public delete<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
273
- // @ts-ignore
274
- return this.request<P>('delete', url, params, config);
275
- }
276
-
277
- // 单独抽离的put工具函数
278
- public put<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
279
- // @ts-ignore
280
- return this.request<P>('put', url, params, config);
281
- }
282
-
283
- // 单独抽离的get工具函数
284
- public get<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
285
- for (const key in params) {
286
- if (!params[key]) delete params[key];
287
- }
288
- // @ts-ignore
289
- return this.request<P>('get', url, params, config);
290
- }
291
-
292
- public postRouter<T>(params?: any): Promise<T> {
293
- return this.request<T>('post', 'route/service', params);
294
- }
295
- }
296
-
297
- export const http = new SuHttp();
1
+ import Axios, { type AxiosInstance, type AxiosRequestConfig } from 'axios';
2
+ import type { SuHttpError, RequestMethods, SuHttpResponse, SuHttpRequestConfig } from './types.d';
3
+
4
+ import { errorMessage, cookies, NProgress } from '@utogether/utils';
5
+ import { isRef } from 'vue';
6
+ import { has } from 'xe-utils';
7
+
8
+ const kTOKENKEY = 'authorized-token';
9
+ const defaultConfig: AxiosRequestConfig = {
10
+ timeout: 180000,
11
+ headers: {
12
+ Accept: 'application/json, text/plain, */*',
13
+ 'Content-Type': 'application/json',
14
+ 'X-Requested-With': 'XMLHttpRequest'
15
+ }
16
+ };
17
+ class SuHttp {
18
+ constructor() {
19
+ this.httpInterceptorsRequest();
20
+ this.httpInterceptorsResponse();
21
+ }
22
+ router = undefined;
23
+ baseUrl = null;
24
+ /** token过期后,暂存待执行的请求 */
25
+ private static requests = [];
26
+
27
+ /** 防止重复刷新token */
28
+ private static isRefreshing = false;
29
+
30
+ /** 设置router调整 */
31
+ setRouter(router) {
32
+ this.router = router;
33
+ }
34
+
35
+ setBaseUrl(baseUrl) {
36
+ this.baseUrl = baseUrl;
37
+ }
38
+ // 初始化配置对象
39
+ private static initConfig: SuHttpRequestConfig = {};
40
+
41
+ // 保存当前Axios实例对象
42
+ private static axiosInstance: AxiosInstance = Axios.create(defaultConfig);
43
+
44
+ /** 重连原始请求 */
45
+ private static retryOriginalRequest(config: SuHttpRequestConfig) {
46
+ return new Promise(resolve => {
47
+ // @ts-ignore
48
+ SuHttp.requests.push((token: string) => {
49
+ config.headers!['Authorization'] = 'Bearer ' + token;
50
+ resolve(config);
51
+ });
52
+ });
53
+ }
54
+
55
+ // 请求拦截
56
+ private httpInterceptorsRequest(): void {
57
+ SuHttp.axiosInstance.interceptors.request.use(
58
+ (config: any) => {
59
+ if (has(config, 'loading') && isRef(config.loading)) {
60
+ config.loading.value = true;
61
+ }
62
+ // 开启进度条动画
63
+ NProgress.start();
64
+ // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
65
+ if (typeof config.beforeRequestCallback === 'function') {
66
+ config.beforeRequestCallback(config);
67
+ return config;
68
+ }
69
+ if (SuHttp.initConfig.beforeRequestCallback) {
70
+ SuHttp.initConfig.beforeRequestCallback(config);
71
+ return config;
72
+ }
73
+ const whiteList = ['/refreshToken', '/login'];
74
+ if (whiteList.some(v => config.url.indexOf(v) > -1)) return config;
75
+ return new Promise(resolve => {
76
+ const token = cookies.get(kTOKENKEY);
77
+ debugger;
78
+ if (token) {
79
+ const data = JSON.parse(token);
80
+ const now = new Date().getTime();
81
+ const expired = data.expires - now <= 0;
82
+ if (expired) {
83
+ if (!SuHttp.isRefreshing) {
84
+ SuHttp.isRefreshing = true;
85
+ // requests.push(() => SuHttp.axiosInstance(config));
86
+ this.get((this.baseUrl || '') + '/uath/refreshToken', {
87
+ refreshToken: data.refreshToken
88
+ })
89
+ .then((res: any) => {
90
+ // 替换token
91
+ this.setToken(res);
92
+ config.headers['Authorization'] = 'Bearer ' + res.access_token;
93
+ // token 刷新后将数组的方法重新执行
94
+ // @ts-ignore
95
+ SuHttp.requests.forEach(cb => cb(res.access_token));
96
+ SuHttp.requests = []; // 重新请求完清空
97
+ })
98
+ .finally(() => {
99
+ SuHttp.isRefreshing = false;
100
+ this.router.push({ name: 'login' });
101
+ });
102
+ }
103
+ // @ts-ignore
104
+ resolve(SuHttp.retryOriginalRequest(config));
105
+ } else {
106
+ config.headers['Authorization'] = 'Bearer ' + data.accessToken;
107
+ resolve(config);
108
+ }
109
+ } else {
110
+ const sessionKey = cookies.get('kCookies_token') as string;
111
+ if (sessionKey) {
112
+ config.headers['X-Token'] = sessionKey;
113
+ }
114
+ resolve(config);
115
+ }
116
+ });
117
+ },
118
+ error => {
119
+ return Promise.reject(error);
120
+ }
121
+ );
122
+ }
123
+
124
+ private setToken(token) {
125
+ const { access_token, expires_in, refresh_token } = token;
126
+ const cache = {
127
+ accessToken: access_token,
128
+ refreshToken: refresh_token,
129
+ expires: Date.now() + expires_in * 1000
130
+ };
131
+ cookies.set(kTOKENKEY, JSON.stringify(cache));
132
+ // cookies.set(kTOKENKEY, JSON.stringify(cache), { expires: expires_in });
133
+ }
134
+
135
+ // 响应拦截
136
+ private httpInterceptorsResponse(): void {
137
+ const instance = SuHttp.axiosInstance;
138
+ instance.interceptors.response.use(
139
+ (response: SuHttpResponse) => {
140
+ const $config = response.config;
141
+ if (has($config, 'loading') && isRef($config.loading)) {
142
+ $config.loading.value = false;
143
+ }
144
+ // 关闭进度条动画
145
+ NProgress.done();
146
+ // 优先判断post/get等方法是否传入回掉,否则执行初始化设置等回掉
147
+ if (typeof $config.beforeResponseCallback === 'function') {
148
+ $config.beforeResponseCallback(response);
149
+ return response.data;
150
+ }
151
+ if (SuHttp.initConfig.beforeResponseCallback) {
152
+ SuHttp.initConfig.beforeResponseCallback(response);
153
+ return response.data;
154
+ }
155
+ return response.data;
156
+ },
157
+ (error: SuHttpError) => {
158
+ const $error = error;
159
+ // @ts-ignore
160
+ if (has(error.config, 'loading') && isRef(error?.config?.loading)) {
161
+ // @ts-ignore
162
+ error.config.loading.value = false;
163
+ }
164
+ $error.isCancelRequest = Axios.isCancel($error);
165
+ // 关闭进度条动画
166
+ NProgress.done();
167
+ // 所有的响应异常 区分来源为取消请求/非取消请求
168
+ return Promise.reject($error);
169
+ }
170
+ );
171
+ }
172
+
173
+ private transformConfigByMethod(params: any, config: SuHttpRequestConfig): SuHttpRequestConfig {
174
+ const { method } = config;
175
+ const methods = ['get'];
176
+ const m = method!.toLocaleLowerCase();
177
+ const props = methods.includes(m) ? 'params' : 'data';
178
+ return {
179
+ ...config,
180
+ [props]: params
181
+ };
182
+ }
183
+
184
+ // 通用请求工具函数
185
+ public request<T>(
186
+ method: RequestMethods,
187
+ url: string,
188
+ param?: AxiosRequestConfig,
189
+ axiosConfig?: SuHttpRequestConfig
190
+ ): Promise<T> {
191
+ const config = this.transformConfigByMethod(param, {
192
+ method,
193
+ url,
194
+ ...axiosConfig
195
+ } as SuHttpRequestConfig);
196
+ // 单独处理自定义请求/响应回掉
197
+ return new Promise((resolve, reject) => {
198
+ SuHttp.axiosInstance
199
+ .request(config)
200
+ .then((res: any) => {
201
+ if (res && res?.code === '0') {
202
+ resolve(res.data);
203
+ } else if (res.code === '500') {
204
+ errorMessage(res.msg, { duration: 8000 });
205
+ if (config.method === 'get') {
206
+ resolve(res);
207
+ } else {
208
+ reject(res.msg);
209
+ }
210
+ } else if (res?.code !== '-1') {
211
+ resolve(res);
212
+ } else {
213
+ const msg = res?.msg || '服务异常';
214
+ errorMessage(msg, { duration: 8000 });
215
+ reject(msg);
216
+ }
217
+ })
218
+ .catch(e => {
219
+ if (e?.code) {
220
+ if (e?.response?.status === 401) {
221
+ // @ts-ignore
222
+ return this.router?.push({ path: '/login' });
223
+ }
224
+ errorMessage(this.getNetworkError(e?.response?.status) || e?.message, { duration: 8000 });
225
+ }
226
+ if (e.config.method === 'get') {
227
+ resolve(e);
228
+ } else {
229
+ reject(e);
230
+ }
231
+ });
232
+ });
233
+ }
234
+
235
+ getNetworkError = code => {
236
+ if (!code) return null;
237
+ const msgMap = {
238
+ 400: '错误的请求',
239
+ 401: '服务未授权,请重新登录',
240
+ 403: '拒绝访问',
241
+ 404: '请求错误,未找到该资源',
242
+ 408: '请求超时',
243
+ 500: '服务器端出差',
244
+ 502: '网络超时',
245
+ 503: '服务不可用',
246
+ 504: '网络超时'
247
+ };
248
+ return msgMap[code];
249
+ };
250
+
251
+ // 单独抽离的post工具函数
252
+ public post<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
253
+ // @ts-ignore
254
+ return this.request<P>('post', url, params, config);
255
+ }
256
+
257
+ // 单独抽离的delete工具函数
258
+ public delete<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
259
+ // @ts-ignore
260
+ return this.request<P>('delete', url, params, config);
261
+ }
262
+
263
+ // 单独抽离的put工具函数
264
+ public put<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
265
+ // @ts-ignore
266
+ return this.request<P>('put', url, params, config);
267
+ }
268
+
269
+ // 单独抽离的get工具函数
270
+ public get<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P> {
271
+ for (const key in params) {
272
+ if (!params[key]) delete params[key];
273
+ }
274
+ // @ts-ignore
275
+ return this.request<P>('get', url, params, config);
276
+ }
277
+
278
+ public postRouter<T>(params?: any): Promise<T> {
279
+ return this.request<T>('post', 'route/service', params);
280
+ }
281
+ }
282
+
283
+ export const http = new SuHttp();
@@ -2,10 +2,10 @@
2
2
  * @Author: wei.li
3
3
  * @Date: 2022-03-08 15:17:39
4
4
  * @LastEditors: levi7754 levi7754@163.com
5
- * @LastEditTime: 2023-12-01 11:59:12
5
+ * @LastEditTime: 2023-12-01 13:55:46
6
6
  * @Description: SuHttp Axios 类型定义文件
7
7
  */
8
- import type { Method, AxiosError, AxiosResponse, AxiosRequestConfig } from 'axios';
8
+ import { Method, AxiosError, AxiosResponse, AxiosRequestConfig } from "axios";
9
9
 
10
10
  export type resultType = {
11
11
  access_token: string;
@@ -15,12 +15,11 @@ export type resultType = {
15
15
 
16
16
  export type RequestMethods = Extract<
17
17
  Method,
18
- 'get' | 'post' | 'put' | 'delete' | 'patch' | 'option' | 'head'
18
+ "get" | "post" | "put" | "delete" | "patch" | "option" | "head"
19
19
  >;
20
20
 
21
21
  export interface SuHttpError extends AxiosError {
22
22
  isCancelRequest?: boolean;
23
- loading?: boolean;
24
23
  }
25
24
 
26
25
  export interface SuHttpResponse extends AxiosResponse {
@@ -41,6 +40,10 @@ export default class SuHttp {
41
40
  axiosConfig?: SuHttpRequestConfig
42
41
  ): Promise<T>;
43
42
  post<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P>;
44
- postRouter<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P>;
43
+ postRouter<T, P>(
44
+ url: string,
45
+ params?: T,
46
+ config?: SuHttpRequestConfig
47
+ ): Promise<P>;
45
48
  get<T, P>(url: string, params?: T, config?: SuHttpRequestConfig): Promise<P>;
46
49
  }