@hzab/data-model 1.6.0 → 1.7.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ # @hzab/data-model@1.7.2
2
+
3
+ feat: axios.js getCancelTokenSource 获取 axios.CancelToken.source, 用于取消请求
4
+
5
+ # @hzab/data-model@1.7.1
6
+
7
+ feat: sortKey/orderByColumn 支持对象格式,支持定义数据类型、自定义数据处理函数;自定义数据对比函数
8
+
9
+ # @hzab/data-model@1.7.0
10
+
11
+ feat: 本地数据支持排序查询列表数据
12
+
13
+ # @hzab/data-model@1.6.1
14
+
15
+ fix: array-data-modal getMap getListMap 修复;getListMap、getListResMap 逻辑优化
16
+
1
17
  # @hzab/data-model@1.6.0
2
18
 
3
19
  fix: 注释;通用函数抽离
package/README.md CHANGED
@@ -182,6 +182,28 @@ function Demo({ orgId }) {
182
182
  | opt.effectParams | Object | 否 | - | 动态的 params 数据,包含了 query |
183
183
  | opt.effectQuery | Object | 否 | - | 动态的 query 数据 |
184
184
 
185
+ ## array-data-model
186
+
187
+ - 参数通 DataModel
188
+
189
+ ### query 配置
190
+
191
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
192
+ | --------------------- | ----------------------------------------- | ---- | ------ | --------------------------------------------------------- |
193
+ | sortKey/orderByColumn | string/Object/Array[string]/Array[Object] | 否 | - | 排序字段,支持多维度排序,按照传入的顺序排优先级 |
194
+ | sortType/isAsc | string/Object | 否 | - | 排序规则,支持对象配置各字段排序规则 正序: asc 倒序: desc |
195
+
196
+ ### sortKey/orderByColumn Object 配置
197
+
198
+ | 参数 | 类型 | 必填 | 默认值 | 说明 |
199
+ | ------------- | -------- | ---- | ------ | -------------------------------------------------------------------------------------------------------------------------- |
200
+ | path | string | 否 | - | path 取值路径字符串 |
201
+ | type | string | 否 | - | 值类型 用于进行内部相关转换 date |
202
+ | customFormat | Function | 否 | - | 自定义数据处理函数 |
203
+ | customCompare | Function | 否 | - | 自定义数据对比函数 (a, b)=>number,如果 a 小于 b,返回值为负数,如果 a 大于 b,返回值为正数,如果两个元素相等,返回值为 0。NaN 被视为 0。 |
204
+
205
+ - type 目前支持的类型: date
206
+
185
207
  # 组件开发流程
186
208
 
187
209
  - 在 config/webpack.config.js 中按需修改 library 配置的文件名
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hzab/data-model",
3
- "version": "1.6.0",
3
+ "version": "1.7.2",
4
4
  "description": "data model",
5
5
  "main": "src",
6
6
  "scripts": {
@@ -43,6 +43,7 @@
43
43
  "js-cookie": ">=3.0.5"
44
44
  },
45
45
  "dependencies": {
46
+ "dayjs": "^1.11.7",
46
47
  "axios": ">=1.6.2",
47
48
  "js-cookie": ">=3.0.5",
48
49
  "nanoid": "^3.3.7"
package/src/ArrayUtils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { nanoid } from "nanoid";
2
2
  import _ from "lodash";
3
+ import dayjs from "dayjs";
3
4
 
4
5
  export const STATE_CODE = {
5
6
  SUC: 200,
@@ -57,8 +58,11 @@ export class ArrayUtils {
57
58
  * @param {*} query
58
59
  */
59
60
  findListByQuery(query) {
60
- const { pageNum = 1, pageSize = 10 } = query || {};
61
- const { _list } = this;
61
+ const { pageNum = 1, pageSize = 10, sortKey, sortType, orderByColumn, isAsc } = query || {};
62
+ // 排序
63
+ const _list = this.getSortList(sortKey || orderByColumn, sortType || isAsc);
64
+
65
+ // 分页
62
66
  // 边界检查
63
67
  if (pageNum < 1 || pageSize <= 0) {
64
68
  return { list: [], pagination: { current: 1, total: 0 } };
@@ -76,6 +80,107 @@ export class ArrayUtils {
76
80
  return { list: _list.slice(startIndex, endIndex), pagination: { current: pageNum, total: this.getCount() } };
77
81
  }
78
82
 
83
+ /**
84
+ * 对目标数据进行多维度排序
85
+ * @param list
86
+ * @param {*} sortKey 排序的字段 支持 a.b.c 写法
87
+ * @param {*} sortType 排序方式 正序: asc 倒序: desc
88
+ * @returns
89
+ */
90
+ getSortList(sortKey, sortType) {
91
+ const _list = _.cloneDeep(this._list);
92
+ if (!sortKey && !sortType) {
93
+ return _list;
94
+ }
95
+ const paths = Array.isArray(sortKey) ? sortKey : [sortKey];
96
+ _list.sort((a, b) => {
97
+ return this.compareData(a, b, { paths, sortType });
98
+ });
99
+ return _list;
100
+ }
101
+
102
+ /**
103
+ * 根据传入的 paths 比较对象目标数据先后顺序
104
+ * @param dataA
105
+ * @param dataB
106
+ * @param opt
107
+ * @param opt.paths 数据 path 数组
108
+ * @param opt.pathIdx 当前数据下标
109
+ * @param opt.sortType 排序规则
110
+ * @returns
111
+ */
112
+ compareData(dataA, dataB, opt = { paths, pathIdx: 0, sortType: "desc" }) {
113
+ const { paths, pathIdx = 0, sortType = "desc" } = opt;
114
+ const path = paths[pathIdx];
115
+ let _path = path;
116
+
117
+ if (path && typeof path === "object") {
118
+ // 外部自定义比较函数
119
+ if (typeof path.customCompare === "function") {
120
+ return direction * path.customCompare(dataA, dataB, opt);
121
+ }
122
+ _path = path.path;
123
+ }
124
+
125
+ /** 排序规则 */
126
+ let direction = 1;
127
+ // 兼容 string 及 object 类型
128
+ let _sortType = sortType;
129
+ if (typeof _sortType === "object") {
130
+ _sortType = _sortType[_path];
131
+ }
132
+ if (_sortType === "asc") {
133
+ direction = -1;
134
+ }
135
+
136
+ let valueA = this.handleValByKey(dataA, path);
137
+ let valueB = this.handleValByKey(dataB, path);
138
+ if (valueA < valueB) {
139
+ return -1 * direction;
140
+ }
141
+ if (valueA > valueB) {
142
+ return 1 * direction;
143
+ }
144
+
145
+ if (pathIdx + 1 >= paths.length) {
146
+ return 0;
147
+ }
148
+
149
+ if (valueA == valueB) {
150
+ // 继续使用次选排序规则排序
151
+ return this.compareData(dataA, dataB, { ...opt, pathIdx: pathIdx + 1 });
152
+ }
153
+ }
154
+
155
+ /**
156
+ * 根据 path 配置对数据进行处理
157
+ * @param {Object} data 数据
158
+ * @param {string/Object} path 取值路径字符串 或 取值配置
159
+ * @param {string} path.path 取值路径字符串
160
+ * @param {string} path.type 值类型 用于进行相关转换
161
+ * @param {Function} path.customFormat 自定义数据处理函数
162
+ */
163
+ handleValByKey(data, path) {
164
+ if (typeof path === "string") {
165
+ return _.get(data, path);
166
+ }
167
+ if (path === null || path === undefined) {
168
+ return;
169
+ }
170
+ if (typeof path === "object") {
171
+ let val = _.get(data, path.path);
172
+ // 日期格式转为时间戳进行比较
173
+ if (path?.type === "date") {
174
+ val = dayjs(val).valueOf();
175
+ }
176
+ // 外部自定义数据格式函数,便于排序比较
177
+ if (typeof path?.customFormat === "function") {
178
+ val = customFormat(val);
179
+ }
180
+ return val;
181
+ }
182
+ }
183
+
79
184
  /**
80
185
  * 获取完整列表
81
186
  */
@@ -99,6 +204,7 @@ export class ArrayUtils {
99
204
  // TODO: id 去重
100
205
  const item = _.cloneDeep(data);
101
206
  this.setId(item);
207
+ this.setCreateTime(item);
102
208
  this._list.push(item);
103
209
  return STATE_CODE.SUC;
104
210
  }
@@ -111,6 +217,7 @@ export class ArrayUtils {
111
217
  // TODO: id 去重
112
218
  const item = _.cloneDeep(data);
113
219
  this.setId(item);
220
+ this.setCreateTime(item);
114
221
  this._list.unshift(item);
115
222
  return STATE_CODE.SUC;
116
223
  }
@@ -131,6 +238,7 @@ export class ArrayUtils {
131
238
  return STATE_CODE.NOT_FOUNT;
132
239
  }
133
240
  _list.splice(idx, 1, data);
241
+ this.setUpdateTime(data);
134
242
  return STATE_CODE.SUC;
135
243
  }
136
244
 
@@ -152,6 +260,7 @@ export class ArrayUtils {
152
260
  Object.keys(data).forEach((key) => {
153
261
  item[key] = data[key];
154
262
  });
263
+ this.setUpdateTime(data);
155
264
  return STATE_CODE.SUC;
156
265
  }
157
266
 
@@ -186,6 +295,22 @@ export class ArrayUtils {
186
295
  }
187
296
  return item;
188
297
  }
298
+
299
+ /**
300
+ * 设置创建时间
301
+ * @param {*} item
302
+ */
303
+ setCreateTime(item) {
304
+ item.createTime = Date.now();
305
+ }
306
+
307
+ /**
308
+ * 设置更新时间
309
+ * @param {*} item
310
+ */
311
+ setUpdateTime(item) {
312
+ item.createTime = Date.now();
313
+ }
189
314
  }
190
315
 
191
316
  export const hasId = (id) => {
@@ -121,6 +121,9 @@ class ArrayDataModel extends ArrayUtils {
121
121
 
122
122
  return new Promise((resolve, reject) => {
123
123
  let res = this.findItem(query);
124
+ if (this.getMap) {
125
+ res = this.getMap(res);
126
+ }
124
127
  if (this.getResMap) {
125
128
  res = this.getResMap(res);
126
129
  }
@@ -144,15 +147,15 @@ class ArrayDataModel extends ArrayUtils {
144
147
  let resultList = null;
145
148
  if (this.getListFunc) {
146
149
  resultList = await this.getListFunc(query);
147
- if (this.getListResMap) {
148
- resultList = this.getListResMap(resultList);
149
- }
150
150
  } else {
151
151
  const getPro = this.findListByQuery(query);
152
152
  resultList = await getPro;
153
- if (this.getListResMap) {
154
- resultList = this.getListResMap(resultList);
155
- }
153
+ }
154
+ if (this.getListMap) {
155
+ resultList.list = resultList.list.map((record) => this.getListMap(record));
156
+ }
157
+ if (this.getListResMap) {
158
+ resultList = this.getListResMap(resultList);
156
159
  }
157
160
  return resultList;
158
161
  }
package/src/axios.js CHANGED
@@ -113,8 +113,20 @@ export function setAxAuthorization(token) {
113
113
  setAxHeaders("Authorization", token);
114
114
  }
115
115
 
116
+ /**
117
+ * 设置 baseUrl
118
+ * @param {*} url
119
+ */
116
120
  export function setAxBaseUrl(url) {
117
121
  setAxDefaults("baseURL", url);
118
122
  }
119
123
 
124
+ /**
125
+ * 获取 axios.CancelToken.source, 用于取消请求
126
+ * @returns
127
+ */
128
+ export function getCancelTokenSource() {
129
+ return axios.CancelToken.source();
130
+ }
131
+
120
132
  export default axios;
package/src/data-model.js CHANGED
@@ -233,9 +233,6 @@ class DataModel {
233
233
  let resultList = null;
234
234
  if (this.getListFunc) {
235
235
  resultList = await this.getListFunc(query);
236
- if (this.getListResMap) {
237
- resultList = this.getListResMap(resultList);
238
- }
239
236
  } else {
240
237
  const getPro = new Promise((resolve, reject) => {
241
238
  const apiUrl = this.getApiUrl(this.getListApi, q, ctx, { from: "getList" });
@@ -251,12 +248,6 @@ class DataModel {
251
248
  response,
252
249
  (res) => {
253
250
  // data: { list: [], pagination: { current: 0, total: 1 } }
254
- if (this.getListMap) {
255
- res.list = res.list.map((record) => this.getListMap(record));
256
- }
257
- if (this.getListResMap) {
258
- res = this.getListResMap(res);
259
- }
260
251
  resolve(res);
261
252
  },
262
253
  reject,
@@ -266,6 +257,12 @@ class DataModel {
266
257
  });
267
258
  resultList = await getPro;
268
259
  }
260
+ if (this.getListMap) {
261
+ resultList.list = resultList.list.map((record) => this.getListMap(record));
262
+ }
263
+ if (this.getListResMap) {
264
+ resultList = this.getListResMap(resultList);
265
+ }
269
266
  return resultList;
270
267
  }
271
268