helper.ts 1.1 KB

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