helper.ts 1.1 KB
Newer Older
V
vben 已提交
1 2
import { isObject, isString } from '/@/utils/is';

3
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss';
4

V
Vben 已提交
5
export function joinTimestamp<T extends boolean>(
V
vben 已提交
6
  join: boolean,
V
vben 已提交
7
  restful: T,
V
vben 已提交
8 9
): T extends true ? string : object;

V
Vben 已提交
10
export function joinTimestamp(join: boolean, restful = false): string | object {
V
vben 已提交
11 12 13 14 15 16 17
  if (!join) {
    return restful ? '' : {};
  }
  const now = new Date().getTime();
  if (restful) {
    return `?_t=${now}`;
  }
V
Vben 已提交
18
  return { _t: now };
V
vben 已提交
19 20 21
}

/**
V
Vben 已提交
22
 * @description: Format request parameter time
V
vben 已提交
23
 */
24 25 26 27 28
export function formatRequestDate(params: Recordable) {
  if (Object.prototype.toString.call(params) !== '[object Object]') {
    return;
  }

V
vben 已提交
29
  for (const key in params) {
V
vben 已提交
30 31
    const format = params[key]?.format ?? null;
    if (format && typeof format === 'function') {
V
vben 已提交
32 33 34 35 36 37 38
      params[key] = params[key].format(DATE_TIME_FORMAT);
    }
    if (isString(key)) {
      const value = params[key];
      if (value) {
        try {
          params[key] = isString(value) ? value.trim() : value;
V
vben 已提交
39
        } catch (error: any) {
V
vben 已提交
40 41 42 43 44 45 46 47 48
          throw new Error(error);
        }
      }
    }
    if (isObject(params[key])) {
      formatRequestDate(params[key]);
    }
  }
}