Blame view

src/utils/http/axios/helper.ts 1.09 KB
vben authored
1
2
import { isObject, isString } from '/@/utils/is';
3
4
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
5
export function joinTimestamp<T extends boolean>(
vben authored
6
7
8
9
  join: boolean,
  restful: T
): T extends true ? string : object;
10
export function joinTimestamp(join: boolean, restful = false): string | object {
vben authored
11
12
13
14
15
16
17
  if (!join) {
    return restful ? '' : {};
  }
  const now = new Date().getTime();
  if (restful) {
    return `?_t=${now}`;
  }
Vben authored
18
  return { _t: now };
vben authored
19
20
21
}

/**
Vben authored
22
 * @description: Format request parameter time
vben authored
23
 */
24
25
26
27
28
export function formatRequestDate(params: Recordable) {
  if (Object.prototype.toString.call(params) !== '[object Object]') {
    return;
  }
vben authored
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  for (const key in params) {
    if (params[key] && params[key]._isAMomentObject) {
      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;
        } catch (error) {
          throw new Error(error);
        }
      }
    }
    if (isObject(params[key])) {
      formatRequestDate(params[key]);
    }
  }
}