Sanakey
3 天以前 b5c1614fe473330ceca8b7cff0f1802e19bd5039
提交 | 用户 | age
4d2fb0 1 import { isObject, isString } from '@/utils/is';
f646e3 2
522534 3 const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
5465f0 4
50f94b 5 export function joinTimestamp<T extends boolean>(
f646e3 6   join: boolean,
56a966 7   restful: T,
f646e3 8 ): T extends true ? string : object;
V 9
50f94b 10 export function joinTimestamp(join: boolean, restful = false): string | object {
f646e3 11   if (!join) {
V 12     return restful ? '' : {};
13   }
14   const now = new Date().getTime();
15   if (restful) {
16     return `?_t=${now}`;
17   }
a821d9 18   return { _t: now };
f646e3 19 }
V 20
21 /**
a821d9 22  * @description: Format request parameter time
f646e3 23  */
834fa7 24 export function formatRequestDate(params: Recordable) {
V 25   if (Object.prototype.toString.call(params) !== '[object Object]') {
26     return;
27   }
28
f646e3 29   for (const key in params) {
3fcfac 30     const format = params[key]?.format ?? null;
V 31     if (format && typeof format === 'function') {
f646e3 32       params[key] = params[key].format(DATE_TIME_FORMAT);
V 33     }
34     if (isString(key)) {
35       const value = params[key];
36       if (value) {
37         try {
38           params[key] = isString(value) ? value.trim() : value;
a248e2 39         } catch (error: any) {
f646e3 40           throw new Error(error);
V 41         }
42       }
43     }
44     if (isObject(params[key])) {
45       formatRequestDate(params[key]);
46     }
47   }
48 }