@hzab/utils 1.1.6 → 1.1.7
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 +11 -0
- package/LICENSE +21 -0
- package/dist/index.cjs +1532 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +741 -0
- package/dist/index.d.ts +741 -0
- package/dist/index.js +1423 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -49
- package/src/color.ts +8 -8
- package/src/file/file.ts +4 -3
- package/src/file/fileType.ts +1 -1
- package/src/formily/field.ts +142 -0
- package/src/global.d.ts +9 -0
- package/src/idCardVerify.ts +1 -1
- package/src/img-utils.ts +9 -9
- package/src/index.ts +43 -0
- package/src/nanoid.ts +19 -19
- package/src/options.ts +248 -226
- package/src/upload/OfflineUpload.ts +2 -2
- package/src/upload/OssUploadUtil.ts +1 -1
- package/src/upload/fileDataNormalization.ts +3 -3
- package/src/upload/ossUpload.ts +2 -1
- package/src/url.ts +181 -155
- package/README.md +0 -137
- package/changelog.md +0 -56
package/src/options.ts
CHANGED
|
@@ -1,226 +1,248 @@
|
|
|
1
|
-
import { cloneDeep } from "lodash-es";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
*
|
|
46
|
-
* @param
|
|
47
|
-
* @param
|
|
48
|
-
* @
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const {
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* @param
|
|
117
|
-
* @
|
|
118
|
-
*/
|
|
119
|
-
export
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
*
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
1
|
+
import { cloneDeep } from "lodash-es";
|
|
2
|
+
|
|
3
|
+
import type { IHandleEmptyValOpt } from "./empty-val";
|
|
4
|
+
import { checkEmpty, handleEmptyVal } from "./empty-val";
|
|
5
|
+
|
|
6
|
+
export interface IFieldNames {
|
|
7
|
+
value?: string;
|
|
8
|
+
label?: string;
|
|
9
|
+
children?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface IFindOptionOpt extends IHandleEmptyValOpt {
|
|
13
|
+
fieldNames?: IFieldNames;
|
|
14
|
+
// 是否返回完整的路径数组
|
|
15
|
+
isPathList?: boolean;
|
|
16
|
+
pathList?: Array<any>;
|
|
17
|
+
labelList?: Array<string>;
|
|
18
|
+
parent?: any;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface IGetArrayLabels extends IFindOptionOpt {
|
|
22
|
+
split?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface IGetValLabelOpt extends IHandleEmptyValOpt {
|
|
26
|
+
showEmpty?: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type formatOptionsOptT = {
|
|
30
|
+
fieldNames?: IFieldNames;
|
|
31
|
+
sourceFieldNames?: IFieldNames;
|
|
32
|
+
/**
|
|
33
|
+
* 是否清除其他参数
|
|
34
|
+
*/
|
|
35
|
+
isClear?: boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const DEF_FIELD_NAMES = {
|
|
39
|
+
value: "value",
|
|
40
|
+
label: "label",
|
|
41
|
+
children: "children",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* 获取无限级数据的选中项
|
|
46
|
+
* @param val
|
|
47
|
+
* @param options
|
|
48
|
+
* @param opt
|
|
49
|
+
* @returns
|
|
50
|
+
*/
|
|
51
|
+
export const findOptionByVal = function (
|
|
52
|
+
val,
|
|
53
|
+
options,
|
|
54
|
+
opt: IFindOptionOpt = {
|
|
55
|
+
// 是否返回完整的路径数组
|
|
56
|
+
isPathList: false,
|
|
57
|
+
pathList: [],
|
|
58
|
+
labelList: [],
|
|
59
|
+
parent: undefined,
|
|
60
|
+
fieldNames: {
|
|
61
|
+
label: "label",
|
|
62
|
+
value: "value",
|
|
63
|
+
children: "children",
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
) {
|
|
67
|
+
if (!options) {
|
|
68
|
+
console.warn("getDeepData 请传入 options");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const { fieldNames, isPathList, pathList = [], labelList = [] } = opt || {};
|
|
72
|
+
const { label = "label", value = "value", children = "children" } = fieldNames || {};
|
|
73
|
+
const res = options.find((it) => it[value] === val);
|
|
74
|
+
if (res) {
|
|
75
|
+
if (isPathList) {
|
|
76
|
+
pathList.unshift(res);
|
|
77
|
+
labelList.unshift(res[label]);
|
|
78
|
+
return {
|
|
79
|
+
last: res,
|
|
80
|
+
pathList,
|
|
81
|
+
labelList,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
return res;
|
|
85
|
+
}
|
|
86
|
+
const len = options.length;
|
|
87
|
+
for (let i = 0; i < len; i++) {
|
|
88
|
+
const item = options[i];
|
|
89
|
+
const childList = item[children];
|
|
90
|
+
if (Array.isArray(childList) && childList.length > 0) {
|
|
91
|
+
const res = findOptionByVal(val, childList, {
|
|
92
|
+
...opt,
|
|
93
|
+
parent: item,
|
|
94
|
+
pathList,
|
|
95
|
+
labelList,
|
|
96
|
+
});
|
|
97
|
+
if (res) {
|
|
98
|
+
if (isPathList) {
|
|
99
|
+
pathList.unshift(item);
|
|
100
|
+
labelList.unshift(item[label]);
|
|
101
|
+
return {
|
|
102
|
+
last: res.last,
|
|
103
|
+
labelList,
|
|
104
|
+
pathList,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
return res;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 在命中项上写出标准 value / label / children(保留原字段)。
|
|
115
|
+
* 与 `getOptItByVal` 的归一化语义一致;供 `normalizeOption(findOptionByVal(...))` 组合使用。
|
|
116
|
+
* @param item 命中的 option 项
|
|
117
|
+
* @param fieldNames 自定义字段名映射
|
|
118
|
+
*/
|
|
119
|
+
export function normalizeOption<T extends Record<string, any>>(
|
|
120
|
+
item: T,
|
|
121
|
+
fieldNames?: IFieldNames,
|
|
122
|
+
): T & { value: unknown; label: unknown; children: unknown } {
|
|
123
|
+
const { value = "value", label = "label", children = "children" } = fieldNames || {};
|
|
124
|
+
return {
|
|
125
|
+
...item,
|
|
126
|
+
value: item?.[value],
|
|
127
|
+
label: item?.[label],
|
|
128
|
+
children: item?.[children],
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 获取 options 中 val 对应的 label
|
|
134
|
+
* @param {*} val
|
|
135
|
+
* @param {Array<Object>} options
|
|
136
|
+
* @param {Object} opt
|
|
137
|
+
* @returns
|
|
138
|
+
*/
|
|
139
|
+
export const getOptionLabel = function (val, options = [], opt: IFindOptionOpt = {}) {
|
|
140
|
+
const { fieldNames = DEF_FIELD_NAMES } = opt || {};
|
|
141
|
+
return findOptionByVal(val, options, opt)?.[fieldNames?.label || DEF_FIELD_NAMES.label];
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 获取 options 中的指定项 的 label
|
|
146
|
+
* @param {*} val
|
|
147
|
+
* @param {*} options
|
|
148
|
+
* @param {*} opt
|
|
149
|
+
* @returns
|
|
150
|
+
*/
|
|
151
|
+
export function getValLabel(val, options = [], opt: IGetValLabelOpt = {}) {
|
|
152
|
+
const { showEmpty } = opt;
|
|
153
|
+
if (showEmpty && checkEmpty(val, opt)) {
|
|
154
|
+
return handleEmptyVal(val, opt);
|
|
155
|
+
}
|
|
156
|
+
return getOptionLabel(val, options, opt);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* 获取 options 中的指定项 的 label 数组
|
|
161
|
+
* @param {*} values
|
|
162
|
+
* @param {*} options
|
|
163
|
+
* @param {*} opt
|
|
164
|
+
* @returns
|
|
165
|
+
*/
|
|
166
|
+
export function getArrayLabels(values, options, opt: IGetArrayLabels = {}) {
|
|
167
|
+
if (checkEmpty(values, opt)) {
|
|
168
|
+
return handleEmptyVal(values, opt);
|
|
169
|
+
}
|
|
170
|
+
const { split = "、" } = opt || {};
|
|
171
|
+
let _values = values;
|
|
172
|
+
if (!Array.isArray(_values)) {
|
|
173
|
+
_values = [_values];
|
|
174
|
+
}
|
|
175
|
+
return _values
|
|
176
|
+
?.map((val) => getOptionLabel(val, options, opt) ?? val)
|
|
177
|
+
?.filter((it) => (it ?? it) || false)
|
|
178
|
+
?.join(split);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* 格式化 options 数组
|
|
183
|
+
*/
|
|
184
|
+
export const formatOptions = function (options, opt: formatOptionsOptT = {}) {
|
|
185
|
+
if (!Array.isArray(options)) {
|
|
186
|
+
console.warn("formatOptions Error: options not Array");
|
|
187
|
+
return options;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const {
|
|
191
|
+
fieldNames = DEF_FIELD_NAMES,
|
|
192
|
+
sourceFieldNames = DEF_FIELD_NAMES,
|
|
193
|
+
// 是否清除其他参数
|
|
194
|
+
isClear,
|
|
195
|
+
} = opt || {};
|
|
196
|
+
|
|
197
|
+
const { label, value, children } = fieldNames;
|
|
198
|
+
const { label: sLabel, value: sValue, children: sChildren } = sourceFieldNames;
|
|
199
|
+
|
|
200
|
+
const _options = cloneDeep(options);
|
|
201
|
+
const resOptions = [];
|
|
202
|
+
_options.forEach((it) => {
|
|
203
|
+
const _it = isClear ? {} : { ...it };
|
|
204
|
+
_it[value] = it[sValue];
|
|
205
|
+
_it[label] = it[sLabel];
|
|
206
|
+
resOptions.push(_it);
|
|
207
|
+
const childList = it[sChildren];
|
|
208
|
+
if (childList) {
|
|
209
|
+
_it[children] = formatOptions(childList, opt);
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
return resOptions;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* 根据 val 获取对应的 options 项数据,并进行数据归一化处理
|
|
217
|
+
* value, label, children
|
|
218
|
+
* @param {string|number|boolean} val
|
|
219
|
+
* @param {Object} field
|
|
220
|
+
*/
|
|
221
|
+
export const getOptItByVal = function (val, options, opt) {
|
|
222
|
+
const { fieldNames } = opt || {};
|
|
223
|
+
const { value = "value", label = "label", children = "children" } = fieldNames || {};
|
|
224
|
+
for (let i = 0; i < options.length; i++) {
|
|
225
|
+
const it = options[i];
|
|
226
|
+
if (it?.[value] === val) {
|
|
227
|
+
return {
|
|
228
|
+
...it,
|
|
229
|
+
value: it?.[value],
|
|
230
|
+
label: it?.[label],
|
|
231
|
+
children: it?.[children],
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
// 递归处理
|
|
235
|
+
const _children = it?.[children];
|
|
236
|
+
if (Array.isArray(_children) && _children.length > 0) {
|
|
237
|
+
const child = getOptItByVal(val, _children, opt);
|
|
238
|
+
if (child) {
|
|
239
|
+
return {
|
|
240
|
+
...child,
|
|
241
|
+
value: child?.[value],
|
|
242
|
+
label: child?.[label],
|
|
243
|
+
children: child?.[children],
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import dayjs from "dayjs";
|
|
2
|
-
import { Axios, AxiosRequestConfig } from "axios";
|
|
2
|
+
import type { Axios, AxiosRequestConfig } from "axios";
|
|
3
3
|
|
|
4
4
|
import { axios } from "@hzab/data-model";
|
|
5
5
|
|
|
@@ -141,7 +141,7 @@ export function getSignature(opt: IGetSignatureOpt = {}) {
|
|
|
141
141
|
|
|
142
142
|
export interface IGetPreviewUrlsOpt {
|
|
143
143
|
previewUrl?: string;
|
|
144
|
-
params?:
|
|
144
|
+
params?: object;
|
|
145
145
|
axios?: Axios;
|
|
146
146
|
axiosConf?: AxiosRequestConfig;
|
|
147
147
|
}
|
|
@@ -66,11 +66,11 @@ export interface INormalizationOpt {
|
|
|
66
66
|
/** 子项数据模式: 格式化对象 | file 文件格式 | url 地址 | JSON 序列化字符串 */
|
|
67
67
|
itemMode: "object" | "file" | "url" | "jsonStr";
|
|
68
68
|
/** 预览配置参数 */
|
|
69
|
-
previewConfig?:
|
|
69
|
+
previewConfig?: object;
|
|
70
70
|
/** 设置预览 URL 的回调 */
|
|
71
71
|
getPreviewUrl?: (file, opt) => string;
|
|
72
72
|
/** 缩略图配置参数 */
|
|
73
|
-
thumbnailUrlConf?:
|
|
73
|
+
thumbnailUrlConf?: object;
|
|
74
74
|
/** 设置缩略图 URL 的回调 */
|
|
75
75
|
getThumbnailUrl?: (file, opt) => string;
|
|
76
76
|
}
|
|
@@ -105,7 +105,7 @@ export const normalizationFileObj = (data: IFile, opt: INormalizationOpt) => {
|
|
|
105
105
|
console.warn("Upload handleInputData 请传入正确的数据");
|
|
106
106
|
return data;
|
|
107
107
|
}
|
|
108
|
-
|
|
108
|
+
const file = {
|
|
109
109
|
...fileTpl,
|
|
110
110
|
};
|
|
111
111
|
// 对象字符串 转为 对象
|
package/src/upload/ossUpload.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { mergeDirStr, getFileNameByFileObj } from "./uploadUtils";
|
|
2
|
-
import
|
|
2
|
+
import type { IUploadOpt } from "./OssUploadUtil";
|
|
3
|
+
import OssUploadUtil, { axios } from "./OssUploadUtil";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* oss 上传工具类
|