util.ts

121 阅读2分钟

工具ts文件

util.ts
/*
 * @Author: ShiJunJie
 * @Date: 2020-11-03 10:01:50
 * @LastEditors: ShiJunJie
 * @LastEditTime: 2022-04-06 12:00:13
 * @Descripttion:
 */

export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = (s: string) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

// 防抖
export const _debounce = (fn: () => void, delay = 200) => {
  // console.log('utils --> 触发防抖')
  let timer: NodeJS.Timeout = null
  return () => {
    if (timer) {
      clearTimeout(timer)
    }
    timer = setTimeout(() => {
      timer = null
      fn()
    }, delay)
  }
}
// 节流
export const _throttle = (fn: () => void, interval = 200) => {
  // console.log('utils --> 触发节流')
  let last: number
  let timer: NodeJS.Timeout = null
  return function () {
    let now = +new Date()
    if (last && now - last < interval) {
      clearTimeout(timer)
      timer = setTimeout(function () {
        last = now
        fn()
      }, interval)
    } else {
      last = now
      fn()
    }
  }
}

/**
 * 树无限极拍平
 * @param {*} array Array [{'children':[ {''children:[]} ]},{}]
 * @param {*} key 子节点的key
 * @param {*} _response 返回体
 * @returns []
 */
export const patDown = (array: any = [], key: any = 'children', _response: any = []) => {
  array.forEach((e: { [x: string]: any }) => {
    _response.push(e)
    e[key] && e[key].length > 0 && patDown(e[key], key, _response)
  })
  return _response

  // return [].concat(...array.map(item => [].concat(item, ...patDown(item[key],key))))
}

/**
 * 树数据过滤并生成新的树
 * patDown2([...array],Object:{ sonK: "children", k: "value", v: "123" })
 * @param {*} array 树
 * @param {*} conditions {sonk=>子节点的Key , k=>判断条件的Key , v=>判断条件的值}
 * @param {*} _response 新的树
 * @returns
 */
export const patDown2 = (
  array: any = [],
  conditions: any = { sonK: 'children', k: 'value', v: '123' },
  _response: any = []
) => {
  _response = array
  for (let k = 0; k < _response.length; k++) {
    const e = _response[k]
    // console.log(e[conditions.sonK].length, e, k, e[conditions.k], conditions.v)
    if (e[conditions.sonK].length === 0 && e[conditions.k] !== conditions.v) {
      _response.splice(k, 1)
      k--
    }
    e[conditions.sonK] && e[conditions.sonK].length > 0 && patDown2(e[conditions.sonK], conditions, _response)
  }
  return _response
}

/**
 * 无限极修改对象数据
 * @param {Array} data
 * @param {function} callback
 */
export const editorAll = (data = [], callback, children = 'children') => {
  data.forEach((e, i) => {
    callback(e, data, i)
    if (e[children] && e[children].length > 0) editorAll(e[children], callback, children)
  })
}

/**
 * 只有当前同子节点的child为空才会给该末子节点增加特定class
 *
 * ant 树样式重写
 * scss => .ant-tree-list-holder-inner {display: block !important;.inlineTreeNodeSon {display: inline-flex;}}
 *
 * @param {Array} data
 * @returns
 */
export const inlineTreeData = (data: Array<any>) => {
  // 当前同级节点的数量
  let count = data.length
  // 计算同级节点的子节点为空数量
  data.forEach((e) => (e.children && e.children.length > 0 ? inlineTreeData(e.children) : --count))
  // 当同节点的所有子节点都为空时给子节点增加自定义class
  !count && data.forEach((item) => (item['class'] = 'inlineTreeNodeSon'))
  // console.log(data.length, count, data[0].key)
  return data
}

/**
 * 密码格式效验
 * 密码必须为大写英文、小写英文、数字、英文特殊字符组合的8-16位值,且不包含3位以上连续重复字符!
 * @param {String} value
 * @returns Boolean
 */
export const testPassword = (value: string) => {
  let type = true
  const reg = new RegExp(
    /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*[_!@#\$%\^&\*`~()\-\+=])[0-9a-zA-Z_!@#\$%\^&\*`~()\-\+=]{8,16}$/
  )
  if (reg.test(value)) {
    const arr = value.split('')
    for (let i = 0; i < arr.length - 2; i++) {
      if (arr[i] === arr[i + 1] && arr[i] === arr[i + 2]) {
        console.log('不合规', arr[i], arr[i + 1], arr[i + 2])
        type = false
        break
      }
    }
  } else {
    type = false
  }
  return type
}

/** 文件大小转换 */
export const fileSizeConversion = (e: string | number) => {
  const num = parseInt(`${e}`)
  if (num > 1024 * 1024) {
    return (num / 1024 / 1024).toFixed(2) + ' MB'
  } else if (num > 1024) {
    return (num / 1024).toFixed(2) + ' KB'
  } else {
    return num + ' B'
  }
}

/**
 * Base64 svg 转换 png
 * @param svg 
 * @returns 
 */
export const base64Svg2Png = (svg: string) => {
  return new Promise((resolve, reject) => {
    if (svg.indexOf('svg+xml') !== -1) return resolve(svg)
    const img = new Image(); // 创建图片容器
    img.src = svg; //imageBase64 为svg+xml的Base64 文件流
    img.onload = () => {
      // 图片创建后再执行,转Base64过程
      const canvas = document.createElement('canvas');
      canvas.width = img.width; //设置好 宽高  不然图片 不完整
      canvas.height = img.height;
      const context = canvas.getContext('2d');
      context.drawImage(img, 0, 0);
      let ImgBase64 = canvas.toDataURL('image/png');
      // console.log(ImgBase64, 'Svg 转 png');
      //ImgBase64 为转换后的 png ImgBase64 文件流
      resolve(ImgBase64);
    }
  })
}

/** 文件下载 */
export const downloadFileInIframe = (url = '') => {
  const iframe = document.createElement("iframe");
  iframe.src = url;
  iframe.style.cssText = "width: 0; height: 0;border:0;";
  console.log(iframe);
  document.body.appendChild(iframe);
  setTimeout(() => {
    iframe.remove();
  }, 200);
}