helper.ts 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { isObject, isString } from '/@/utils/is';
  2. import dayjs from "dayjs";
  3. const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm';
  4. export function joinTimestamp<T extends boolean>(join: boolean, restful: T): T extends true ? string : object;
  5. export function joinTimestamp(join: boolean, restful = false): string | object {
  6. if (!join) {
  7. return restful ? '' : {};
  8. }
  9. const now = new Date().getTime();
  10. if (restful) {
  11. return `?_t=${now}`;
  12. }
  13. return { _t: now };
  14. }
  15. /**
  16. * @description: Format request parameter time
  17. */
  18. export function formatRequestDate(params: Recordable) {
  19. if (Object.prototype.toString.call(params) !== '[object Object]') {
  20. return;
  21. }
  22. for (const key in params) {
  23. // 判断是否是dayjs实例
  24. if (dayjs.isDayjs(params[key])) {
  25. params[key] = params[key].format(DATE_TIME_FORMAT);
  26. }
  27. if (isString(key)) {
  28. const value = params[key];
  29. if (value) {
  30. try {
  31. params[key] = isString(value) ? value.trim() : value;
  32. } catch (error) {
  33. throw new Error(error);
  34. }
  35. }
  36. }
  37. if (isObject(params[key])) {
  38. formatRequestDate(params[key]);
  39. }
  40. }
  41. }