| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 | 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 `<span>${year}</span><span>${mon}-${day}</span><span>${hour}:${minute}:${scond}</span>`}// 数字千分位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 }
 |