You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

2 years ago
import { isObject, isString } from '@/utils/is'
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
export function joinTimestamp<T extends boolean>(join: boolean, restful: T): T extends true ? string : object
export function joinTimestamp(join: boolean, restful = false): string | object {
if (!join)
2 years ago
return restful ? '' : {}
2 years ago
const now = new Date().getTime()
if (restful)
2 years ago
return `?_t=${now}`
2 years ago
return { _t: now }
}
/**
2 years ago
* @description:
2 years ago
*/
export function formatRequestDate(params: Recordable) {
if (Object.prototype.toString.call(params) !== '[object Object]')
2 years ago
return
for (const key in params) {
const format = params[key]?.format ?? null
if (format && typeof format === 'function')
2 years ago
params[key] = params[key].format(DATE_TIME_FORMAT)
2 years ago
if (isString(key)) {
const value = params[key]
if (value) {
try {
params[key] = isString(value) ? value.trim() : value
}
catch (error: any) {
2 years ago
throw new Error(error)
}
}
}
if (isObject(params[key]))
2 years ago
formatRequestDate(params[key])
}
}