@ibiz-template/core 0.0.1-alpha.4 → 0.0.1-alpha.40

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 (114) hide show
  1. package/dist/system/index.system.js +1 -0
  2. package/out/constant/http-status-message/http-status-message.d.ts +5 -0
  3. package/out/constant/http-status-message/http-status-message.d.ts.map +1 -0
  4. package/out/constant/http-status-message/http-status-message.js +20 -0
  5. package/out/constant/index.d.ts +4 -0
  6. package/out/constant/index.d.ts.map +1 -1
  7. package/out/constant/index.js +4 -0
  8. package/out/constant/login-mode/login-mode.d.ts +23 -0
  9. package/out/constant/login-mode/login-mode.d.ts.map +1 -0
  10. package/out/constant/login-mode/login-mode.js +24 -0
  11. package/out/constant/menu-permission-mode/menu-permission-mode.d.ts +23 -0
  12. package/out/constant/menu-permission-mode/menu-permission-mode.d.ts.map +1 -0
  13. package/out/constant/menu-permission-mode/menu-permission-mode.js +24 -0
  14. package/out/constant/util/util.d.ts +5 -0
  15. package/out/constant/util/util.d.ts.map +1 -0
  16. package/out/constant/util/util.js +5 -0
  17. package/out/context/index.d.ts +2 -39
  18. package/out/context/index.d.ts.map +1 -1
  19. package/out/context/index.js +34 -65
  20. package/out/environment/environment.d.ts.map +1 -1
  21. package/out/environment/environment.js +11 -1
  22. package/out/error/http-error/http-error.d.ts +18 -0
  23. package/out/error/http-error/http-error.d.ts.map +1 -0
  24. package/out/error/http-error/http-error.js +36 -0
  25. package/out/error/index.d.ts +3 -0
  26. package/out/error/index.d.ts.map +1 -0
  27. package/out/error/index.js +2 -0
  28. package/out/error/runtime-error/runtime-error.d.ts +15 -0
  29. package/out/error/runtime-error/runtime-error.d.ts.map +1 -0
  30. package/out/error/runtime-error/runtime-error.js +15 -0
  31. package/out/factory-center.d.ts +11 -0
  32. package/out/factory-center.d.ts.map +1 -0
  33. package/out/factory-center.js +10 -0
  34. package/out/ibizsys.d.ts +8 -1
  35. package/out/ibizsys.d.ts.map +1 -1
  36. package/out/ibizsys.js +8 -1
  37. package/out/index.d.ts +3 -1
  38. package/out/index.d.ts.map +1 -1
  39. package/out/index.js +3 -1
  40. package/out/install.d.ts.map +1 -1
  41. package/out/install.js +4 -2
  42. package/out/interface/click-outside/click-outside.d.ts +70 -0
  43. package/out/interface/click-outside/click-outside.d.ts.map +1 -0
  44. package/out/interface/click-outside/click-outside.js +1 -0
  45. package/out/interface/i-environment/i-environment.d.ts +76 -0
  46. package/out/interface/i-environment/i-environment.d.ts.map +1 -1
  47. package/out/interface/index.d.ts +1 -0
  48. package/out/interface/index.d.ts.map +1 -1
  49. package/out/interface/index.js +1 -1
  50. package/out/types.d.ts +20 -0
  51. package/out/types.d.ts.map +1 -1
  52. package/out/utils/click-outside/click-outside.d.ts +14 -0
  53. package/out/utils/click-outside/click-outside.d.ts.map +1 -0
  54. package/out/utils/click-outside/click-outside.js +85 -0
  55. package/out/utils/event/event.d.ts +34 -0
  56. package/out/utils/event/event.d.ts.map +1 -0
  57. package/out/utils/event/event.js +60 -0
  58. package/out/utils/index.d.ts +5 -1
  59. package/out/utils/index.d.ts.map +1 -1
  60. package/out/utils/index.js +5 -1
  61. package/out/utils/interceptor/core-interceptor.d.ts +15 -0
  62. package/out/utils/interceptor/core-interceptor.d.ts.map +1 -0
  63. package/out/utils/interceptor/core-interceptor.js +30 -0
  64. package/out/utils/interceptor/index.d.ts +3 -0
  65. package/out/utils/interceptor/index.d.ts.map +1 -0
  66. package/out/utils/interceptor/index.js +2 -0
  67. package/out/utils/interceptor/interceptor.d.ts +74 -3
  68. package/out/utils/interceptor/interceptor.d.ts.map +1 -1
  69. package/out/utils/interceptor/interceptor.js +73 -22
  70. package/out/utils/logger/logger.d.ts +3 -0
  71. package/out/utils/logger/logger.d.ts.map +1 -0
  72. package/out/utils/logger/logger.js +2 -0
  73. package/out/utils/net/http-response.d.ts +4 -9
  74. package/out/utils/net/http-response.d.ts.map +1 -1
  75. package/out/utils/net/http-response.js +0 -7
  76. package/out/utils/net/net.d.ts +82 -31
  77. package/out/utils/net/net.d.ts.map +1 -1
  78. package/out/utils/net/net.js +145 -50
  79. package/out/utils/url-helper/url-helper.d.ts +51 -0
  80. package/out/utils/url-helper/url-helper.d.ts.map +1 -0
  81. package/out/utils/url-helper/url-helper.js +59 -0
  82. package/out/utils/util/util.d.ts +45 -0
  83. package/out/utils/util/util.d.ts.map +1 -1
  84. package/out/utils/util/util.js +96 -0
  85. package/package.json +18 -7
  86. package/src/constant/http-status-message/http-status-message.ts +20 -0
  87. package/src/constant/index.ts +4 -0
  88. package/src/constant/login-mode/login-mode.ts +23 -0
  89. package/src/constant/menu-permission-mode/menu-permission-mode.ts +23 -0
  90. package/src/constant/util/util.ts +5 -0
  91. package/src/context/index.ts +35 -69
  92. package/src/environment/environment.ts +11 -1
  93. package/src/error/http-error/http-error.ts +43 -0
  94. package/src/error/index.ts +2 -0
  95. package/src/error/runtime-error/runtime-error.ts +14 -0
  96. package/src/factory-center.ts +9 -0
  97. package/src/ibizsys.ts +9 -1
  98. package/src/index.ts +3 -1
  99. package/src/install.ts +4 -2
  100. package/src/interface/click-outside/click-outside.ts +71 -0
  101. package/src/interface/i-environment/i-environment.ts +86 -0
  102. package/src/interface/index.ts +1 -0
  103. package/src/types.ts +21 -0
  104. package/src/utils/click-outside/click-outside.ts +110 -0
  105. package/src/utils/event/event.ts +77 -0
  106. package/src/utils/index.ts +5 -1
  107. package/src/utils/interceptor/core-interceptor.ts +35 -0
  108. package/src/utils/interceptor/index.ts +2 -0
  109. package/src/utils/interceptor/interceptor.ts +109 -22
  110. package/src/utils/logger/logger.ts +3 -0
  111. package/src/utils/net/http-response.ts +6 -9
  112. package/src/utils/net/net.ts +175 -61
  113. package/src/utils/url-helper/url-helper.ts +62 -0
  114. package/src/utils/util/util.ts +115 -0
@@ -1,7 +1,5 @@
1
- import axios from 'axios';
2
- import { getToken } from '../util/util';
3
1
  /**
4
- * 默认请求拦截器
2
+ * 拦截器基类
5
3
  *
6
4
  * @author chitanda
7
5
  * @date 2022-07-20 18:07:11
@@ -9,25 +7,78 @@ import { getToken } from '../util/util';
9
7
  * @class Interceptor
10
8
  */
11
9
  export class Interceptor {
12
- constructor() {
13
- this.init();
10
+ /**
11
+ * 请求之前处理
12
+ *
13
+ * @author lxm
14
+ * @date 2022-10-27 17:10:41
15
+ * @protected
16
+ * @param {AxiosRequestConfig} config
17
+ * @returns {*} {Promise<AxiosRequestConfig>}
18
+ */
19
+ async onBeforeRequest(config) {
20
+ return config;
14
21
  }
15
- init() {
16
- axios.interceptors.request.use(config => {
17
- if (!config.headers) {
18
- config.headers = {};
19
- }
20
- config.headers.Authorization = `Bearer ${getToken()}`;
21
- const { orgData } = ibiz;
22
- if (orgData) {
23
- if (orgData.systemid) {
24
- config.headers.srfsystemid = orgData.systemid;
25
- }
26
- if (orgData.orgid) {
27
- config.headers.srforgid = orgData.orgid;
28
- }
29
- }
30
- return config;
31
- });
22
+ /**
23
+ * 请求失败之后处理
24
+ *
25
+ * @author lxm
26
+ * @date 2022-10-27 17:10:40
27
+ * @protected
28
+ * @param {*} error
29
+ */
30
+ onRequestError(error) {
31
+ // 处理请求错误
32
+ return Promise.reject(error);
33
+ }
34
+ /**
35
+ * 响应成功之后处理
36
+ *
37
+ * @author lxm
38
+ * @date 2022-10-27 17:10:38
39
+ * @protected
40
+ * @param {AxiosResponse} config
41
+ * @returns {*} {Promise<AxiosRequestConfig>}
42
+ */
43
+ async onResponseSuccess(config) {
44
+ return config;
45
+ }
46
+ /**
47
+ * 响应失败之后处理
48
+ *
49
+ * @author lxm
50
+ * @date 2022-10-27 17:10:37
51
+ * @protected
52
+ * @param {*} _error
53
+ */
54
+ onResponseError(error) {
55
+ // 处理响应错误
56
+ return Promise.reject(error);
57
+ }
58
+ /**
59
+ * 使用拦截器
60
+ *
61
+ * @author lxm
62
+ * @date 2022-10-27 17:10:28
63
+ * @param {AxiosInstance} instance
64
+ */
65
+ use(instance) {
66
+ this.requestTag = instance.interceptors.request.use(this.onBeforeRequest, this.onRequestError);
67
+ this.responseTag = instance.interceptors.response.use(this.onResponseSuccess, this.onResponseError);
68
+ }
69
+ /**
70
+ * 移出拦截器
71
+ *
72
+ * @author lxm
73
+ * @date 2022-10-27 17:10:27
74
+ * @param {AxiosInstance} instance
75
+ */
76
+ eject(instance) {
77
+ if (this.requestTag) {
78
+ instance.interceptors.request.eject(this.requestTag);
79
+ }
80
+ if (this.responseTag) {
81
+ instance.interceptors.response.eject(this.responseTag);
82
+ }
32
83
  }
33
84
  }
@@ -0,0 +1,3 @@
1
+ import * as Logger from 'loglevel';
2
+ export { Logger };
3
+ //# sourceMappingURL=logger.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../../../src/utils/logger/logger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AAEnC,OAAO,EAAE,MAAM,EAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ import * as Logger from 'loglevel';
2
+ export { Logger };
@@ -27,7 +27,7 @@ export interface IHttpResponse<T = IData> extends AxiosResponse {
27
27
  * @date 2022-08-18 15:08:44
28
28
  * @type {boolean}
29
29
  */
30
- local?: boolean;
30
+ local: boolean;
31
31
  data: T;
32
32
  /**
33
33
  * 当前页
@@ -64,14 +64,9 @@ export declare class HttpResponse<T = IData> implements IHttpResponse<T> {
64
64
  data: T;
65
65
  status: number;
66
66
  statusText: string;
67
- /**
68
- * header 本地仿造时值为空
69
- *
70
- * @author chitanda
71
- * @date 2022-08-18 15:08:46
72
- * @type {AxiosResponseHeaders}
73
- */
74
- headers: AxiosResponseHeaders;
67
+ headers: AxiosResponseHeaders | Partial<Record<string, string> & {
68
+ 'set-cookie'?: string[] | undefined;
69
+ }>;
75
70
  /**
76
71
  * AxiosResponse 本地仿造时值为空
77
72
  *
@@ -1 +1 @@
1
- {"version":3,"file":"http-response.d.ts","sourceRoot":"","sources":["../../../src/utils/net/http-response.ts"],"names":[],"mappings":"AAAA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AOG;IACH,KAAK,EAAE,OAAO,CAAC;IAEf,IAAI,EAAE,CAAC,CAAC;IAER;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;;GASG;AACH,qBAAa,YAAY,CAAC,CAAC,GAAG,KAAK,CAAE,YAAW,aAAa,CAAC,CAAC,CAAC;IAC9D;;;;;OAKG;IACH,KAAK,UAAQ;IAEb,EAAE,UAAS;IAEX,IAAI,EAAE,CAAC,CAAC;IAER,MAAM,EAAE,MAAM,CAAC;IAEf,UAAU,EAAE,MAAM,CAAC;IAEnB,OAAO,EACH,oBAAoB,GACpB,OAAO,CACL,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG;QAAE,YAAY,CAAC,EAAE,MAAM,EAAE,GAAG,SAAS,CAAA;KAAE,CACjE,CAAM;IAEX;;;;;;OAMG;IACH,MAAM,EAAE,kBAAkB,CAAC,KAAK,CAAC,CAAM;IAEvC;;;;;;;;OAQG;gBACS,IAAI,CAAC,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;CAQjE"}
@@ -27,13 +27,6 @@ export class HttpResponse {
27
27
  */
28
28
  this.local = true;
29
29
  this.ok = false;
30
- /**
31
- * header 本地仿造时值为空
32
- *
33
- * @author chitanda
34
- * @date 2022-08-18 15:08:46
35
- * @type {AxiosResponseHeaders}
36
- */
37
30
  this.headers = {};
38
31
  /**
39
32
  * AxiosResponse 本地仿造时值为空
@@ -1,4 +1,5 @@
1
- import { AxiosRequestHeaders } from 'axios';
1
+ import { RawAxiosRequestHeaders, AxiosRequestConfig, AxiosInstance, CreateAxiosDefaults } from 'axios';
2
+ import { Interceptor } from '../interceptor/interceptor';
2
3
  import { IHttpResponse } from './http-response';
3
4
  /**
4
5
  * 全局请求工具类
@@ -10,73 +11,133 @@ import { IHttpResponse } from './http-response';
10
11
  */
11
12
  export declare class Net {
12
13
  /**
13
- * 标准请求前缀
14
+ * axios实例
14
15
  *
15
- * @author chitanda
16
- * @date 2022-07-21 10:07:48
16
+ * @author lxm
17
+ * @date 2022-10-27 17:10:18
17
18
  * @protected
18
- * @type {string}
19
+ * @type {AxiosInstance}
19
20
  */
20
- protected _prefix: string;
21
- get prefix(): string;
21
+ protected instance: AxiosInstance;
22
22
  /**
23
23
  * Creates an instance of Net.
24
+ * @author lxm
25
+ * @date 2022-10-27 16:10:05
26
+ * @param {CreateAxiosDefaults} [config] 创建实例用的默认配置
27
+ */
28
+ constructor(config?: CreateAxiosDefaults);
29
+ /**
30
+ * 注册的拦截器
24
31
  *
25
- * @author chitanda
26
- * @date 2022-07-21 10:07:27
27
- * @param {string} [prefix] 请求前缀
32
+ * @author lxm
33
+ * @date 2022-10-27 17:10:18
34
+ * @type {Map<string, Interceptor>}
35
+ */
36
+ interceptors: Map<string, Interceptor>;
37
+ /**
38
+ * 添加拦截器
39
+ *
40
+ * @author lxm
41
+ * @date 2022-10-27 17:10:42
42
+ * @param {string} name 唯一标识
43
+ * @param {Interceptor} interceptor 拦截器
44
+ */
45
+ addInterceptor(name: string, interceptor: Interceptor): void;
46
+ /**
47
+ * 删除拦截器
48
+ *
49
+ * @author lxm
50
+ * @date 2022-10-27 17:10:27
51
+ * @param {string} name 唯一标识
52
+ */
53
+ removeInterceptor(name: string): void;
54
+ /**
55
+ * 预置config,绑定动态的配置
56
+ *
57
+ * @author lxm
58
+ * @date 2022-10-27 16:10:48
59
+ * @readonly
60
+ * @protected
61
+ * @type {AxiosRequestConfig}
28
62
  */
29
- constructor(prefix?: string);
63
+ protected get presetConfig(): AxiosRequestConfig;
64
+ /**
65
+ * 从左到右递归合并配置参数(内置第一个合并的预置参数)
66
+ *
67
+ * @author lxm
68
+ * @date 2022-10-27 16:10:09
69
+ * @protected
70
+ * @param {...AxiosRequestConfig[]} configs
71
+ * @returns {*}
72
+ */
73
+ protected mergeConfig(...configs: AxiosRequestConfig[]): any;
30
74
  /**
31
75
  * Post 请求
32
76
  *
33
77
  * @author chitanda
34
- * @date 2022-07-14 15:07:01
78
+ * @date 2022-10-19 11:10:30
35
79
  * @param {string} url
80
+ * @param {IData} data
36
81
  * @param {IParams} [params={}]
82
+ * @param {RawAxiosRequestHeaders} [headers={}]
37
83
  * @return {*} {Promise<IHttpResponse>}
38
84
  */
39
- post(url: string, data: IData, params?: IParams, headers?: AxiosRequestHeaders): Promise<IHttpResponse>;
85
+ post(url: string, data: IData, params?: IParams, headers?: RawAxiosRequestHeaders): Promise<IHttpResponse>;
40
86
  /**
41
87
  * Get 请求
42
88
  *
43
89
  * @author chitanda
44
- * @date 2022-07-14 15:07:08
90
+ * @date 2022-10-19 11:10:24
45
91
  * @param {string} url
46
92
  * @param {IParams} [params={}]
93
+ * @param {RawAxiosRequestHeaders} [headers={}]
94
+ * @param {IParams} [option={}]
47
95
  * @return {*} {Promise<IHttpResponse>}
48
96
  */
49
- get(url: string, params?: IParams, headers?: AxiosRequestHeaders, option?: IParams): Promise<IHttpResponse>;
97
+ get(url: string, params?: IParams, headers?: RawAxiosRequestHeaders, option?: AxiosRequestConfig): Promise<IHttpResponse>;
50
98
  /**
51
99
  * Delete 请求
52
100
  *
53
101
  * @author chitanda
54
- * @date 2022-07-14 15:07:43
102
+ * @date 2022-10-19 11:10:17
55
103
  * @param {string} url
56
104
  * @param {IParams} [params]
105
+ * @param {RawAxiosRequestHeaders} [headers={}]
57
106
  * @return {*} {Promise<IHttpResponse>}
58
107
  */
59
- delete(url: string, params?: IParams, headers?: AxiosRequestHeaders): Promise<IHttpResponse>;
108
+ delete(url: string, params?: IParams, headers?: RawAxiosRequestHeaders): Promise<IHttpResponse>;
60
109
  /**
61
110
  * Put 请求
62
111
  *
63
112
  * @author chitanda
64
- * @date 2022-07-14 15:07:49
113
+ * @date 2022-10-19 11:10:11
65
114
  * @param {string} url
66
115
  * @param {IData} data
116
+ * @param {IParams} [params={}]
117
+ * @param {RawAxiosRequestHeaders} [headers={}]
67
118
  * @return {*} {Promise<IHttpResponse>}
68
119
  */
69
- put(url: string, data: IData, params?: IParams, headers?: AxiosRequestHeaders): Promise<IHttpResponse>;
120
+ put(url: string, data: IData, params?: IParams, headers?: RawAxiosRequestHeaders): Promise<IHttpResponse>;
70
121
  /**
71
122
  * 获取模型数据
72
123
  *
73
124
  * @author chitanda
74
125
  * @date 2022-07-14 15:07:15
75
126
  * @param {string} url
76
- * @param {AxiosRequestHeaders} [headers={}]
127
+ * @param {RawAxiosRequestHeaders} [headers={}]
77
128
  * @return {*} {Promise<IHttpResponse>}
78
129
  */
79
- getModel(url: string, headers?: AxiosRequestHeaders): Promise<IHttpResponse>;
130
+ getModel(url: string, headers?: RawAxiosRequestHeaders): Promise<IHttpResponse>;
131
+ /**
132
+ * 基础请求方法,会合并预置配置
133
+ *
134
+ * @author lxm
135
+ * @date 2022-10-27 14:10:06
136
+ * @param {string} url
137
+ * @param {AxiosRequestConfig} [config={}]
138
+ * @returns {*} {Promise<IHttpResponse>}
139
+ */
140
+ request(url: string, config?: AxiosRequestConfig): Promise<IHttpResponse>;
80
141
  /**
81
142
  * 统一处理请求返回
82
143
  *
@@ -109,15 +170,5 @@ export declare class Net {
109
170
  * @return {*} {string}
110
171
  */
111
172
  private attachUrlParam;
112
- /**
113
- * 处理 url 参数
114
- *
115
- * @author chitanda
116
- * @date 2022-07-20 20:07:17
117
- * @private
118
- * @param {string} url
119
- * @return {*} {string}
120
- */
121
- private attachUrl;
122
173
  }
123
174
  //# sourceMappingURL=net.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"net.d.ts","sourceRoot":"","sources":["../../../src/utils/net/net.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,mBAAmB,EAAiB,MAAM,OAAO,CAAC;AAGlE,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhD;;;;;;;GAOG;AACH,qBAAa,GA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,CAAC;IAczB;;;;;;;;OAQG;IACG,QAAQ,CACZ,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,sBAA2B,GACnC,OAAO,CAAC,aAAa,CAAC;IAWzB;;;;;;;;OAQG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,MAAM,GAAE,kBAAuB,GAC9B,OAAO,CAAC,aAAa,CAAC;IAWzB;;;;;;;;OAQG;IACH,OAAO,CAAC,gBAAgB;IAQxB;;;;;;;;;OASG;IACH,OAAO,CAAC,oBAAoB;IAc5B;;;;;;;;;OASG;IACH,OAAO,CAAC,cAAc;CAevB"}
@@ -1,6 +1,8 @@
1
1
  import axios from 'axios';
2
+ import { merge } from 'lodash-es';
2
3
  import { stringify } from 'qs';
3
4
  import { notNilEmpty } from 'qx-util';
5
+ import { HttpError } from '../../error';
4
6
  /**
5
7
  * 全局请求工具类
6
8
  *
@@ -12,54 +14,115 @@ import { notNilEmpty } from 'qx-util';
12
14
  export class Net {
13
15
  /**
14
16
  * Creates an instance of Net.
15
- *
16
- * @author chitanda
17
- * @date 2022-07-21 10:07:27
18
- * @param {string} [prefix] 请求前缀
17
+ * @author lxm
18
+ * @date 2022-10-27 16:10:05
19
+ * @param {CreateAxiosDefaults} [config] 创建实例用的默认配置
19
20
  */
20
- constructor(prefix) {
21
+ constructor(config) {
21
22
  /**
22
- * 标准请求前缀
23
+ * 注册的拦截器
23
24
  *
24
- * @author chitanda
25
- * @date 2022-07-21 10:07:48
26
- * @protected
27
- * @type {string}
25
+ * @author lxm
26
+ * @date 2022-10-27 17:10:18
27
+ * @type {Map<string, Interceptor>}
28
28
  */
29
- this._prefix = '';
30
- if (prefix) {
31
- this._prefix = prefix;
29
+ this.interceptors = new Map();
30
+ this.instance = axios.create(config);
31
+ }
32
+ /**
33
+ * 添加拦截器
34
+ *
35
+ * @author lxm
36
+ * @date 2022-10-27 17:10:42
37
+ * @param {string} name 唯一标识
38
+ * @param {Interceptor} interceptor 拦截器
39
+ */
40
+ addInterceptor(name, interceptor) {
41
+ interceptor.use(this.instance);
42
+ this.interceptors.set(name, interceptor);
43
+ }
44
+ /**
45
+ * 删除拦截器
46
+ *
47
+ * @author lxm
48
+ * @date 2022-10-27 17:10:27
49
+ * @param {string} name 唯一标识
50
+ */
51
+ removeInterceptor(name) {
52
+ const interceptor = this.interceptors.get(name);
53
+ if (interceptor) {
54
+ interceptor.eject(this.instance);
55
+ this.interceptors.delete(name);
32
56
  }
33
57
  }
34
- get prefix() {
35
- return this._prefix || ibiz.env.baseUrl;
58
+ /**
59
+ * 预置config,绑定动态的配置
60
+ *
61
+ * @author lxm
62
+ * @date 2022-10-27 16:10:48
63
+ * @readonly
64
+ * @protected
65
+ * @type {AxiosRequestConfig}
66
+ */
67
+ get presetConfig() {
68
+ return {
69
+ // 请求前缀路径
70
+ baseURL: this.instance.defaults.baseURL || ibiz.env.baseUrl,
71
+ headers: {
72
+ 'Content-Type': 'application/json;charset=UTF-8',
73
+ Accept: 'application/json',
74
+ },
75
+ };
76
+ }
77
+ /**
78
+ * 从左到右递归合并配置参数(内置第一个合并的预置参数)
79
+ *
80
+ * @author lxm
81
+ * @date 2022-10-27 16:10:09
82
+ * @protected
83
+ * @param {...AxiosRequestConfig[]} configs
84
+ * @returns {*}
85
+ */
86
+ mergeConfig(...configs) {
87
+ if (configs.length === 0) {
88
+ return this.presetConfig;
89
+ }
90
+ return merge(this.presetConfig, ...configs);
36
91
  }
37
92
  /**
38
93
  * Post 请求
39
94
  *
40
95
  * @author chitanda
41
- * @date 2022-07-14 15:07:01
96
+ * @date 2022-10-19 11:10:30
42
97
  * @param {string} url
98
+ * @param {IData} data
43
99
  * @param {IParams} [params={}]
100
+ * @param {RawAxiosRequestHeaders} [headers={}]
44
101
  * @return {*} {Promise<IHttpResponse>}
45
102
  */
46
103
  async post(url, data, params = {}, headers = {}) {
47
104
  url = this.handleAppPresetParam(url, params);
48
- const response = await axios({
49
- method: 'post',
50
- url,
51
- data,
52
- headers: Object.assign({ 'Content-Type': 'application/json;charset=UTF-8', Accept: 'application/json' }, headers),
53
- });
54
- return this.doResponseResult(response);
105
+ try {
106
+ const response = await this.request(url, {
107
+ method: 'post',
108
+ data,
109
+ headers,
110
+ });
111
+ return this.doResponseResult(response);
112
+ }
113
+ catch (error) {
114
+ throw new HttpError(error);
115
+ }
55
116
  }
56
117
  /**
57
118
  * Get 请求
58
119
  *
59
120
  * @author chitanda
60
- * @date 2022-07-14 15:07:08
121
+ * @date 2022-10-19 11:10:24
61
122
  * @param {string} url
62
123
  * @param {IParams} [params={}]
124
+ * @param {RawAxiosRequestHeaders} [headers={}]
125
+ * @param {IParams} [option={}]
63
126
  * @return {*} {Promise<IHttpResponse>}
64
127
  */
65
128
  async get(url, params = {}, headers = {}, option = {}) {
@@ -68,36 +131,58 @@ export class Net {
68
131
  // delete params.srfparentdata;
69
132
  // }
70
133
  url = this.attachUrlParam(url, params);
71
- const response = await axios.get(url, Object.assign({ headers }, option));
72
- return this.doResponseResult(response);
134
+ try {
135
+ const response = await this.request(url, merge({ method: 'get', headers }, option));
136
+ return this.doResponseResult(response);
137
+ }
138
+ catch (error) {
139
+ throw new HttpError(error);
140
+ }
73
141
  }
74
142
  /**
75
143
  * Delete 请求
76
144
  *
77
145
  * @author chitanda
78
- * @date 2022-07-14 15:07:43
146
+ * @date 2022-10-19 11:10:17
79
147
  * @param {string} url
80
148
  * @param {IParams} [params]
149
+ * @param {RawAxiosRequestHeaders} [headers={}]
81
150
  * @return {*} {Promise<IHttpResponse>}
82
151
  */
83
152
  async delete(url, params, headers = {}) {
84
153
  url = this.handleAppPresetParam(url, params);
85
- const response = await axios.delete(url, { headers });
86
- return this.doResponseResult(response);
154
+ try {
155
+ const response = await this.request(url, { method: 'delete', headers });
156
+ return this.doResponseResult(response);
157
+ }
158
+ catch (error) {
159
+ throw new HttpError(error);
160
+ }
87
161
  }
88
162
  /**
89
163
  * Put 请求
90
164
  *
91
165
  * @author chitanda
92
- * @date 2022-07-14 15:07:49
166
+ * @date 2022-10-19 11:10:11
93
167
  * @param {string} url
94
168
  * @param {IData} data
169
+ * @param {IParams} [params={}]
170
+ * @param {RawAxiosRequestHeaders} [headers={}]
95
171
  * @return {*} {Promise<IHttpResponse>}
96
172
  */
97
173
  async put(url, data, params = {}, headers = {}) {
98
174
  url = this.handleAppPresetParam(url, params);
99
- const response = await axios.put(url, data, { headers });
100
- return this.doResponseResult(response);
175
+ try {
176
+ const response = await this.request(url, {
177
+ method: 'put',
178
+ data,
179
+ headers,
180
+ });
181
+ return this.doResponseResult(response);
182
+ }
183
+ catch (error) {
184
+ throw new HttpError(error);
185
+ }
101
186
  }
102
187
  /**
103
188
  * 获取模型数据
@@ -105,14 +190,37 @@ export class Net {
105
190
  * @author chitanda
106
191
  * @date 2022-07-14 15:07:15
107
192
  * @param {string} url
108
- * @param {AxiosRequestHeaders} [headers={}]
193
+ * @param {RawAxiosRequestHeaders} [headers={}]
109
194
  * @return {*} {Promise<IHttpResponse>}
110
195
  */
111
196
  async getModel(url, headers = {}) {
112
- const response = await axios.get(url, {
113
- headers,
114
- });
115
- return this.doResponseResult(response);
197
+ try {
198
+ const response = await this.instance.get(url, {
199
+ headers,
200
+ });
201
+ return this.doResponseResult(response);
202
+ }
203
+ catch (error) {
204
+ throw new HttpError(error);
205
+ }
206
+ }
207
+ /**
208
+ * 基础请求方法,会合并预置配置
209
+ *
210
+ * @author lxm
211
+ * @date 2022-10-27 14:10:06
212
+ * @param {string} url
213
+ * @param {AxiosRequestConfig} [config={}]
214
+ * @returns {*} {Promise<IHttpResponse>}
215
+ */
216
+ async request(url, config = {}) {
217
+ try {
218
+ const response = await this.instance.request(this.mergeConfig({ url }, config));
219
+ return this.doResponseResult(response);
220
+ }
221
+ catch (error) {
222
+ throw new HttpError(error);
223
+ }
116
224
  }
117
225
  /**
118
226
  * 统一处理请求返回
@@ -164,7 +272,6 @@ export class Net {
164
272
  * @return {*} {string}
165
273
  */
166
274
  attachUrlParam(url, params) {
167
- url = this.attachUrl(url);
168
275
  const strParams = stringify(params);
169
276
  if (notNilEmpty(strParams)) {
170
277
  if (url.endsWith('?')) {
@@ -182,16 +289,4 @@ export class Net {
182
289
  }
183
290
  return url;
184
291
  }
185
- /**
186
- * 处理 url 参数
187
- *
188
- * @author chitanda
189
- * @date 2022-07-20 20:07:17
190
- * @private
191
- * @param {string} url
192
- * @return {*} {string}
193
- */
194
- attachUrl(url) {
195
- return `${this.prefix}${url}`;
196
- }
197
292
  }