import dayjs from 'dayjs' function getCookie(name: string) { const prefix = name + '=' const start = document.cookie.indexOf(prefix) if (start === -1) { return null } let end = document.cookie.indexOf(';', start + prefix.length) if (end === -1) { end = document.cookie.length } const value = document.cookie.substring(start + prefix.length, end) return unescape(value) } // 本地存储封装 const storage = { get(key: string) { const val: string | null = localStorage.getItem(key) if (val) { return JSON.parse(val) } return null }, set(key: string, value: any) { if (value) { value = JSON.stringify(value) } localStorage.setItem(key, value) }, del(key: string) { localStorage.removeItem(key) } } // 节流 const throttle = (fn: Function, delay: number) => { // 定义上次触发时间 let last: number = 0 return (...args: any[]) => { const now: number = +Date.now() console.log('call', now, last, delay) if (now > last + delay) { last = now fn.apply(this, args) } } } const debounce = (fn: Function, delay: number) => { let timer: NodeJS.Timer | null = null return (...args: any[]) => { // 判断定时器是否存在,清除定时器 if (timer) { clearTimeout(Number(timer)) } // 重新调用setTimeout timer = setTimeout(() => { fn.apply(this, args) }, delay) } } /** * 将子组件路径还原成带前缀的路径 * @param parentPath - 父组件路径 * @param pathOfTargetConfig - 用户希望访问的组件的在路由配置信息中填写的路径 * @returns 拼接后的path */ const combinationPath = (parentPath: string | undefined, pathOfTargetConfig: string): string => { let combinedPath = !pathOfTargetConfig.startsWith('/') ? `/${pathOfTargetConfig}` : pathOfTargetConfig combinedPath = parentPath ? `${parentPath}${combinedPath}` : combinedPath return combinedPath } /** * 日期格式化 * @param format - 格式 */ const dayjsFormat = (date: dayjs.ConfigType, format: string = 'YYYY-MM-DD HH:mm:ss') => { if (date === "0001-01-01 00:00:00") return '' return dayjs(date).format(format) } /** * 生成随机密码 * @param len - 长度 */ const generatePsw = (len: number): string => { const pasArr = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '_', '-', '$', '%', '&', '@', '+', '!' ] let password = '' for (let i = 0; i < len; i++) { const x = Math.floor(Math.random() * pasArr.length) password += pasArr[x] } return password } const formatDate = (d: string) => { if (!d) return '' const date = new Date(d) const year = date.getFullYear() let mon: number | string = date.getMonth() + 1 let day: number | string = date.getDate() let hour: number | string = date.getHours() let minute: number | string = date.getMinutes() let scond: number | string = date.getSeconds() if (mon < 10) { mon = '0' + mon.toString() } if (day < 10) { day = '0' + day.toString() } if (hour < 10) { hour = '0' + hour.toString() } if (minute < 10) { minute = '0' + minute.toString() } if (scond < 10) { scond = '0' + scond.toString() } return `${year}${mon}-${day}${hour}:${minute}:${scond}` } // 数字千分位 const formatMoney = (num: number) => { return (Math.round(num) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,') + '.00' } export { getCookie, storage, throttle, debounce, combinationPath, dayjsFormat, generatePsw, formatDate, formatMoney }