-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreset.js
More file actions
295 lines (261 loc) · 9.96 KB
/
preset.js
File metadata and controls
295 lines (261 loc) · 9.96 KB
1
2
3
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
117
118
119
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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import {interceptors, preset, RULES} from "@kne/react-form-antd";
import dayjs from "dayjs";
import merge from "lodash/merge";
import get from "lodash/get";
import transform from 'lodash/transform';
import "@kne/react-form-antd/dist/index.css";
import {PHONE_NUMBER_INPUT} from "./fields/PhoneNumber";
import HelperGuideLabel from "@components/FormInfo/HelperGuideLabel";
import validateIDCard from '@common/utils/validateIDCard';
import {loadModule} from "@kne/remote-loader";
const _olderREQ = RULES.REQ;
const _olderLEN = RULES.LEN;
const formPreset = async (options, otherOptions) => {
const {locale} = Object.assign({}, otherOptions);
interceptors.input.use("photo-string", (value) => {
if (value && typeof value === "string") {
const [id, params] = value.split("?");
const searchParams = new URLSearchParams(params);
return Object.assign({}, {id}, transform(Array.from(searchParams.entries()), (result, value) => {
result[value[0]] = value[1];
}, {}));
}
return value;
});
interceptors.output.use("photo-string", (value) => {
if (!value || typeof value === "string") {
return value;
}
return `${value?.id}${value?.filename ? `?filename=${value?.filename}` : ''}`;
});
interceptors.input.use("photo-string-list", (value) => {
if (value && Array.isArray(value) && value.length > 0) {
return value.map((item) => {
const [id, params] = item.split("?");
const searchParams = new URLSearchParams(params);
return Object.assign({}, {id}, transform(Array.from(searchParams.entries()), (result, value) => {
result[value[0]] = value[1];
}, {}));
});
}
return value;
});
interceptors.output.use("photo-string-list", (value) => {
if (value && Array.isArray(value) && value.length > 0) {
return value.map((item) => {
return `${item?.id}${item?.filename ? `?filename=${item?.filename}` : ''}`;
});
}
return value;
});
interceptors.input.use("to-array", (value) => {
return Array.isArray(value) ? value[0] : value;
});
interceptors.output.use("to-array", (value) => {
return [value];
});
interceptors.input.use("array-single", (value) => {
return value ? [value] : [];
});
interceptors.output.use("array-single", (value) => {
return value ? value[0] : null;
});
interceptors.input.use("boolean-number", (value) => {
return value === 1 || value === "1";
});
interceptors.output.use("boolean-number", (value) => {
return value ? 1 : 0;
});
interceptors.input.use("un-boolean", (value) => {
return !value;
});
interceptors.output.use("un-boolean", (value) => {
return !value;
});
interceptors.output.use("object-output-value", (value) => {
if (!value) {
return value;
}
return value.value || value.id;
});
interceptors.output.use("array-output-value", (value) => {
if (!(Array.isArray(value) && value.length > 0)) {
return [];
}
return value.map((value) => value.value || value.id);
});
interceptors.input.use("date-string", (value) => {
return value ? dayjs(value) : null;
});
interceptors.output.use("date-string", (value) => {
return value ? new Date(value.valueOf()).toISOString() : "";
});
interceptors.input.use("date-range-string", (value) => {
if (!Array.isArray(value)) {
return [];
}
const output = [];
if (value[0]) {
output.push(dayjs(value[0]));
}
if (value[0] && value[1] && value[1] === "sofar") {
output.push("至今");
}
if (value[0] && value[1] && value[1] !== "至今") {
output.push(dayjs(value[1]));
}
return output;
});
interceptors.output.use("date-range-string", (value) => {
if (!Array.isArray(value)) {
return [];
}
const output = [];
if (value[0]) {
output.push(new Date(value[0].valueOf()).toISOString());
}
if (value[0] && value[1] && value[1] === "至今") {
output.push("sofar");
}
if (value[0] && value[1] && value[1] !== "至今") {
output.push(new Date(value[1].valueOf()).toISOString());
}
return output;
});
interceptors.output.use("file-format", (value) => {
if (!Array.isArray(value)) {
return [];
}
return value.map((item) => Object.assign({}, item, {
id: item.id || item.ossId, originalName: item.filename,
}));
});
interceptors.input.use("file-format", (value) => {
if (!Array.isArray(value)) {
return [];
}
return value.map((item) => ({
id: item.id || item.ossId, ossId: item.id || item.ossId, filename: item.originalName || item.filename,
}));
});
const {default: loadCountyList} = await loadModule("components-iconfont:CountyFlag@load");
const countyList = await loadCountyList();
const countyAbMap = new Map(countyList.map(({ab, country_code}) => [ab, country_code]));
const countyCodeMap = new Map(countyList.map(({ab, country_code}) => [country_code, ab]));
interceptors.output.use("phone-number-string", (value) => {
if (!(value?.code && value?.value)) {
return "";
}
return `+${countyAbMap.get(value.code)} ${value.value.replace(/\s+/g, "")}`;
});
interceptors.input.use("phone-number-string", (value) => {
if (!value) {
return {};
}
const matcher = value.match(/^\+(\d+)\s(.*)/);
if (!(matcher && matcher.length >= 3)) {
return {code: countyCodeMap.get(86), value};
}
return {code: countyCodeMap.get(Number(matcher[1])), value: matcher[2]};
});
const getLocaleMsg = (type, values) => {
values = Object.assign({}, values);
const mapping = {
REQ: {
"zh-CN": "%s不能为空", "en-US": "%s cannot be empty ",
}, EMAIL: {
"zh-CN": "请输入有效的%s", "en-US": "Please enter a valid %s",
}, LENGTH_EQUAL: {
"zh-CN": `%s必须等于${values.end}`, "en-US": `%s must be equal to ${values.end}`,
}, LENGTH_MORE: {
"zh-CN": `%s必须大于${values.start}`, "en-US": `%s must be greater than ${values.start}`,
}, LENGTH_LESS: {
"zh-CN": `%s必须小于${values.end}`, "en-US": `%s must be less than ${values.end}`,
}, ID_CARD_INVALID: {
"zh-CN": "请输入有效的身份证号码", "en-US": "Please enter a valid ID card number",
},
};
const target = get(mapping, `${type}.${locale}`);
if (target) {
return target;
}
return get(mapping, `${type}.zh-CN`);
};
preset(merge({}, {
type: "inner", rules: {
REQ: (...args) => {
return Object.assign({}, _olderREQ(...args), {
errMsg: getLocaleMsg("REQ"),
});
}, LEN: (...args) => {
const [value, start, end] = args;
let ruleName = "";
if (end === start && value.length !== Number(end)) {
ruleName = "LENGTH_EQUAL";
}
if (value.length < start) {
ruleName = "LENGTH_MORE";
}
if (end && value.length > end) {
ruleName = "LENGTH_LESS";
}
return Object.assign({}, _olderLEN(...args), {
errMsg: getLocaleMsg(ruleName, {start, end}),
});
}, EMAIL: function (value) {
return {
result: /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(value),
errMsg: getLocaleMsg('EMAIL')
};
}, PHONE_NUMBER_INPUT, ARRAY_LENGTH: (value, start, end) => {
if (end === start && value.length !== Number(end)) {
return {
result: false, errMsg: getLocaleMsg("LENGTH_EQUAL", {start, end}),
};
}
if (value.length < start) {
return {
result: false, errMsg: getLocaleMsg("LENGTH_MORE", {start, end}),
};
}
if (end && value.length > end) {
return {
result: false, errMsg: getLocaleMsg("LENGTH_LESS", {start, end}),
};
}
return {result: true};
}, ID_CARD: (value) => {
const res = validateIDCard(value);
if (!res.isValid) {
return {result: false, errMsg: getLocaleMsg('ID_CARD_INVALID')};
}
return {result: true};
},
}, field: {
datePicker: {
defaultProps: {
interceptor: "date-string",
},
}, rangeDatePicker: {
defaultProps: {
interceptor: "date-range-string",
},
}, inputNumber: {
defaultProps: {
changeOnWheel: false,
},
}, textArea: {
defaultProps: {
autoSize: {
minRows: 2, maxRows: 6,
},
},
},
}, globalProps: {
labelTips: (props) => {
return <HelperGuideLabel {...props} />;
},
},
}, typeof options === "function" ? options({interceptors, RULES}) : options));
};
export default formPreset;