常用的小工具函数
import _ from 'lodash'export const isEmpty = params => { let result = false if ( params === undefined || params === null || params === [] || params === '' ) { result = true } if (typeof params === 'object') { result = _.isEmpty(params) } if (typeof params === 'string') { params = _.trim(params) result = params === '' } return result}export const hasStarSymbol = params => { return params.indexOf('****') === -1}export const isNotEmpty = params => { return !isEmpty(params)}export const isUrl = url => { return url && url.includes('http')}// 验证区号是否是三到四位数的合法区号export const isValidAreacode = str => { let result = false if (!str || isEmpty(str)) { result = false } if (str && str.length > 0) { result = _.every(_.split(str, '-'), v => { return (v.length === 3 || v.length === 4) && v.charAt(0) === '0' }) } return result}// 用于生成 key 防止组件缓存export const getUUID = () => { let key = Math.random() .toString(36) .substring(3, 8) return `crm${key}`}// 过滤掉无用请求参数export const filterQueryParams = (obj, ...args) => { const queryParams = JSON.parse(JSON.stringify(obj)) args.forEach(x => { Reflect.deleteProperty(queryParams, x) }) return queryParams}// 提取有用的请求参数export const filterUseQueryParams = (obj, args = []) => { const queryParams = {} const objCopy = JSON.parse(JSON.stringify(obj)) args.forEach(x => { queryParams[x] = objCopy[x] if (objCopy.hasOwnProperty(x + 'MaskSec')) { queryParams[x + 'MaskSec'] = objCopy[x + 'MaskSec'] } if (objCopy[x + 'Mask']) { queryParams[x + 'Mask'] = objCopy[x + 'Mask'] } }) return queryParams}// 清空obj中对应的属性export const clearObjValues = (obj, ...args) => { args && args.forEach(x => { if (obj && x && obj[x]) { obj[x] = '' } }) return obj}// 处理显示的串undefined null为空串export const handlerDisplay = str => { if (str && typeof str === 'string') { str = str.replace(/(undefined)|(null)/gi, '').trim() } return str}// 处理 loading 的显示效果let cacheEl = document.querySelector('.el-loading-mask.is-fullscreen')export const loading = (flag = true) => { if (!cacheEl) { cacheEl = document.querySelector('.el-loading-mask.is-fullscreen') } if (flag) { // 修复loading 框偏移的问题 // cacheEl.style.width = '1296px' cacheEl.style.display = 'block' } else { cacheEl.style.display = 'none' }}// 处理选项排到第一位export const getFmsBalanceAccountModuleList = (arr, text = 'DefaultChooseLabel') => { let result = arr if (Array.isArray(arr) && arr.length > 0) { let index = arr.findIndex(x => x.area === text) if (index >= 0) { let matchOne = arr[index] arr.splice(index, 1) arr.unshift(matchOne) } } return result}// 全部为空export const isAllEmpty = (obj, arr = []) => { let flag = true let values = arr.map(x => obj[x]) || [] flag = values.every(x => isEmpty(x)) return flag}// 全部必填export const isAllFill = (obj, arr = []) => { let flag = true let values = arr.map(x => obj[x]) || [] flag = values.every(x => !isEmpty(x)) return flag}// 保留 2 位小数// 计提保留2位小数 由后台数据库存储的时候处理好 3-> 3.00export const twoDec = (val, precision = 2) => { let res = '' if (isEmpty(val)) { return res } if (val) { val = Number(val).toFixed(precision) res = `${val}` } else if (val === 0 || val === 0.0) { val = Number(val).toFixed(precision) res = `${val}` } else { res = '' } return res}// 根据组件名字查找子组件,深度优先遍历// 向下找到最近的指定组件export function findComponentDownward (context, componentName) { const childrens = context.$children let children = null if (childrens.length) { for (const child of childrens) { const name = child.$options.name if (name === componentName) { children = child break } else { children = findComponentDownward(child, componentName) if (children) break } } } return children}// 计算多个值的求和export const calcTotal = (arr) => { let myarr = _.compact(arr).map(x => Number(x)) let res = myarr.reduce((prev, curr, index, array) => { if (!_.isFinite(curr)) { curr = 0 } return Number(curr) + Number(prev) }, 0) return res}// 用于判断区号重复export const duplicates = (arr) => { var newArr = [] for (var i = 0; i < arr.length; i++) { var count = 0 for (var j = 0; j < arr.length; j++) { if (arr[i] === arr[j]) { count++ } } if (count > 1 && newArr.indexOf(arr[i]) === -1) { newArr.push(arr[i]) } } return newArr.join(',')}// 判断权限 在列表中就不去发请求export const isIntoNOAuthSilentMethodList = (vm, currentMethod) => { let menus = vm.$store.getters.permission.menus let result = false result = (!menus.includes(currentMethod) && NO_AUTH_SILENT_METHOD_LIST.includes(currentMethod)) console.log(`${currentMethod} is ${result}`) return result}function customFilter (x, key, currentOptions) { let result = false if (!currentOptions[key]) { result = true } else { result = x['extObj'][key] + '' === currentOptions[key] } return result}// 服务方式过滤// 观察DOMexport const observeDom = (el, options, callback) => { var defaultOptions = { childList: true, subtree: true, attributes: true, attributeOldValue: true, characterData: true } var currentOptions = Object.assign(defaultOptions, options) var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver var observer = new MutationObserver(callback) observer.observe(el, currentOptions)}