text: import config from '@/config'
  import Cookies from 'js-cookie'
  const { title, cookieExpires, useI18n } = config
  const TOKEN_KEY = 'token'
  
  /**
   * @description 获取当前时间指定前后今天的日期
   * @param {Number} days 相差的天数 获取当天时不能省略=>传 0
   */
  export const getDate = (days) => {
    let dd = new Date()
    dd.setDate(dd.getDate() + days) // 获取days天后的日期
    let y = dd.getFullYear()
    let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) // 获取当前月份的日期，不足10补0
    let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
    return m + d
  }
  
  /**
   * @description 时间戳转换成正常时间
   * @param {string} time 时间戳
   */
  export const getTime = (time) => {
    let dd = new Date(time)
    dd.setDate(dd.getDate())
    let y = dd.getFullYear()
    let m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) // 获取当前月份的日期，不足10补0
    let d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号，不足10补0
    return y + '-' + m + '-' + d
  }
  
  /**
   * @description 获取地址栏参数
   * @param {String} name 参数名
   */
  export const getUrlKey = name => {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || ['', ''])[1].replace(/\+/g, '%20')) || null
  }
  
  /**
     * 获取URL中的？后面的参数
     * @returns {string}
     */
  export const getUrlParam = () => {
    return document.location.toString().split('?')[1]
  }
  
  /**
   * 根据URL中的？后面的参数封装成对象
   * 去掉#之前的
   * @param param
   */
  export const getUrlParamObj = (param) => {
    let obj = {}
    if (param) {
      let urlParam = param.indexOf('&') !== -1 ? param.split('&') : param
      if (urlParam instanceof Array) {
        urlParam.forEach(item => {
          let objParam = item.split('=')
          let key = objParam[0]
          obj[key] = objParam[1].indexOf('#') !== -1 ? decodeURI(objParam[1].split('#')[0]) : decodeURI(objParam[1])
        })
      }
    }
    return obj
  }
  
  // cookie方法封装
  export const delToken = () => {
    Cookies.remove(TOKEN_KEY)
  }
  
  export const setToken = (token) => {
    Cookies.set(TOKEN_KEY, token, { expires: cookieExpires })
  }
  
  export const getToken = () => {
    const token = Cookies.get(TOKEN_KEY)
    if (token) return token
    else return false
  }
  
  // localStorage方法封装
  export const localSave = (key, value) => {
    localStorage.setItem(key, JSON.stringify(value))
  }
  
  export const localRead = (key) => {
    return localStorage.getItem(key) || ''
  }
  
  export const localRmove = (key) => {
    if (!key) return
    return localStorage.removeItem(key)
  }
  
  export const hasChild = (item) => {
    return item.children && item.children.length !== 0
  }
  
  /**
   * @param {Array} target 目标数组
   * @param {Array} arr 需要查询的数组
   * @description 判断要查询的数组是否至少有一个元素包含在目标数组中
   */
  export const hasOneOf = (targetarr, arr) => {
    return targetarr.some(_ => arr.indexOf(_) > -1)
  }
  
  const showThisMenuEle = (item, access) => {
    if (item.meta && item.meta.access && item.meta.access.length) {
      if (hasOneOf(item.meta.access, access)) return true
      else return false
    } else return true
  }
  /**
   * @param {Array} list 通过路由列表得到菜单列表
   * @returns {Array}
   */
  export const getMenuByRouter = (list, access) => {
    let res = []
    list.forEach(item => {
      if (!item.meta || (item.meta && !item.meta.hideInMenu)) {
        let obj = {
          icon: (item.meta && item.meta.icon) || '',
          name: item.name,
          meta: item.meta
        }
        if ((hasChild(item) || (item.meta && item.meta.showAlways)) && showThisMenuEle(item, access)) {
          obj.children = getMenuByRouter(item.children, access)
        }
        if (item.meta && item.meta.href) obj.href = item.meta.href
        if (showThisMenuEle(item, access)) res.push(obj)
      }
    })
    return res
  }
  
  /**
  * @description: 判断打开的标签列表里是否已存在这个新添加的路由对象
   */
  export const routeHasExist = (tagNavList, routeItem) => {
    let len = tagNavList.length
    let res = false
    doCustomTimes(len, (index) => {
      if (routeEqual(tagNavList[index], routeItem)) res = true
    })
    return res
  }
  
  /**
   * @description: 执行N次回调函数
   * @param {Number} times 回调函数需要执行的次数
   * @param {Function} callback 回调函数
   */
  export const doCustomTimes = (times, callback) => {
    let i = -1
    while (++i < times) {
      callback(i)
    }
  }
  
  /**
   * @description 根据name/params/query判断两个路由对象是否相等
   * @param {*} route1 路由对象
   * @param {*} route2 路由对象
   */
  export const routeEqual = (route1, route2) => {
    const params1 = route1.params || {}
    const params2 = route2.params || {}
    const query1 = route1.query || {}
    const query2 = route2.query || {}
    return (route1.name === route2.name) && objEqual(params1, params2) && objEqual(query1, query2)
  }
  
  /**
   * @param {Array} routers 路由列表数组
   * @description 用于找到路由列表中name为home的对象
   */
  export const getHomeRoute = (routers, homeName = 'home') => {
    let i = -1
    let len = routers.length
    let homeRoute = {}
    while (++i < len) {
      let item = routers[i]
      if (item.children && item.children.length) {
        let res = getHomeRoute(item.children, homeName)
        if (res.name) return res
      } else {
        if (item.name === homeName) homeRoute = item
      }
    }
    return homeRoute
  }
  
  /**
   * @description 判断两个对象是否相等，这两个对象的值只能是数字或字符串
   * @param {*} obj1 对象
   * @param {*} obj2 对象
   */
  export const objEqual = (obj1, obj2) => {
    const keysArr1 = Object.keys(obj1)
    const keysArr2 = Object.keys(obj2)
    if (keysArr1.length !== keysArr2.length) return false
    else if (keysArr1.length === 0 && keysArr2.length === 0) return true
    /* eslint-disable-next-line */
    else return !keysArr1.some(key => obj1[key] != obj2[key])
  }
  
  /**
   * 鉴权
   * @param {*} name 即将跳转的路由name
   * @param {*} access 用户权限数组
   * @param {*} routes 路由列表
   * @description 用户是否可跳转到该页
   */
  export const canTurnTo = (name, access, routes) => {
    const routePermissionJudge = (list) => {
      return list.some(item => {
        if (item.children && item.children.length) {
          return routePermissionJudge(item.children)
        } else if (item.name === name) {
          return hasAccess(access, item)
        }
      })
    }
  
    return routePermissionJudge(routes)
  }
  
  /**
   * @param {*} access 用户权限数组
   * @param {*} route 路由列表
   */
  const hasAccess = (access, route) => {
    if (route.meta && route.meta.access) return hasOneOf(access, route.meta.access)
    else return true
  }
  
  /**
   * @description 根据当前跳转的路由设置显示在浏览器标签的title
   * @param {Object} routeItem 路由对象
   * @param {Object} vm Vue实例
   */
  export const setTitle = (routeItem, vm) => {
    const handledRoute = getRouteTitleHandled(routeItem)
    const pageTitle = showTitle(handledRoute, vm)
    const resTitle = pageTitle ? `${title} - ${pageTitle}` : title
    window.document.title = resTitle
  }
  
  export const getRouteTitleHandled = (route) => {
    let router = { ...route }
    let meta = { ...route.meta }
    let title = ''
    if (meta.title) {
      if (typeof meta.title === 'function') {
        meta.__titleIsFunction__ = true
        title = meta.title(router)
      } else title = meta.title
    }
    meta.title = title
    router.meta = meta
    return router
  }
  
  export const showTitle = (item, vm) => {
    let { title, __titleIsFunction__ } = item.meta
    if (!title) return
    if (useI18n) {
      if (title.includes('{{') && title.includes('}}') && useI18n) title = title.replace(/({{[\s\S]+?}})/, (m, str) => str.replace(/{{([\s\S]*)}}/, (m, _) => vm.$t(_.trim())))
      else if (__titleIsFunction__) title = item.meta.title
      else title = vm.$t(item.name)
    } else title = (item.meta && item.meta.title) || item.name
    return title
  }
  