@hzab/data-model 1.8.9-alpha.1 → 2.0.0-alpha.0

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.
@@ -1,422 +0,0 @@
1
- import _ from "lodash";
2
-
3
- import { objToFormData, formDataToObj, _$Temp } from "./utils";
4
- import ArrayUtils, { STATE_CODE } from "./ArrayUtils";
5
- const errMsg = {
6
- 404: "未找到对应子项",
7
- 501: "未传入表单id",
8
- };
9
- /**
10
- * ArrayDataModel 本地数据 DataModel
11
- */
12
- class ArrayDataModel extends ArrayUtils {
13
- constructor(params) {
14
- super(params);
15
- const {
16
- ctx,
17
- query,
18
- createApi,
19
- createMap,
20
- getApi,
21
- getMap,
22
- getListApi,
23
- getListMap,
24
- getListFunc,
25
- updateApi,
26
- updateMap,
27
- patchApi,
28
- patchMap,
29
- deleteApi,
30
- multipleDeleteApi,
31
- } = params;
32
-
33
- /** 请求 url 替换的额外参数 */
34
- this.ctx = ctx || {};
35
- /** GET 请求的参数 */
36
- this.query = query || {};
37
-
38
- /** POST 接口地址 */
39
- this.createApi = createApi;
40
- /** POST 接口提交前的数据处理回调 record => (record) */
41
- this.createMap = createMap;
42
- /** GET 详情 接口地址 */
43
- this.getApi = getApi;
44
- /** GET 详情 接口返回后的数据处理回调 record => (record) */
45
- this.getMap = getMap;
46
- /** GET 列表 接口地址 */
47
- this.getListApi = getListApi;
48
- /** GET 列表 接口返回后的数据处理回调 record => (record) */
49
- this.getListMap = getListMap;
50
- /** GET 列表 接口回调,用于自定义列表请求接口。query => ({list: [], pagination: { total: 0 }}) 优先级高于 getListApi */
51
- this.getListFunc = getListFunc;
52
- /** PUT 接口地址 */
53
- this.updateApi = updateApi;
54
- /** PUT 接口提交前的数据处理回调 record => (record) */
55
- this.updateMap = updateMap;
56
- /** PATCH 接口地址 */
57
- this.patchApi = patchApi;
58
- /** PATCH 接口提交前的数据处理回调 record => (record) */
59
- this.patchMap = patchMap;
60
- /** DELETE 接口地址 */
61
- this.deleteApi = deleteApi;
62
- /** DELETE 批量删除 接口地址 */
63
- this.multipleDeleteApi = multipleDeleteApi;
64
-
65
- const {
66
- getListReqMap,
67
- getListResMap,
68
- getReqMap,
69
- getResMap,
70
- createReqMap,
71
- createResMap,
72
- updateReqMap,
73
- updateResMap,
74
- patchReqMap,
75
- patchResMap,
76
- deleteReqMap,
77
- deleteResMap,
78
- multipleDeleteReqMap,
79
- multipleDeleteResMap,
80
- } = params || {};
81
-
82
- /** GET 列表 接口请求前数据处理回调 */
83
- this.getListReqMap = getListReqMap;
84
- /** GET 列表 接口请求结果数据处理回调 */
85
- this.getListResMap = getListResMap;
86
- /** GET 详情 接口请求前数据处理回调 */
87
- this.getReqMap = getReqMap;
88
- /** GET 详情 接口请求结果数据处理回调 */
89
- this.getResMap = getResMap;
90
- /** POST 接口请求前数据处理回调 */
91
- this.createReqMap = createReqMap;
92
- /** POST 接口请求结果数据处理回调 */
93
- this.createResMap = createResMap;
94
- /** PUT 接口请求前数据处理回调 */
95
- this.updateReqMap = updateReqMap;
96
- /** PUT 接口请求结果数据处理回调 */
97
- this.updateResMap = updateResMap;
98
- /** PATCH 接口请求前数据处理回调 */
99
- this.patchReqMap = patchReqMap;
100
- /** PATCH 接口请求结果数据处理回调 */
101
- this.patchResMap = patchResMap;
102
- /** DELETE 接口请求前数据处理回调 */
103
- this.deleteReqMap = deleteReqMap;
104
- /** DELETE 接口请求结果数据处理回调 */
105
- this.deleteResMap = deleteResMap;
106
- /** DELETE 批量删除 接口请求前数据处理回调 */
107
- this.multipleDeleteReqMap = multipleDeleteReqMap;
108
- /** DELETE 批量删除 接口请求结果数据处理回调 */
109
- this.multipleDeleteResMap = multipleDeleteResMap;
110
-
111
- const {
112
- /**
113
- *
114
- * @param {Object} [options={}] - 查询选项
115
- * @param {boolean} [options.fuzzy=false] - 字符串字段是否开启模糊匹配
116
- * @param {boolean} [options.ignoreCase=true] - 模糊匹配时是否忽略大小写
117
- */
118
- arrOptions,
119
- } = params || {};
120
- this._arrOptions = arrOptions;
121
- }
122
-
123
- /**
124
- * GET 详情请求
125
- * @param {Object} q query 参数
126
- * @returns
127
- */
128
- get(q = {}) {
129
- let query = _.merge({}, this.query, q);
130
- query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
131
-
132
- if (this.getReqMap) {
133
- query = this.getReqMap(query);
134
- }
135
-
136
- return new Promise((resolve, reject) => {
137
- let res = this.findItem(query, this._arrOptions);
138
- if (this.getMap) {
139
- res = this.getMap(res);
140
- }
141
- if (this.getResMap) {
142
- res = this.getResMap(res);
143
- }
144
- resolve(res);
145
- });
146
- }
147
-
148
- /**
149
- * GET 列表请求
150
- * @param {Object} q query 参数
151
- * @returns
152
- */
153
- async getList(q) {
154
- let query = _.merge({}, this.query, q);
155
- query = _.pickBy(query, (val) => !_.isNil(val) && val !== "");
156
-
157
- if (this.getListReqMap) {
158
- query = this.getListReqMap(query);
159
- }
160
-
161
- let resultList = null;
162
- if (this.getListFunc) {
163
- resultList = await this.getListFunc(query);
164
- } else {
165
- const getPro = this.findListByQuery(query, this._arrOptions);
166
- resultList = await getPro;
167
- }
168
- if (this.getListMap) {
169
- resultList.list = resultList.list.map((record) => this.getListMap(record));
170
- }
171
- if (this.getListResMap) {
172
- resultList = this.getListResMap(resultList);
173
- }
174
- return resultList;
175
- }
176
-
177
- /**
178
- * POST 请求
179
- * @param {Object} params 参数
180
- * @returns
181
- */
182
- create(params) {
183
- return new Promise((resolve, reject) => {
184
- const _params = this.handleInputParams(params, this.createReqMap);
185
- const res = this.pushItem(_params);
186
- if (res === STATE_CODE.SUC) {
187
- let _res = {};
188
- if (this.createResMap) {
189
- _res = this.createResMap(res);
190
- }
191
- resolve(_res);
192
- } else {
193
- reject({
194
- code: res,
195
- _message: errMsg[res] || "未知错误",
196
- });
197
- }
198
- });
199
- }
200
-
201
- /**
202
- * PUT 请求
203
- * @param {Object} params 参数
204
- * @returns
205
- */
206
- update(params) {
207
- return new Promise((resolve, reject) => {
208
- const _params = this.handleInputParams(params, this.updateReqMap);
209
- const res = this.replaceItem(_params);
210
- if (res === STATE_CODE.SUC) {
211
- let _res = {};
212
- if (this.updateResMap) {
213
- _res = this.updateResMap(res);
214
- }
215
- resolve(_res);
216
- } else {
217
- reject({
218
- code: res,
219
- _message: errMsg[res] || "未知错误",
220
- });
221
- }
222
- });
223
- }
224
-
225
- /**
226
- * PATCH 请求
227
- * @param {Object} params 参数
228
- * @returns
229
- */
230
- patch(params) {
231
- return new Promise((resolve, reject) => {
232
- const _params = this.handleInputParams(params, this.patchReqMap);
233
- const res = this.updateItemValue(_params);
234
- if (res === STATE_CODE.SUC) {
235
- let _res = {};
236
- if (this.patchResMap) {
237
- _res = this.patchResMap(res);
238
- }
239
- resolve(_res);
240
- } else {
241
- reject({
242
- code: res,
243
- _message: errMsg[res] || "未知错误",
244
- });
245
- }
246
- });
247
- }
248
-
249
- /**
250
- * 删除接口
251
- * @param {*} config axios.delete config 参数,
252
- * @param {*} config.params axios.delete url 参数,
253
- * @param {*} config.data axios.delete data 参数,
254
- * @param {*} ctx
255
- * @returns
256
- */
257
- delete(config, ctx) {
258
- let _config = _.cloneDeep(config) || {};
259
- if (this.deleteReqMap) {
260
- _config = this.deleteReqMap(_config);
261
- }
262
- return new Promise((resolve, reject) => {
263
- const { _idKey } = this;
264
- const id = config[_idKey] || config.params?.[_idKey] || config.data?.[_idKey] || ctx[_idKey];
265
- const res = this.delItem(id);
266
- if (res === STATE_CODE.SUC) {
267
- let _res = {};
268
- if (this.deleteResMap) {
269
- _res = this.deleteResMap(res);
270
- }
271
- resolve(_res);
272
- } else {
273
- reject({
274
- code: res,
275
- _message: errMsg[res] || "未知错误",
276
- });
277
- }
278
- });
279
- }
280
-
281
- /**
282
- * 批量删除接口
283
- * @param {*} config axios.delete config 参数,
284
- * @param {*} config.params axios.delete url 参数,
285
- * @param {*} config.data axios.delete data 参数,
286
- * @param {*} ctx
287
- * @returns
288
- */
289
- multipleDelete(config, ctx) {
290
- let _config = _.cloneDeep(config) || {};
291
- if (this.multipleDeleteReqMap) {
292
- _config = this.multipleDeleteReqMap(_config);
293
- }
294
- return new Promise((resolve, reject) => {
295
- const { _idKey } = this;
296
- let ids = config[_idKey] || config.params?.[_idKey] || config.data?.[_idKey] || ctx[_idKey];
297
-
298
- if (typeof ids === "string") {
299
- ids = ids.split(",");
300
- }
301
-
302
- if (Array.isArray(ids)) {
303
- const sucList = [];
304
- const failList = [];
305
- ids.forEach((id) => {
306
- try {
307
- const res = this.delItem(id);
308
- if (res === STATE_CODE.SUC) {
309
- sucList.push(id);
310
- } else {
311
- failList.push(id);
312
- }
313
- } catch (error) {
314
- console.log("error", error);
315
- }
316
- });
317
- if (sucList.length > 0) {
318
- let _res = { sucList, failList };
319
- if (this.multipleDeleteResMap) {
320
- _res = this.multipleDeleteResMap(_res);
321
- }
322
- resolve(_res);
323
- } else {
324
- reject({ failList });
325
- }
326
- return;
327
- }
328
-
329
- reject({});
330
- });
331
- }
332
-
333
- /**
334
- * 处理传入的数据
335
- * @returns
336
- */
337
- handleInputParams(params, reqMapFn) {
338
- let _params = _.cloneDeep(formDataToObj(params));
339
- if (reqMapFn) {
340
- _params = reqMapFn.bind(this)(_params, params);
341
- }
342
- // TODO: FormData 文件存储?
343
- // if (params instanceof FormData) {
344
- // _params = objToFormData(_params);
345
- // }
346
- return _params;
347
- }
348
-
349
- handleRes(response, resolve, reject) {
350
- if (typeof response !== "object") {
351
- reject(new Error("response not object"));
352
- return;
353
- }
354
- let _res = response;
355
- // 兼容 传入的是 response.data 的情况
356
- if (_res.data && _res.headers && _res.request) {
357
- _res = _res.data;
358
- }
359
- const { code, data } = _res;
360
- const message = this.handleMsg(response);
361
- if (code == 200) {
362
- let _data = data ?? {};
363
- if (_.isObject(_data)) {
364
- if (_data.content && _data.pageNumber && _data.total) {
365
- _data.list = _data.content;
366
- _data.pagination = { current: _data.pageNumber, total: _data.total };
367
- }
368
- // 前缀 _ 避免与 data 里已有的 message 冲突
369
- _data._msg = message;
370
- _data._message = message;
371
- }
372
- resolve(_data);
373
- } else {
374
- const error = new Error(message);
375
- error.code = code;
376
- error.response = response;
377
- error._msg = message;
378
- error._message = message;
379
- reject(error);
380
- }
381
- }
382
-
383
- errorHandler(err, reject) {
384
- const response = err.response || err;
385
- if (response) {
386
- const message = this.handleMsg(response, { useStatusText: true });
387
- const error = new Error(message);
388
- error.code = response.status;
389
- error.response = response;
390
- if (message) {
391
- // 前缀 _ 避免与 data 里已有的 message 冲突
392
- error._message = message;
393
- error._msg = message;
394
- }
395
- return reject(error);
396
- }
397
- return reject(err || { _msg: _$Temp.defaultErrMsg });
398
- }
399
-
400
- handleMsg(
401
- response,
402
- opt = {
403
- // 是否使用 response.statusText 作为兜底 msg
404
- useStatusText: false,
405
- },
406
- ) {
407
- const {
408
- // 是否使用 response.statusText 作为兜底 msg
409
- useStatusText,
410
- } = opt || {};
411
- let message = (response.data && (response.data.message || response.data.msg)) || response.msg || response.message;
412
- if (!message && useStatusText) {
413
- message = response.statusText;
414
- }
415
-
416
- return message || _$Temp.defaultErrMsg;
417
- }
418
- }
419
-
420
- export { ArrayDataModel };
421
-
422
- export default ArrayDataModel;
package/src/axios.js DELETED
@@ -1,162 +0,0 @@
1
- import axiosDef from "axios";
2
- import Cookies from "js-cookie";
3
- import _ from "lodash";
4
-
5
- export const TOKEN = "token";
6
-
7
- const axios = axiosDef.create();
8
-
9
- /**
10
- * 设置 token
11
- * @param {string} token
12
- * @param {Object} opt
13
- */
14
- export function setToken(token = "", opt) {
15
- const { hasCookie = false } = opt || {};
16
- const _t = token || "";
17
- if (hasCookie) {
18
- const { cookieAttrs } = opt || {};
19
- const cookieOpt = _.merge(
20
- {
21
- expires: 365,
22
- },
23
- cookieAttrs,
24
- );
25
- Cookies.set(TOKEN, _t, cookieOpt);
26
- }
27
- localStorage.setItem(TOKEN, _t);
28
- setAxAuthorization(_t);
29
- }
30
-
31
- /**
32
- * 获取 token
33
- * @returns
34
- */
35
- export function getToken(opt) {
36
- const { hasCookie = false } = opt || {};
37
- let token = "";
38
- if (hasCookie) {
39
- token = Cookies.get(TOKEN) || "";
40
- if (token) {
41
- return token;
42
- }
43
- }
44
- token = localStorage.getItem(TOKEN);
45
- return token;
46
- }
47
-
48
- export const temp = { axiosList: [axios, axiosDef] };
49
-
50
- export function setAxios(axiosList) {
51
- temp.axiosList = [...temp.axiosList, ...axiosList];
52
- return temp.axiosList;
53
- }
54
-
55
- /**
56
- * axios 守卫
57
- * @param {Function} cb
58
- * @param {Function} errCb
59
- * @param {Object} opt
60
- * @param {Object} opt.replaceErrCb 直接替换回调函数
61
- */
62
- export function setAxRequest(cb, errCb, opt = {}) {
63
- temp.axiosList?.forEach((_ax) => {
64
- _ax.interceptors.request.use(cb, handleErrCb(errCb, { logPrefix: "Error axios response: ", ...opt }));
65
- });
66
- }
67
-
68
- /**
69
- * 请求到结果的拦截处理
70
- * @param {Function} cb
71
- * @param {Function} errCb
72
- * @param {Object} opt
73
- * @param {Object} opt.replaceErrCb 直接替换回调函数
74
- */
75
- export function setAxResponse(cb, errCb, opt = {}) {
76
- temp.axiosList?.forEach((_ax) => {
77
- _ax.interceptors.response.use(cb, handleErrCb(errCb, { logPrefix: "Error axios response: ", ...opt }));
78
- });
79
- }
80
-
81
- /**
82
- * 处理错误回调函数
83
- * @param {Function} errCb
84
- * @param {Object} opt
85
- * @param {Object} opt.replaceErrCb 直接替换回调函数
86
- * @returns
87
- */
88
- export function handleErrCb(errCb, opt) {
89
- const { replaceErrCb, logPrefix } = opt || {};
90
- const isFn = typeof errCb === "function";
91
-
92
- if (replaceErrCb && isFn) {
93
- return errCb;
94
- }
95
-
96
- return (error) => {
97
- if (axiosDef.isCancel(error)) {
98
- console.info("axios 请求已取消:", error);
99
- return Promise.reject(error);
100
- }
101
- console.error(logPrefix, error);
102
- return isFn ? errCb() : Promise.reject(error);
103
- };
104
- }
105
-
106
- /**
107
- * 设置 axios.defaults
108
- * @param {string} key
109
- * @param {*} val
110
- */
111
- export function setAxDefaults(key, val) {
112
- temp.axiosList?.forEach((_ax) => {
113
- _ax.defaults[key] = val;
114
- });
115
- }
116
-
117
- /**
118
- * 设置 headers
119
- * @param {string} key
120
- * @param {*} val
121
- */
122
- export function setAxHeaders(key, val) {
123
- temp.axiosList?.forEach((_ax) => {
124
- _ax.defaults.headers[key] = val;
125
- });
126
- }
127
-
128
- /**
129
- * 设置 timeout
130
- * @param {number} timeout
131
- */
132
- export function setAxTimeout(timeout = 0) {
133
- setAxDefaults("timeout", timeout);
134
- }
135
-
136
- /**
137
- * 设置 Authorization token
138
- * @param {string} token
139
- */
140
- export function setAxAuthorization(token) {
141
- setAxHeaders("Authorization", token);
142
- }
143
-
144
- /**
145
- * 设置 baseUrl
146
- * @param {*} url
147
- */
148
- export function setAxBaseUrl(url) {
149
- setAxDefaults("baseURL", url);
150
- }
151
-
152
- /**
153
- * 获取 axios.CancelToken.source, 用于取消请求
154
- * @returns
155
- */
156
- export function getCancelTokenSource() {
157
- return axiosDef.CancelToken.source();
158
- }
159
-
160
- export { axios, axiosDef };
161
-
162
- export default axios;