export const formatTime = (date: Date) => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return ( [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') ) } const formatNumber = (n: number) => { const s = n.toString() return s[1] ? s : '0' + s } /** * 函数防抖: 当事件被触发 n 秒后再执行回调,如果在 n 秒内又被触发,则重新计时。 * @param {*} func * @param {*} wait * @returns { Function } */ export const debounce = (func: any, wait: number) => { let delay = wait || 500; let timer: number | null; return () => { const _this = this; let args: any = arguments; if (timer) clearTimeout(timer); timer = setTimeout(() => { timer = null; func.apply(_this, args) }, delay) } } /** * 函数节流: 规定在一个单位时间内只能触发一次函数,如果在单位时间内触发多次,只执行一次。 * @param {*} func * @param {*} wait * @returns { Function } */ export const throttle = (func: { apply: (arg0: undefined, arg1: any) => void }, wait: number) => { let delay = wait || 500; let timer: number | null; return () => { let _this = this; let args = arguments; if (!timer) { timer = setTimeout(() => { func.apply(_this, args); timer = null; }, delay) } } } /** * 格式化金额 * @param price * @param type */ export const formatPrice = (price: number, type?: string) => { const amountStr = price.toFixed(2); const [integerPart, decimalPart] = amountStr.split("."); if (type === "ALL") { return amountStr; } return { integerPart, decimalPart, }; } export const GRADE_ENUM = { '1': '一年级', '2': '二年级', '3': '三年级', '4': '四年级', '5': '五年级', '6': '六年级', '7': '七年级', '8': '八年级', '9': '九年级' } as any;