util.js 4.32 KB
import cookies from "./util.cookies"
import db from "./util.db"
import log from "./util.log"
import permision from "./util.permision"
const util = {
  cookies,
  db,
  log,
  permision
}

/**
 * @description 更新标题
 * @param {String} title 标题
 */
util.title = function (titleText) {
  const processTitle = process.env.VUE_APP_TITLE
  window.document.title = `${processTitle}${titleText ? ` | ${titleText}` : ""}`
}

/**
 * @description 打开新页面
 * @param {String} url 地址
 */
util.open = function (url, target) {
  var a = document.createElement("a")
  a.setAttribute("href", url)
  a.setAttribute("target", target || "_blank")
  a.setAttribute("id", "d2admin-link-temp")
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(document.getElementById("d2admin-link-temp"))
}
/**
 * 深克隆 所有原型链上的对象和属性
 * 例: const bar=deepClone(foo)
 * console.log(bar===foo) false
 * @param {*} obj
 */

util.deepClone = (obj) => {
  if (typeof obj !== "object") {
    return obj
  }
  if (Array.isArray(obj)) {
    return util.deepCloneArray(obj)
  } else {
    return util.deepCloneObj(obj)
  }
}

util.deepCloneArray = (arr) => {
  // function deepCloneArray(arr) {
  const target = []
  arr.forEach((it) => {
    if (typeof it === "object") {
      target.push(util.deepClone(it))
    } else {
      target.push(it)
    }
  })
  return target
}

util.deepCloneObj = (target) => {
  const map = new WeakMap()

  function isObject(target) {
    return (
      (typeof target === "object" && target) || typeof target === "function"
    )
  }

  function clone(data) {
    if (!isObject(data)) {
      return data
    }
    if ([Date, RegExp].includes(data.constructor)) {
      return new data.constructor(data)
    }

    // 会在生产环境的clone props对象报错 导致报错 弃用
    if (typeof data === "function") {
      return new Function("return " + data.toString())()
    }

    const exist = map.get(data)
    if (exist) {
      return exist
    }
    const keys = Reflect.ownKeys(data)
    const allDesc = Object.getOwnPropertyDescriptors(data)
    const result = Object.create(Object.getPrototypeOf(data), allDesc)
    map.set(data, result)
    keys.forEach((key) => {
      const val = data[key]
      if (isObject(val)) {
        result[key] = clone(val)
      } else {
        result[key] = val
      }
    })
    return result
  }

  return clone(target)
}

/**
 * 深度递归搜索
 * @param {Array} arr 你要搜索的数组
 * @param {Function} condition 回调函数,必须返回谓词,判断是否找到了。会传入(item, index, level)三个参数
 * @param {String} children 子数组的key
 */
util.deepFind = function (menus, id) {
  const cloneMenus = util.deepClone(menus)
  for (const menu of cloneMenus) {
    if (menu.id === id) {
      delete menu.children
      return menu
    }
    if (menu.children) {
      return util.deepFind(menu.children, id)
    }
  }
  return null
}

/**
 * 16进制颜色值 转换为 rgb
 * @param {*} sColor  16进制颜色值
 * @param {*} alpha 透明度
 */
util.colorToRgb = (sColor, alpha) => {
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
  sColor = sColor.toLowerCase()
  if (sColor && reg.test(sColor)) {
    if (sColor.length === 4) {
      var sColorNew = "#"
      for (var i = 1; i < 4; i += 1) {
        sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
      }
      sColor = sColorNew
    }
    // 处理六位的颜色值
    var sColorChange = []
    for (let i = 1; i < 7; i += 2) {
      sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)))
    }
    if (alpha || alpha === 0 || alpha === "0") {
      return `rgba(${sColorChange.join(",")} ,${alpha})`
    }
    return `rgb(${sColorChange.join(",")})`
  } else {
    return sColor
  }
}

/**
 * 重置form
 * 用法: util.resetForm.call(this,'formName')
 */
util.resetForm = function (formName) {
  this.$nextTick(() => {
    for (var key in this[formName]) {
      if (this[formName].hasOwnProperty(key)) {
        this[formName][key] = ""
      }
    }
    this.$refs[formName] && this.$refs[formName].resetFields()
  })
}

/**
 * 格式化时间
 * @param {Array} time
 */
util.formatTime = function (time) {
  return {
    startTime: time ? time[0] + " " + "00:00:00" : "",
    endTime: time ? time[1] + " " + "23:59:59" : ""
  }
}

export default util
export const deepClone = util.deepClone