|
|
@@ -10,6 +10,7 @@ import {
|
|
|
DataErrorFormatUtils,
|
|
|
defaultDataErrorFormatHandler
|
|
|
} from "@imengyu/js-request-transform";
|
|
|
+import dayjs, { isDayjs } from 'dayjs';
|
|
|
|
|
|
function setErrorFormatter() {
|
|
|
DataErrorFormatUtils.setFormatHandler((error, data) => {
|
|
|
@@ -95,4 +96,49 @@ export function registryConvert() {
|
|
|
};
|
|
|
}
|
|
|
})
|
|
|
+ DataConverter.registerConverter({
|
|
|
+ key: 'Dayjs',
|
|
|
+ targetType: 'dayjs',
|
|
|
+ converter: (source, key, type, child, dateFormat) => {
|
|
|
+ if (isDayjs(source))
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
+ result: source,
|
|
|
+ }
|
|
|
+ if (source instanceof Date)
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
+ result: dayjs(source),
|
|
|
+ }
|
|
|
+ if (typeof source === 'string')
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
+ result: dayjs(source, dateFormat),
|
|
|
+ }
|
|
|
+ if (typeof source === 'number')
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
+ result: dayjs(source),
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ success: false,
|
|
|
+ convertFailMessage: `[${key}] 不是日期类型`,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ })
|
|
|
+ DataConverter.registerConverter({
|
|
|
+ key: 'DayjsToString',
|
|
|
+ targetType: 'dayjsString',
|
|
|
+ converter: (source, key, type, child, dateFormat) => {
|
|
|
+ if (isDayjs(source))
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
+ result: source.format(dateFormat),
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ success: false,
|
|
|
+ convertFailMessage: `[${key}] 不是日期类型`,
|
|
|
+ };
|
|
|
+ }
|
|
|
+ })
|
|
|
}
|