commonUtility.js 8.32 KB
Newer Older
何虹's avatar
何虹 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

import _ from 'lodash'
const emptyList = [undefined, null]
// 解析url的问号参数
function parseUrlQueryString() {
  const host = decodeURIComponent(location.hash)
  const queryObj = {}
  if (host.includes('?')) {
    const query = host.split('?')[1]
    const queryList = query.split('&')
    queryList.forEach(item => {
      const iObj = item.split('=')
      queryObj[iObj[0]] = iObj[1]
    })
  }
  return queryObj
}
// 获取登录信息
function parseGlobalParams() {
  const globalParam = {}
  let user_info = sessionStorage['user_info']
  if (user_info && user_info !== 'undefined') {
    user_info = JSON.parse(user_info)
  }
  Object.assign(globalParam, user_info)
  return globalParam
}
// 解析queryBi dync页面路由上的appCode 和pageName
function parseUrlQueryBIDyncAppcodePageName() {
  const hash = decodeURIComponent(location.hash)
  const hostSplit = hash.split('?')
  let target = hostSplit[0]
  // 如果说最后一位是‘/’ 则删除掉
  if (target.endsWith('/')) {
    target = target.substr(0, target.length - 1)
  }
  const targetSplit = target.split('/')
  const length = targetSplit.length
  let pagename = targetSplit[length - 1]
  const appCode = targetSplit[length - 2]
  if (pagename.includes('/')) {
    pagename = pagename.split('/')[0]
  }
  if (pagename.includes('?')) {
    pagename = pagename.split('?')[0]
  }
  return {
    appCode: appCode,
    pagename: pagename
  }
}
// 解析动态菜单参数和全局参数MenuParams GlobalParams
function parseMenuGlobalParams(str, params) {
  const { menuParam, globalParam, otherParam } = params
  const empyt = [undefined]
  if (typeof str !== 'string' || !str.includes('{') || !str.includes('}')) return str
  const reg = /\{(.*?)\}/g //  /(?<=\{).*?(?=\})/g
  const strList = str.match(reg) || []// value_.match(/(?<=\{).*?(?=\})/g) || [] // value_.match(reg) || [] //
  strList.forEach(p => {
    const p_ = p.replace('{', '').replace('}', '')
    const props = p_.split('.')
    const sourceStr = props[0]
    const propStr = props[1]
    let pt = ''
    if (sourceStr === 'menuParam') {
      if (empyt.includes(menuParam[propStr])) {
        pt = p
      } else {
        pt = menuParam[propStr]
      }
    } else if (sourceStr === 'globalParam') {
      if (empyt.includes(globalParam[propStr])) {
        pt = p
      } else {
        pt = globalParam[propStr]
      }
    } else {
      if (empyt.includes(otherParam[propStr])) {
        pt = p
      } else {
        pt = otherParam[propStr]
      }
    }
    pt = decodeURIComponent(pt)
    if (pt === 'false' || pt === 'true') {
      str = pt !== 'false'// str.replace(`{${p_}}`, pt)
    } else {
      str = str.replace(`{${p_}}`, pt)
    }
  })
  return str
}
// 递归替换动态参数
function recursiveDyncParam(obj, otherOBj = {}) {
  const parseUrlQueryParams = parseUrlQueryString() || {}
  const parseGlobalParams_ = parseGlobalParams() || {}
  const parms = {
    menuParam: parseUrlQueryParams, globalParam: parseGlobalParams_, otherParam: otherOBj
  }
  const objType = typeof obj
  if (Array.isArray(obj)) {
    obj.forEach((item, index) => {
      if (typeof item === 'object') {
        this.recursiveDyncParam(item, otherOBj)
      } else {
        obj[index] = parseMenuGlobalParams(item, parms)
      }
    })
  } else if (objType === 'object') {
    for (const prop in obj) {
      if (typeof obj[prop] === 'object') {
        this.recursiveDyncParam(obj[prop], otherOBj)
      } else {
        obj[prop] = parseMenuGlobalParams(obj[prop], parms)
      }
    }
  } else {
    return parseMenuGlobalParams(obj, parms)
  }
}
function filterSingleRow(newObj, oldObj, prop = 'id') {
  delete newObj.isSelected
  delete oldObj.isSelected
  const _newObj = _.cloneDeep(newObj)
  const _oldObj = _.cloneDeep(oldObj)
  const result = {}
  for (const p in _newObj) {
    if ((_oldObj[p] !== _newObj[p] && !emptyList.includes(_newObj[p])) || p === prop) {
      result[p] = _newObj[p]
    }
  }
  return result
}
// newData:当前数据.oldData:原始数据,primaryKey;主键id,reservedObj:保留字段
function comparisonArray(newData_, oldData_, primaryKey, reservedArray = []) {
  const newData = _.cloneDeep(newData_)
  const oldData = _.cloneDeep(oldData_)
  const emptyValue = [undefined, 'undefined', '']
  // 1,找出原始数据中所有的id
  const old_ids = oldData.reduce((pre, curr) => {
    const id = curr[primaryKey]
    if (id) {
      pre.push(id)
    }
    return pre
  }, [])
  // 2,找出新数据中所有项的id
  const new_ids = newData.reduce((pre, curr) => {
    const id = curr[primaryKey]
    if (id) {
      pre.push(id)
    }
    return pre
  }, [])

  // 3,找出删除项
  const delete_ids = old_ids.filter(item => {
    if (!new_ids.includes(item)) {
      return item
    }
  })
  const newItems = newData_.filter(item => {
    if (item.iUpdateStatus === 1) {
      return item
    }
  })

  // 找出修改项目
  const edit_item = newData.filter(item => {
    delete item.isSelected
    const id = item[primaryKey]
    const curr_old_item = oldData.find(element => {
      if (element[primaryKey] === id && item.iUpdateStatus !== 1) {
        return element
      }
    })
    if (id && curr_old_item) {
      // 找出变化的字段 并且保留必要字段
      for (const key in curr_old_item) {
        const hasKey = key in item
        if (!hasKey) { // 如果这个字段不在现有行中 则视为删除了
          item[key] = ''
        } else { // 如果字段对应的值相等 则视为 没更改 提交数据时把这个这段排除
          if (curr_old_item[key] === item[key] && key !== primaryKey && !reservedArray.includes(key)) {
            delete item[key]
          }
        }
      }
      const length1 = Object.keys(item).length
      if (length1 > 1) {
        return item
      }
    }
  })
  // 组装数据
  // 为新增项加一个标示
  newItems.forEach((item, index) => {
    delete item.isSelected
    // 删除空值
    for (const key in item) {
      const value = item[key]
      if (emptyValue.includes(value)) {
        delete item[key]
      }
    }
    delete item._columns
  })
  // 筛选出非空项
  const newItems_ = newItems.filter(item => Object.keys(item).length)
  // 为修改项加标示
  edit_item.filter(item => Object.keys(item).length).forEach(item => {
    delete item.isSelected
    for (const k in item) {
      if (item[k] === undefined) {
        item.k = ''
      }
    }
    if (Object.keys(item).length > 1) {
      item.iUpdateStatus = 2
    }
  })
  const delete_items = []
  delete_ids.forEach(item => {
    const parm = {
      iUpdateStatus: 4
    }
    parm[primaryKey] = item
    delete_items.push(parm)
  })
  const result = [...edit_item, ...delete_items, ...newItems_]
  return result
}
function comparisonJson(newData_, oldData_, primaryKey, reservedArray = []) {
  const newData = _.cloneDeep(newData_)
  const oldData = _.cloneDeep(oldData_)
  const emptyValue = [undefined, 'undefined']
  const iUpdateStatus = newData.iUpdateStatus
  if (iUpdateStatus === 1) { // 新增
    for (const key in newData) {
      const value = newData[key]
      if (emptyValue.includes(value)) {
        delete newData[key]
      }
    }
  } else { // 修改
    for (const key in newData) {
      if (key === '') {
        debugger
        delete newData[key]
      }
      if (newData[key] === oldData[key] && key !== primaryKey) {
        delete newData[key]
      }
      newData.iUpdateStatus = 2
    }
  }
  delete newData._columns
  return newData
}
// 递归替换action 函数
function recursiveReplaceAction(obj, activeManager) {
  for (const k in obj) {
    const value = obj[k]
    const type_k = typeof value
    if (type_k === 'string') {
      this.replaceItemAction(obj, activeManager)
    } else if (type_k === 'object') {
      this.recursiveReplaceAction(value, activeManager)
    }
  }
}
function replaceItemAction(item, activeManager) {
  const actionProps = ['isHide', 'click', 'disabled', 'changed', 'cleared']
  actionProps.forEach(k => {
    const type = typeof item[k]
    if (type === 'string' && !item[k].startsWith('$')) {
      const funName = '$' + item[k]
      item[k] = activeManager[funName]
    }
  })
}
export default {
  parseUrlQueryString,
  parseGlobalParams,
  parseUrlQueryBIDyncAppcodePageName,
  parseMenuGlobalParams,
  recursiveDyncParam,
  filterSingleRow,
  comparisonJson,
  comparisonArray,
  _,
  cloneDeep: _.cloneDeep, // 深拷贝
  recursiveReplaceAction,
  replaceItemAction
}