@ibiz-template/core 0.0.1-alpha.6 → 0.0.1-beta.130
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.
- package/dist/system/index.system.js +1 -0
- package/out/constant/core/core.d.ts +9 -0
- package/out/constant/core/core.d.ts.map +1 -1
- package/out/constant/core/core.js +9 -0
- package/out/constant/index.d.ts +3 -0
- package/out/constant/index.d.ts.map +1 -1
- package/out/constant/index.js +3 -0
- package/out/constant/login-mode/login-mode.d.ts +23 -0
- package/out/constant/login-mode/login-mode.d.ts.map +1 -0
- package/out/constant/login-mode/login-mode.js +24 -0
- package/out/constant/menu-permission-mode/menu-permission-mode.d.ts +23 -0
- package/out/constant/menu-permission-mode/menu-permission-mode.d.ts.map +1 -0
- package/out/constant/menu-permission-mode/menu-permission-mode.js +24 -0
- package/out/constant/util/util.d.ts +5 -0
- package/out/constant/util/util.d.ts.map +1 -0
- package/out/constant/util/util.js +5 -0
- package/out/context/index.d.ts +49 -27
- package/out/context/index.d.ts.map +1 -1
- package/out/context/index.js +94 -55
- package/out/environment/environment.d.ts.map +1 -1
- package/out/environment/environment.js +16 -2
- package/out/error/http-error/http-error.d.ts.map +1 -1
- package/out/error/http-error/http-error.js +0 -4
- package/out/factory-center.d.ts +11 -0
- package/out/factory-center.d.ts.map +1 -0
- package/out/factory-center.js +10 -0
- package/out/ibizsys.d.ts +8 -1
- package/out/ibizsys.d.ts.map +1 -1
- package/out/ibizsys.js +8 -1
- package/out/index.d.ts +2 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +2 -1
- package/out/install.d.ts.map +1 -1
- package/out/install.js +4 -2
- package/out/interface/click-outside/click-outside.d.ts +70 -0
- package/out/interface/click-outside/click-outside.d.ts.map +1 -0
- package/out/interface/click-outside/click-outside.js +1 -0
- package/out/interface/i-environment/i-environment.d.ts +108 -4
- package/out/interface/i-environment/i-environment.d.ts.map +1 -1
- package/out/interface/index.d.ts +1 -0
- package/out/interface/index.d.ts.map +1 -1
- package/out/interface/index.js +1 -1
- package/out/types.d.ts +41 -0
- package/out/types.d.ts.map +1 -1
- package/out/utils/click-outside/click-outside.d.ts +14 -0
- package/out/utils/click-outside/click-outside.d.ts.map +1 -0
- package/out/utils/click-outside/click-outside.js +85 -0
- package/out/utils/color/color.d.ts +13 -0
- package/out/utils/color/color.d.ts.map +1 -0
- package/out/utils/color/color.js +76 -0
- package/out/utils/download-file/download-file.d.ts +31 -0
- package/out/utils/download-file/download-file.d.ts.map +1 -0
- package/out/utils/download-file/download-file.js +107 -0
- package/out/utils/event/event.d.ts +34 -0
- package/out/utils/event/event.d.ts.map +1 -0
- package/out/utils/event/event.js +60 -0
- package/out/utils/index.d.ts +11 -1
- package/out/utils/index.d.ts.map +1 -1
- package/out/utils/index.js +11 -1
- package/out/utils/interceptor/core-interceptor.d.ts +15 -0
- package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
- package/out/utils/interceptor/core-interceptor.js +30 -0
- package/out/utils/interceptor/index.d.ts +3 -0
- package/out/utils/interceptor/index.d.ts.map +1 -0
- package/out/utils/interceptor/index.js +2 -0
- package/out/utils/interceptor/interceptor.d.ts +74 -3
- package/out/utils/interceptor/interceptor.d.ts.map +1 -1
- package/out/utils/interceptor/interceptor.js +73 -22
- package/out/utils/logger/logger.d.ts +3 -0
- package/out/utils/logger/logger.d.ts.map +1 -0
- package/out/utils/logger/logger.js +2 -0
- package/out/utils/net/http-response.d.ts +4 -9
- package/out/utils/net/http-response.d.ts.map +1 -1
- package/out/utils/net/http-response.js +0 -7
- package/out/utils/net/net.d.ts +98 -30
- package/out/utils/net/net.d.ts.map +1 -1
- package/out/utils/net/net.js +133 -38
- package/out/utils/plural/plural.d.ts.map +1 -1
- package/out/utils/plural/plural.js +2 -0
- package/out/utils/string-util/string-util.d.ts +46 -0
- package/out/utils/string-util/string-util.d.ts.map +1 -0
- package/out/utils/string-util/string-util.js +71 -0
- package/out/utils/sync/await-timeout.d.ts +14 -0
- package/out/utils/sync/await-timeout.d.ts.map +1 -0
- package/out/utils/sync/await-timeout.js +23 -0
- package/out/utils/sync/count-latch.d.ts +60 -0
- package/out/utils/sync/count-latch.d.ts.map +1 -0
- package/out/utils/sync/count-latch.js +90 -0
- package/out/utils/sync/index.d.ts +3 -0
- package/out/utils/sync/index.d.ts.map +1 -0
- package/out/utils/sync/index.js +2 -0
- package/out/utils/upload/select-file.d.ts +53 -0
- package/out/utils/upload/select-file.d.ts.map +1 -0
- package/out/utils/upload/select-file.js +47 -0
- package/out/utils/upload/upload-file.d.ts +46 -0
- package/out/utils/upload/upload-file.d.ts.map +1 -0
- package/out/utils/upload/upload-file.js +150 -0
- package/out/utils/url-helper/url-helper.d.ts +51 -0
- package/out/utils/url-helper/url-helper.d.ts.map +1 -0
- package/out/utils/url-helper/url-helper.js +59 -0
- package/out/utils/util/util.d.ts +20 -4
- package/out/utils/util/util.d.ts.map +1 -1
- package/out/utils/util/util.js +59 -5
- package/package.json +21 -10
- package/src/constant/core/core.ts +10 -0
- package/src/constant/index.ts +3 -0
- package/src/constant/login-mode/login-mode.ts +23 -0
- package/src/constant/menu-permission-mode/menu-permission-mode.ts +23 -0
- package/src/constant/util/util.ts +5 -0
- package/src/context/index.ts +127 -51
- package/src/environment/environment.ts +16 -2
- package/src/error/http-error/http-error.ts +0 -4
- package/src/factory-center.ts +9 -0
- package/src/ibizsys.ts +9 -1
- package/src/index.ts +2 -1
- package/src/install.ts +4 -2
- package/src/interface/click-outside/click-outside.ts +71 -0
- package/src/interface/i-environment/i-environment.ts +122 -4
- package/src/interface/index.ts +1 -0
- package/src/types.ts +45 -0
- package/src/utils/click-outside/click-outside.ts +110 -0
- package/src/utils/color/color.ts +84 -0
- package/src/utils/download-file/download-file.ts +109 -0
- package/src/utils/event/event.ts +77 -0
- package/src/utils/index.ts +11 -1
- package/src/utils/interceptor/core-interceptor.ts +35 -0
- package/src/utils/interceptor/index.ts +2 -0
- package/src/utils/interceptor/interceptor.ts +109 -22
- package/src/utils/logger/logger.ts +3 -0
- package/src/utils/net/http-response.ts +6 -9
- package/src/utils/net/net.ts +171 -50
- package/src/utils/plural/plural.ts +3 -0
- package/src/utils/string-util/string-util.ts +75 -0
- package/src/utils/sync/await-timeout.ts +27 -0
- package/src/utils/sync/count-latch.ts +96 -0
- package/src/utils/sync/index.ts +2 -0
- package/src/utils/upload/select-file.ts +86 -0
- package/src/utils/upload/upload-file.ts +206 -0
- package/src/utils/url-helper/url-helper.ts +62 -0
- package/src/utils/util/util.ts +72 -14
- package/LICENSE +0 -21
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* 拦截器基类
|
|
6
6
|
*
|
|
7
7
|
* @author chitanda
|
|
8
8
|
* @date 2022-07-20 18:07:11
|
|
@@ -10,26 +10,113 @@ import { getToken } from '../util/util';
|
|
|
10
10
|
* @class Interceptor
|
|
11
11
|
*/
|
|
12
12
|
export class Interceptor {
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* 请求拦截器绑定标识
|
|
15
|
+
*
|
|
16
|
+
* @author lxm
|
|
17
|
+
* @date 2022-10-27 17:10:20
|
|
18
|
+
* @private
|
|
19
|
+
* @type {number}
|
|
20
|
+
*/
|
|
21
|
+
private requestTag?: number;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 响应拦截器绑定标识
|
|
25
|
+
*
|
|
26
|
+
* @author lxm
|
|
27
|
+
* @date 2022-10-27 17:10:19
|
|
28
|
+
* @private
|
|
29
|
+
* @type {number}
|
|
30
|
+
*/
|
|
31
|
+
private responseTag?: number;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 请求之前处理
|
|
35
|
+
*
|
|
36
|
+
* @author lxm
|
|
37
|
+
* @date 2022-10-27 17:10:41
|
|
38
|
+
* @protected
|
|
39
|
+
* @param {AxiosRequestConfig} config
|
|
40
|
+
* @returns {*} {Promise<AxiosRequestConfig>}
|
|
41
|
+
*/
|
|
42
|
+
protected async onBeforeRequest(
|
|
43
|
+
config: AxiosRequestConfig,
|
|
44
|
+
): Promise<AxiosRequestConfig> {
|
|
45
|
+
return config;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 请求失败之后处理
|
|
50
|
+
*
|
|
51
|
+
* @author lxm
|
|
52
|
+
* @date 2022-10-27 17:10:40
|
|
53
|
+
* @protected
|
|
54
|
+
* @param {*} error
|
|
55
|
+
*/
|
|
56
|
+
protected onRequestError(error: any) {
|
|
57
|
+
// 处理请求错误
|
|
58
|
+
return Promise.reject(error);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 响应成功之后处理
|
|
63
|
+
*
|
|
64
|
+
* @author lxm
|
|
65
|
+
* @date 2022-10-27 17:10:38
|
|
66
|
+
* @protected
|
|
67
|
+
* @param {AxiosResponse} config
|
|
68
|
+
* @returns {*} {Promise<AxiosRequestConfig>}
|
|
69
|
+
*/
|
|
70
|
+
protected async onResponseSuccess(
|
|
71
|
+
config: AxiosResponse,
|
|
72
|
+
): Promise<AxiosResponse> {
|
|
73
|
+
return config;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 响应失败之后处理
|
|
78
|
+
*
|
|
79
|
+
* @author lxm
|
|
80
|
+
* @date 2022-10-27 17:10:37
|
|
81
|
+
* @protected
|
|
82
|
+
* @param {*} _error
|
|
83
|
+
*/
|
|
84
|
+
protected onResponseError(error: any) {
|
|
85
|
+
// 处理响应错误
|
|
86
|
+
return Promise.reject(error);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 使用拦截器
|
|
91
|
+
*
|
|
92
|
+
* @author lxm
|
|
93
|
+
* @date 2022-10-27 17:10:28
|
|
94
|
+
* @param {AxiosInstance} instance
|
|
95
|
+
*/
|
|
96
|
+
use(instance: AxiosInstance): void {
|
|
97
|
+
this.requestTag = instance.interceptors.request.use(
|
|
98
|
+
this.onBeforeRequest,
|
|
99
|
+
this.onRequestError,
|
|
100
|
+
);
|
|
101
|
+
this.responseTag = instance.interceptors.response.use(
|
|
102
|
+
this.onResponseSuccess,
|
|
103
|
+
this.onResponseError,
|
|
104
|
+
);
|
|
15
105
|
}
|
|
16
106
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
return config;
|
|
33
|
-
});
|
|
107
|
+
/**
|
|
108
|
+
* 移出拦截器
|
|
109
|
+
*
|
|
110
|
+
* @author lxm
|
|
111
|
+
* @date 2022-10-27 17:10:27
|
|
112
|
+
* @param {AxiosInstance} instance
|
|
113
|
+
*/
|
|
114
|
+
eject(instance: AxiosInstance) {
|
|
115
|
+
if (this.requestTag) {
|
|
116
|
+
instance.interceptors.request.eject(this.requestTag);
|
|
117
|
+
}
|
|
118
|
+
if (this.responseTag) {
|
|
119
|
+
instance.interceptors.response.eject(this.responseTag);
|
|
120
|
+
}
|
|
34
121
|
}
|
|
35
122
|
}
|
|
@@ -28,7 +28,7 @@ export interface IHttpResponse<T = IData> extends AxiosResponse {
|
|
|
28
28
|
* @date 2022-08-18 15:08:44
|
|
29
29
|
* @type {boolean}
|
|
30
30
|
*/
|
|
31
|
-
local
|
|
31
|
+
local: boolean;
|
|
32
32
|
|
|
33
33
|
data: T;
|
|
34
34
|
|
|
@@ -75,14 +75,11 @@ export class HttpResponse<T = IData> implements IHttpResponse<T> {
|
|
|
75
75
|
|
|
76
76
|
statusText: string;
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
* @type {AxiosResponseHeaders}
|
|
84
|
-
*/
|
|
85
|
-
headers: AxiosResponseHeaders = {};
|
|
78
|
+
headers:
|
|
79
|
+
| AxiosResponseHeaders
|
|
80
|
+
| Partial<
|
|
81
|
+
Record<string, string> & { 'set-cookie'?: string[] | undefined }
|
|
82
|
+
> = {};
|
|
86
83
|
|
|
87
84
|
/**
|
|
88
85
|
* AxiosResponse 本地仿造时值为空
|
package/src/utils/net/net.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import axios, {
|
|
1
|
+
import axios, {
|
|
2
|
+
AxiosError,
|
|
3
|
+
RawAxiosRequestHeaders,
|
|
4
|
+
AxiosResponse,
|
|
5
|
+
AxiosRequestConfig,
|
|
6
|
+
AxiosInstance,
|
|
7
|
+
CreateAxiosDefaults,
|
|
8
|
+
} from 'axios';
|
|
9
|
+
import { merge } from 'lodash-es';
|
|
2
10
|
import { stringify } from 'qs';
|
|
3
11
|
import { notNilEmpty } from 'qx-util';
|
|
4
12
|
import { HttpError } from '../../error';
|
|
13
|
+
import { CoreInterceptor } from '../interceptor';
|
|
14
|
+
import { Interceptor } from '../interceptor/interceptor';
|
|
5
15
|
import { IHttpResponse } from './http-response';
|
|
6
16
|
|
|
7
17
|
/**
|
|
@@ -14,58 +24,136 @@ import { IHttpResponse } from './http-response';
|
|
|
14
24
|
*/
|
|
15
25
|
export class Net {
|
|
16
26
|
/**
|
|
17
|
-
*
|
|
27
|
+
* axios实例
|
|
28
|
+
*
|
|
29
|
+
* @author lxm
|
|
30
|
+
* @date 2022-10-27 17:10:18
|
|
31
|
+
* @protected
|
|
32
|
+
* @type {AxiosInstance}
|
|
33
|
+
*/
|
|
34
|
+
protected instance: AxiosInstance;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 是否为 http || https 开头
|
|
18
38
|
*
|
|
19
39
|
* @author chitanda
|
|
20
|
-
* @date 2022-07
|
|
40
|
+
* @date 2022-11-07 14:11:28
|
|
21
41
|
* @protected
|
|
22
|
-
* @type {string}
|
|
23
42
|
*/
|
|
24
|
-
protected
|
|
43
|
+
protected urlReg = /^http[s]?:\/\/[^\s]*/;
|
|
25
44
|
|
|
26
|
-
|
|
27
|
-
|
|
45
|
+
/**
|
|
46
|
+
* Creates an instance of Net.
|
|
47
|
+
* @author lxm
|
|
48
|
+
* @date 2022-10-27 16:10:05
|
|
49
|
+
* @param {CreateAxiosDefaults} [config] 创建实例用的默认配置
|
|
50
|
+
*/
|
|
51
|
+
constructor(config?: CreateAxiosDefaults) {
|
|
52
|
+
this.instance = axios.create(config);
|
|
53
|
+
this.addInterceptor('Default', new CoreInterceptor());
|
|
28
54
|
}
|
|
29
55
|
|
|
30
56
|
/**
|
|
31
|
-
*
|
|
57
|
+
* 注册的拦截器
|
|
32
58
|
*
|
|
33
|
-
* @author
|
|
34
|
-
* @date 2022-
|
|
35
|
-
* @
|
|
59
|
+
* @author lxm
|
|
60
|
+
* @date 2022-10-27 17:10:18
|
|
61
|
+
* @type {Map<string, Interceptor>}
|
|
62
|
+
*/
|
|
63
|
+
interceptors: Map<string, Interceptor> = new Map();
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 添加拦截器
|
|
67
|
+
*
|
|
68
|
+
* @author lxm
|
|
69
|
+
* @date 2022-10-27 17:10:42
|
|
70
|
+
* @param {string} name 唯一标识
|
|
71
|
+
* @param {Interceptor} interceptor 拦截器
|
|
36
72
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
73
|
+
addInterceptor(name: string, interceptor: Interceptor) {
|
|
74
|
+
interceptor.use(this.instance);
|
|
75
|
+
this.interceptors.set(name, interceptor);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 删除拦截器
|
|
80
|
+
*
|
|
81
|
+
* @author lxm
|
|
82
|
+
* @date 2022-10-27 17:10:27
|
|
83
|
+
* @param {string} name 唯一标识
|
|
84
|
+
*/
|
|
85
|
+
removeInterceptor(name: string) {
|
|
86
|
+
const interceptor = this.interceptors.get(name);
|
|
87
|
+
if (interceptor) {
|
|
88
|
+
interceptor.eject(this.instance);
|
|
89
|
+
this.interceptors.delete(name);
|
|
40
90
|
}
|
|
41
91
|
}
|
|
42
92
|
|
|
93
|
+
/**
|
|
94
|
+
* 预置config,绑定动态的配置
|
|
95
|
+
*
|
|
96
|
+
* @author lxm
|
|
97
|
+
* @date 2022-10-27 16:10:48
|
|
98
|
+
* @readonly
|
|
99
|
+
* @protected
|
|
100
|
+
* @type {AxiosRequestConfig}
|
|
101
|
+
*/
|
|
102
|
+
protected get presetConfig(): AxiosRequestConfig {
|
|
103
|
+
return {
|
|
104
|
+
// 请求前缀路径
|
|
105
|
+
baseURL: ibiz.env.baseUrl,
|
|
106
|
+
headers: {
|
|
107
|
+
'Content-Type': 'application/json;charset=UTF-8',
|
|
108
|
+
Accept: 'application/json',
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 从左到右递归合并配置参数(内置第一个合并的预置参数)
|
|
115
|
+
*
|
|
116
|
+
* @author lxm
|
|
117
|
+
* @date 2022-10-27 16:10:09
|
|
118
|
+
* @protected
|
|
119
|
+
* @param {...AxiosRequestConfig[]} configs
|
|
120
|
+
* @returns {*}
|
|
121
|
+
*/
|
|
122
|
+
protected mergeConfig(...configs: AxiosRequestConfig[]) {
|
|
123
|
+
const config = this.presetConfig;
|
|
124
|
+
if (configs.length === 0) {
|
|
125
|
+
return config;
|
|
126
|
+
}
|
|
127
|
+
const { url } = configs[0];
|
|
128
|
+
if (url && this.urlReg.test(url)) {
|
|
129
|
+
delete config.baseURL;
|
|
130
|
+
}
|
|
131
|
+
return merge(config, ...configs);
|
|
132
|
+
}
|
|
133
|
+
|
|
43
134
|
/**
|
|
44
135
|
* Post 请求
|
|
45
136
|
*
|
|
46
137
|
* @author chitanda
|
|
47
|
-
* @date 2022-
|
|
138
|
+
* @date 2022-10-19 11:10:30
|
|
48
139
|
* @param {string} url
|
|
140
|
+
* @param {IData} data
|
|
49
141
|
* @param {IParams} [params={}]
|
|
142
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
50
143
|
* @return {*} {Promise<IHttpResponse>}
|
|
51
144
|
*/
|
|
52
145
|
async post(
|
|
53
146
|
url: string,
|
|
54
147
|
data: IData,
|
|
55
148
|
params: IParams = {},
|
|
56
|
-
headers:
|
|
149
|
+
headers: RawAxiosRequestHeaders = {},
|
|
57
150
|
): Promise<IHttpResponse> {
|
|
58
151
|
url = this.handleAppPresetParam(url, params);
|
|
59
152
|
try {
|
|
60
|
-
const response = await
|
|
153
|
+
const response = await this.request(url, {
|
|
61
154
|
method: 'post',
|
|
62
|
-
url,
|
|
63
155
|
data,
|
|
64
|
-
headers
|
|
65
|
-
'Content-Type': 'application/json;charset=UTF-8',
|
|
66
|
-
Accept: 'application/json',
|
|
67
|
-
...headers,
|
|
68
|
-
},
|
|
156
|
+
headers,
|
|
69
157
|
});
|
|
70
158
|
return this.doResponseResult(response);
|
|
71
159
|
} catch (error) {
|
|
@@ -77,16 +165,18 @@ export class Net {
|
|
|
77
165
|
* Get 请求
|
|
78
166
|
*
|
|
79
167
|
* @author chitanda
|
|
80
|
-
* @date 2022-
|
|
168
|
+
* @date 2022-10-19 11:10:24
|
|
81
169
|
* @param {string} url
|
|
82
170
|
* @param {IParams} [params={}]
|
|
171
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
172
|
+
* @param {IParams} [option={}]
|
|
83
173
|
* @return {*} {Promise<IHttpResponse>}
|
|
84
174
|
*/
|
|
85
175
|
async get(
|
|
86
176
|
url: string,
|
|
87
177
|
params: IParams = {},
|
|
88
|
-
headers:
|
|
89
|
-
option:
|
|
178
|
+
headers: RawAxiosRequestHeaders = {},
|
|
179
|
+
option: AxiosRequestConfig = {},
|
|
90
180
|
): Promise<IHttpResponse> {
|
|
91
181
|
// if (params.srfparentdata) {
|
|
92
182
|
// Object.assign(params, params.srfparentdata);
|
|
@@ -94,7 +184,10 @@ export class Net {
|
|
|
94
184
|
// }
|
|
95
185
|
url = this.attachUrlParam(url, params);
|
|
96
186
|
try {
|
|
97
|
-
const response = await
|
|
187
|
+
const response = await this.request(
|
|
188
|
+
url,
|
|
189
|
+
merge({ method: 'get', headers }, option),
|
|
190
|
+
);
|
|
98
191
|
return this.doResponseResult(response);
|
|
99
192
|
} catch (error) {
|
|
100
193
|
throw new HttpError(error as AxiosError);
|
|
@@ -105,19 +198,20 @@ export class Net {
|
|
|
105
198
|
* Delete 请求
|
|
106
199
|
*
|
|
107
200
|
* @author chitanda
|
|
108
|
-
* @date 2022-
|
|
201
|
+
* @date 2022-10-19 11:10:17
|
|
109
202
|
* @param {string} url
|
|
110
203
|
* @param {IParams} [params]
|
|
204
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
111
205
|
* @return {*} {Promise<IHttpResponse>}
|
|
112
206
|
*/
|
|
113
207
|
async delete(
|
|
114
208
|
url: string,
|
|
115
209
|
params?: IParams,
|
|
116
|
-
headers:
|
|
210
|
+
headers: RawAxiosRequestHeaders = {},
|
|
117
211
|
): Promise<IHttpResponse> {
|
|
118
212
|
url = this.handleAppPresetParam(url, params);
|
|
119
213
|
try {
|
|
120
|
-
const response = await
|
|
214
|
+
const response = await this.request(url, { method: 'delete', headers });
|
|
121
215
|
return this.doResponseResult(response);
|
|
122
216
|
} catch (error) {
|
|
123
217
|
throw new HttpError(error as AxiosError);
|
|
@@ -128,20 +222,26 @@ export class Net {
|
|
|
128
222
|
* Put 请求
|
|
129
223
|
*
|
|
130
224
|
* @author chitanda
|
|
131
|
-
* @date 2022-
|
|
225
|
+
* @date 2022-10-19 11:10:11
|
|
132
226
|
* @param {string} url
|
|
133
227
|
* @param {IData} data
|
|
228
|
+
* @param {IParams} [params={}]
|
|
229
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
134
230
|
* @return {*} {Promise<IHttpResponse>}
|
|
135
231
|
*/
|
|
136
232
|
async put(
|
|
137
233
|
url: string,
|
|
138
234
|
data: IData,
|
|
139
235
|
params: IParams = {},
|
|
140
|
-
headers:
|
|
236
|
+
headers: RawAxiosRequestHeaders = {},
|
|
141
237
|
): Promise<IHttpResponse> {
|
|
142
238
|
url = this.handleAppPresetParam(url, params);
|
|
143
239
|
try {
|
|
144
|
-
const response = await
|
|
240
|
+
const response = await this.request(url, {
|
|
241
|
+
method: 'put',
|
|
242
|
+
data,
|
|
243
|
+
headers,
|
|
244
|
+
});
|
|
145
245
|
return this.doResponseResult(response);
|
|
146
246
|
} catch (error) {
|
|
147
247
|
throw new HttpError(error as AxiosError);
|
|
@@ -154,15 +254,15 @@ export class Net {
|
|
|
154
254
|
* @author chitanda
|
|
155
255
|
* @date 2022-07-14 15:07:15
|
|
156
256
|
* @param {string} url
|
|
157
|
-
* @param {
|
|
257
|
+
* @param {RawAxiosRequestHeaders} [headers={}]
|
|
158
258
|
* @return {*} {Promise<IHttpResponse>}
|
|
159
259
|
*/
|
|
160
260
|
async getModel(
|
|
161
261
|
url: string,
|
|
162
|
-
headers:
|
|
262
|
+
headers: RawAxiosRequestHeaders = {},
|
|
163
263
|
): Promise<IHttpResponse> {
|
|
164
264
|
try {
|
|
165
|
-
const response = await
|
|
265
|
+
const response = await this.instance.get(url, {
|
|
166
266
|
headers,
|
|
167
267
|
});
|
|
168
268
|
return this.doResponseResult(response);
|
|
@@ -171,6 +271,41 @@ export class Net {
|
|
|
171
271
|
}
|
|
172
272
|
}
|
|
173
273
|
|
|
274
|
+
/**
|
|
275
|
+
* 基础请求方法,会合并预置配置
|
|
276
|
+
*
|
|
277
|
+
* @author lxm
|
|
278
|
+
* @date 2022-10-27 14:10:06
|
|
279
|
+
* @param {string} url
|
|
280
|
+
* @param {AxiosRequestConfig} [config={}]
|
|
281
|
+
* @returns {*} {Promise<IHttpResponse>}
|
|
282
|
+
*/
|
|
283
|
+
async request(
|
|
284
|
+
url: string,
|
|
285
|
+
config: AxiosRequestConfig = {},
|
|
286
|
+
): Promise<IHttpResponse> {
|
|
287
|
+
try {
|
|
288
|
+
const response = await this.instance.request(
|
|
289
|
+
this.mergeConfig({ url }, config),
|
|
290
|
+
);
|
|
291
|
+
return this.doResponseResult(response);
|
|
292
|
+
} catch (error) {
|
|
293
|
+
throw new HttpError(error as AxiosError);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 创建标准 axios 请求
|
|
299
|
+
*
|
|
300
|
+
* @author chitanda
|
|
301
|
+
* @date 2023-01-30 15:01:27
|
|
302
|
+
* @param {AxiosRequestConfig<IData>} config
|
|
303
|
+
* @return {*}
|
|
304
|
+
*/
|
|
305
|
+
axios(config: AxiosRequestConfig<IData>) {
|
|
306
|
+
return axios(config);
|
|
307
|
+
}
|
|
308
|
+
|
|
174
309
|
/**
|
|
175
310
|
* 统一处理请求返回
|
|
176
311
|
*
|
|
@@ -223,7 +358,6 @@ export class Net {
|
|
|
223
358
|
* @return {*} {string}
|
|
224
359
|
*/
|
|
225
360
|
private attachUrlParam(url: string, params: IParams): string {
|
|
226
|
-
url = this.attachUrl(url);
|
|
227
361
|
const strParams: string = stringify(params);
|
|
228
362
|
if (notNilEmpty(strParams)) {
|
|
229
363
|
if (url.endsWith('?')) {
|
|
@@ -238,17 +372,4 @@ export class Net {
|
|
|
238
372
|
}
|
|
239
373
|
return url;
|
|
240
374
|
}
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* 处理 url 参数
|
|
244
|
-
*
|
|
245
|
-
* @author chitanda
|
|
246
|
-
* @date 2022-07-20 20:07:17
|
|
247
|
-
* @private
|
|
248
|
-
* @param {string} url
|
|
249
|
-
* @return {*} {string}
|
|
250
|
-
*/
|
|
251
|
-
private attachUrl(url: string): string {
|
|
252
|
-
return `${this.prefix}${url}`;
|
|
253
|
-
}
|
|
254
375
|
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { notNilEmpty } from 'qx-util';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 字符串工具类
|
|
5
|
+
*
|
|
6
|
+
* @author chitanda
|
|
7
|
+
* @date 2021-04-23 20:04:27
|
|
8
|
+
* @export
|
|
9
|
+
* @class StringUtil
|
|
10
|
+
*/
|
|
11
|
+
export class StringUtil {
|
|
12
|
+
/**
|
|
13
|
+
* 上下文替换正则
|
|
14
|
+
*
|
|
15
|
+
* @author chitanda
|
|
16
|
+
* @date 2021-04-23 20:04:01
|
|
17
|
+
* @static
|
|
18
|
+
*/
|
|
19
|
+
static contextReg = /\$\{context.[a-zA-Z_$][a-zA-Z0-9_$]{1,}\}/g;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 数据替换正则
|
|
23
|
+
*
|
|
24
|
+
* @author chitanda
|
|
25
|
+
* @date 2021-04-23 20:04:09
|
|
26
|
+
* @static
|
|
27
|
+
*/
|
|
28
|
+
static dataReg = /\$\{data.[a-zA-Z_$][a-zA-Z0-9_$]{1,}\}/g;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 填充字符串中的数据
|
|
32
|
+
*
|
|
33
|
+
* @author chitanda
|
|
34
|
+
* @date 2021-04-23 20:04:17
|
|
35
|
+
* @static
|
|
36
|
+
* @param {string} str
|
|
37
|
+
* @param {*} [context]
|
|
38
|
+
* @param {*} [data]
|
|
39
|
+
* @return {*} {string}
|
|
40
|
+
*/
|
|
41
|
+
static fill(str: string, context?: IContext, data?: IData): string {
|
|
42
|
+
if (notNilEmpty(str)) {
|
|
43
|
+
if (notNilEmpty(context)) {
|
|
44
|
+
const strArr = str.match(this.contextReg);
|
|
45
|
+
strArr?.forEach(_key => {
|
|
46
|
+
const key = _key.slice(10, _key.length - 1);
|
|
47
|
+
str = str.replace(`\${context.${key}}`, context![key] || '');
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (notNilEmpty(data)) {
|
|
51
|
+
const strArr = str.match(this.dataReg);
|
|
52
|
+
strArr?.forEach(_key => {
|
|
53
|
+
const key = _key.slice(7, _key.length - 1);
|
|
54
|
+
str = str.replace(`\${data.${key}}`, data![key] || '');
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return str;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 动态匹配${}
|
|
63
|
+
*
|
|
64
|
+
* @param str
|
|
65
|
+
* @param values
|
|
66
|
+
*/
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
68
|
+
static dynamicMatch(str: string, values: any): string {
|
|
69
|
+
return str.replace(/\${(.*?)}/g, (_, key) => {
|
|
70
|
+
const [objKey, propName] = key.split('.');
|
|
71
|
+
const obj = values[objKey];
|
|
72
|
+
return obj ? obj[propName] : '';
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* 设置延迟wait毫米后执行fun方法,返回fun的返回值
|
|
4
|
+
*
|
|
5
|
+
* @author lxm
|
|
6
|
+
* @date 2023-03-06 08:20:57
|
|
7
|
+
* @export
|
|
8
|
+
* @template T
|
|
9
|
+
* @param {number} wait
|
|
10
|
+
* @param {T} fun
|
|
11
|
+
* @param {any[]} params
|
|
12
|
+
* @returns {*} {Promise<ReturnType<T>>}
|
|
13
|
+
*/
|
|
14
|
+
export async function awaitTimeout<T extends (...args: any) => any>(
|
|
15
|
+
wait: number,
|
|
16
|
+
fun?: T,
|
|
17
|
+
params?: any[],
|
|
18
|
+
): Promise<ReturnType<T> | undefined> {
|
|
19
|
+
await new Promise(resolve => {
|
|
20
|
+
setTimeout(() => {
|
|
21
|
+
resolve(true);
|
|
22
|
+
}, wait);
|
|
23
|
+
});
|
|
24
|
+
if (fun) {
|
|
25
|
+
return fun(...(params || []));
|
|
26
|
+
}
|
|
27
|
+
}
|