派生自 wuyushui/SewerAndRainNetwork

徐旺旺
2021-01-13 0dedf75a903b8502b94d5d03679b42aa306bb18a
src/utils/tools.js
@@ -1,24 +1,23 @@
/* eslint-disable no-prototype-builtins */
import { OAUTH_API_GROUP } from '@/utils/authorityAPI'
import * as $CONST from './constant'
import { Message } from 'element-ui'
// import { notify } from '@nutui/nutui'
export const _ = require('lodash')
/**
 * 集合转换为JSON
 * @param obj collection数据
 * @author TJ 2019/03/11
 * @example 略
 */
export function collectionToJson(collection) {
export function collectionToJson (collection) {
  if (!_.isArray(collection)) return []
  let arr = []
  const arr = []
  // 数据体
  _.forEach(collection, function(item) {
    let tempObj = {}
    if (item.hasOwnProperty('data') && _.isArray(item.data)) {
      _.forEach(item.data, function(obj) {
  _.forEach(collection, function (item) {
    const tempObj = {}
    if (Object.prototype.hasOwnProperty.call(item, 'data') && _.isArray(item.data)) {
      _.forEach(item.data, function (obj) {
        tempObj[_.trim(obj.name)] = _.trim(obj.value)
      })
    }
@@ -33,14 +32,14 @@
 * @author TJ 2019/03/11
 * @example 略
 */
export function transformStandardJson(obj) {
export function transformStandardJson (obj) {
  let body = {}
  let data = null
  let pages = null
  let collection = obj.collection
  const collection = obj.collection
  if (_.isObject(collection)) {
    // 错误的场景
    if (collection.hasOwnProperty('error')) {
    if (Object.prototype.hasOwnProperty.call(collection, 'items')) {
      return {
        code: 300,
        body: null,
@@ -48,11 +47,11 @@
      }
    }
    // 数据体
    if (collection.hasOwnProperty('items') && _.isArray(collection.items)) {
    if (Object.prototype.hasOwnProperty.call(collection, 'items') && _.isArray(collection.items)) {
      data = collectionToJson(collection.items)
    }
    // 分页信息
    if (collection.hasOwnProperty('page') && _.isArray(collection.data)) {
    if (Object.prototype.hasOwnProperty.call(collection, 'page') && _.isArray(collection.data)) {
      pages = collectionToJson([collection.page])
    }
    // 组装body数据
@@ -84,16 +83,16 @@
 * @author TJ 2019/03/11
 * @example 略
 */
export function transformParams(obj) {
  let collection = {
export function transformParams (obj) {
  const collection = {
    version: '1.0',
    href: '',
    items: [],
    templates: []
  }
  if (_.isObject(obj)) {
    let arr = []
    _.each(obj, function(val, key) {
    const arr = []
    _.each(obj, function (val, key) {
      let newVal = val
      if (_.isArray(val)) {
        newVal = val.join(',')
@@ -104,7 +103,7 @@
      })
    })
    let o = {
    const o = {
      data: arr
    }
    collection.templates.push(o)
@@ -121,9 +120,9 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function setSessionStorage(key, content) {
export function setSessionStorage (key, content) {
  if (!key) return false
  let jsonContent = JSON.stringify(content)
  const jsonContent = JSON.stringify(content)
  jsonContent ? sessionStorage.setItem(key, jsonContent) : sessionStorage.setItem(key, content)
}
@@ -134,11 +133,11 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function getSessionStorage(key) {
  let item = sessionStorage.getItem(key)
export function getSessionStorage (key) {
  const item = sessionStorage.getItem(key)
  if (!item) return false
  let result = JSON.parse(sessionStorage.getItem(key))
  return result ? result : item
  const result = JSON.parse(sessionStorage.getItem(key))
  return result || item
}
/**
@@ -147,7 +146,7 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function removeSessionStorage(key) {
export function removeSessionStorage (key) {
  sessionStorage.removeItem(key)
}
@@ -158,9 +157,9 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function setLocalStorage(key, content) {
export function setLocalStorage (key, content) {
  if (!key) return false
  let jsonContent = JSON.stringify(content)
  const jsonContent = JSON.stringify(content)
  jsonContent ? localStorage.setItem(key, jsonContent) : localStorage.setItem(key, content)
}
@@ -171,11 +170,11 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function getLocalStorage(key) {
  let item = localStorage.getItem(key)
export function getLocalStorage (key) {
  const item = localStorage.getItem(key)
  if (!item) return false
  let result = JSON.parse(localStorage.getItem(key))
  return result ? result : item
  const result = JSON.parse(localStorage.getItem(key))
  return result || item
}
/**
@@ -184,7 +183,7 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function removeLocalStorage(key) {
export function removeLocalStorage (key) {
  localStorage.removeItem(key)
}
@@ -195,7 +194,7 @@
 * @author TJ 2018/05/28
 * @example 略
 */
export function isEmptyObject(obj) {
export function isEmptyObject (obj) {
  if (obj === null) return true
  return Object.keys(obj).length === 0
}
@@ -207,15 +206,15 @@
 * @author TJ 2017/05/28
 * @example 略
 */
export function filterParams(params) {
export function filterParams (params) {
  if (!_.isObject(params)) {
    return params
  }
  let newParams = {}
  _.each(params, function(v, k) {
  const newParams = {}
  _.each(params, function (v, k) {
    // 过滤掉条件是空的项
    if (typeof v === 'string' && (v.length === 0 || v === '*全*部*')) {
        console.log()
    } else {
      newParams[k] = v
    }
@@ -228,7 +227,7 @@
 * @param params {obj}
 * @author TJ 2018/05/31
 */
export function emptyObjectWrapper(obj) {
export function emptyObjectWrapper (obj) {
  return obj === null ? {} : obj
}
@@ -240,7 +239,7 @@
 * @author TJ 2017/07/21
 * @example 略
 */
export function parseTime(time, cFormat) {
export function parseTime (time, cFormat) {
  if (!time) return false
  if (arguments.length === 0) {
    return false
@@ -284,7 +283,7 @@
 * @author TJ 2017/08/01
 * @example 略
 */
export function formatTime(time) {
export function formatTime (time) {
  if (Array.isArray(time)) {
    if (!time[0] || !time[1]) return false
    var start = parseTime(time[0], '{y}-{m}-{d}')
@@ -299,32 +298,14 @@
}
/**
 * 判断请求的api是否是要追加权限的
 * @param api {String}
 * @return {boolean}
 * @author TJ 2017/08/01
 * @example 略
 */
export function isExistOauthApi(api) {
  let isExist = false
  for (let item of OAUTH_API_GROUP) {
    if (api && api.includes(item)) {
      isExist = true
      break
    }
  }
  return isExist
}
/**
 * 权限API拼接用户名
 * @param api {String}
 * @return {boolean}
 * @author TJ 2017/08/01
 * @example 略
 */
export function getJoinOauthApi(url) {
  let name = window._loginName ? window._loginName : ''
export function getJoinOauthApi (url) {
  const name = window._loginName ? window._loginName : ''
  if (url.includes('?')) {
    url += '&createId=' + name
  } else {
@@ -341,17 +322,15 @@
 * @author TJ 2017/08/01
 * @example 略
 */
export function joinQueryUrl(url, obj) {
export function joinQueryUrl (url, obj) {
  let str = ''
  let fullUrl = ''
  for (let key in obj) {
    if (key) {
      if (!obj.hasOwnProperty(key)) return
      if (str) {
        str += '&'
      }
      str += key + '=' + obj[key]
  for (const key in obj) {
    if (!Object.prototype.hasOwnProperty.call(obj, key)) return
    if (str) {
      str += '&'
    }
    str += key + '=' + obj[key]
  }
  if (url.includes('?')) {
    fullUrl = url + '&' + str
@@ -367,7 +346,7 @@
 * @return {Boolean}
 * @author TJ 2018/08/27
 */
export function isInlineParams(url) {
export function isInlineParams (url) {
  const splitChar = '{$'
  if (url && url.indexOf(splitChar) > -1) {
    return true
@@ -382,70 +361,20 @@
 * @param params {obj}
 * @author TJ 2018/05/31
 */
export function replaceUrlParams(url, params) {
export function replaceUrlParams (url, params) {
  if (url) {
    if (!isInlineParams(url)) {
      return url
    }
    // 正则匹配{},生成数组
    let patt = /\{.*?\}/g
    let arr = url.match(patt) ? url.match(patt) : []
    arr.forEach(function(item) {
      let key = item.replace('{', '').replace('}', '').replace('$', '')
    const patt = /\{.*?\}/g
    const arr = url.match(patt) ? url.match(patt) : []
    arr.forEach(function (item) {
      const key = item.replace('{', '').replace('}', '').replace('$', '')
      url = url.replace(item, params[key])
    })
  }
  return url
}
/**
 * 获取传入小时之前的日期
 * @param pastHour {Number}
 * @param format {String}
 * @author ZC 2020/11/03
 */
export function zcGetDate(pastHour, format) {
  if (!format) {
    format = 'yyyy-MM-dd'
  }
  // eslint-disable-next-line no-extend-native
  Date.prototype.Format = function(fmt) {
    let o = {
      'M+': this.getMonth() + 1, // 月份
      'd+': this.getDate(), // 日
      'h+': this.getHours(), // 小时
      'm+': this.getMinutes(), // 分
      's+': this.getSeconds(), // 秒
      'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
      'S': this.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length))
    for (let k in o) {
      // eslint-disable-next-line eqeqeq
      if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
    return fmt
  }
  if (pastHour) {
    // 获取之前的时间
    return new Date(new Date().getTime() - pastHour * 60 * 60 * 1000).Format(format)
  } else {
    return new Date(new Date().getTime()).Format(format)
  }
}
/**
 * 获取传入小时之前的日期
 * @param url {String}
 * @author ZC 2020/12/09
 */
export function zcDownload(url) {
  let downloadElement = document.createElement('a')
  downloadElement.href = url // 创建
  downloadElement.download = '导出.zip' // 下载后文件名
  document.body.appendChild(downloadElement)
  downloadElement.click() // 点击下载
  document.body.removeChild(downloadElement) // 下载完成移除元素
}
/**
@@ -454,7 +383,7 @@
 * @author TJ 2017/10/17
 * @return 无返回结果
 */
export function resgisterTheme(echart) {
export function resgisterTheme (echart) {
  /* eslint-disable */
  let theme = {
    'color': [
@@ -967,7 +896,7 @@
 * 根据路由切换皮肤标识,默认皮肤未蓝色
 * @author TJ 2019/05/29
 */
export function changeThemeClass(toRouterName) {
export function changeThemeClass (toRouterName) {
  const darkSkinRouterName = []
  if (darkSkinRouterName.includes(toRouterName)) {
@@ -1004,7 +933,7 @@
 * 判断值是否为真,不包括对0的判断
 * @param {String} val 字符
 */
export function isTrue(val) {
export function isTrue (val) {
  return !_.isNull(val) && !_.isUndefined(val) && val !== ''
}
@@ -1014,13 +943,13 @@
 * @param {String} url
 * @return {object}
 */
export function getQueryObject(url = window.location.href) {
export function getQueryObject (url = window.location.href) {
  if (!url) return
  let search = url.substring(url.lastIndexOf('?') + 1)
  let obj = {}
  let reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, function(rs, $1, $2) {
    let name = decodeURIComponent($1)
  const search = url.substring(url.lastIndexOf('?') + 1)
  const obj = {}
  const reg = /([^?&=]+)=([^?&=]*)/g
  search.replace(reg, function (rs, $1, $2) {
    const name = decodeURIComponent($1)
    let val = decodeURIComponent($2)
    val = String(val)
    obj[name] = val
@@ -1037,12 +966,12 @@
 * @param {string} propValue 属性对应的值
 * @return {object} 节点对象
 */
export function recursion(tree, prop, propValue) {
export function recursion (tree, prop, propValue) {
  if (!tree || !Array.isArray(tree)) return false
  let result = {}
  function handelTree(tree) {
    for (let o of tree) {
  function handelTree (tree) {
    for (const o of tree) {
      if (o.children && Array.isArray(o.children) && o.children.length) {
        if (o[prop] !== propValue) {
          // 递归
@@ -1063,63 +992,39 @@
  return handelTree(tree)
}
export function success(msg = $CONST.MSG_SYS_SUCCESS) {
export function success (msg = $CONST.MSG_SYS_SUCCESS) {
  Message({
    message: msg,
    type: 'success'
  })
}
export function fail(msg = $CONST.MSG_SYS_FAIL) {
export function fail (msg = $CONST.MSG_SYS_FAIL) {
  Message({
    message: msg,
    type: 'error'
  })
}
export function error(msg = $CONST.MSG_SYS_ERR) {
export function error (msg = $CONST.MSG_SYS_ERR) {
  Message({
    message: msg,
    type: 'error'
  })
}
export function warning(msg = $CONST.MSG_SYS_WARNING) {
export function warning (msg = $CONST.MSG_SYS_WARNING) {
  Message({
    message: msg,
    type: 'warning'
  })
}
export function info(msg = $CONST.MSG_SYS_CANCELED) {
export function info (msg = $CONST.MSG_SYS_CANCELED) {
  Message({
    message: msg,
    type: 'info'
  })
}
export function alertError(msg = $CONST.MSG_SYS_ERR, options) {
  options = Object.assign({
    dangerouslyUseHTMLString: true,
    title: '提示',
    type: 'error',
    'show-icon': true
  }, options)
  window.vm.$alert(msg, options)
}
export function alertWarning(msg = $CONST.MSG_SYS_WARNING, options) {
  options = Object.assign({
    dangerouslyUseHTMLString: true,
    title: '提示',
    type: 'warning',
    'show-icon': true
  }, options)
  window.vm.$alert(msg, options)
}
export function todo() {
  warning('开发中。。。')
}
/**