Sfoglia il codice sorgente

🎨 修复日期问题

快乐的梦鱼 3 settimane fa
parent
commit
35812487f2
2 ha cambiato i file con 50 aggiunte e 8 eliminazioni
  1. 4 8
      src/api/collect/AssessmentContent.ts
  2. 46 0
      src/common/ConvertRgeistry.ts

+ 4 - 8
src/api/collect/AssessmentContent.ts

@@ -1,9 +1,9 @@
 import { DataModel, transformArrayDataModel, transformDataModel, type KeyValue } from '@imengyu/js-request-transform';
 import { AppServerRequestModule } from '../RequestModules';
 import { transformSomeToArray } from '../Utils';
-import ApiCofig from '@/common/config/ApiCofig';
 import { appendGetUrlParams } from '@imengyu/imengyu-utils';
 import { useAuthStore } from '@/stores/auth';
+import ApiCofig from '@/common/config/ApiCofig';
 import dayjs from 'dayjs';
 
 /**
@@ -390,13 +390,9 @@ export class SelfAssessmentDetail extends DataModel<SelfAssessmentDetail> {
 
       },
       awardTime: {
-        customToClientFn: (value) => {
-          if (value) {
-            return dayjs(value as string);
-          }
-          return value;
-        },
-        serverSide: 'string',
+        clientSide: 'dayjs',
+        clientSideDateFormat: 'YYYY-MM-DD',
+        serverSide: 'dayjsString',
         serverSideDateFormat: 'YYYY-MM-DD',
       },
       checkItems: {

+ 46 - 0
src/common/ConvertRgeistry.ts

@@ -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}] 不是日期类型`,
+      };
+    }
+  })
 }