export const isValue = (val: any) => { if (val === null || val === undefined || (typeof val === 'string' && val.trim() === '') || (typeof val === 'object' && val?.length === 0)) { return false } return true } export const structureParams = (array: Array<any>, attribute: string) => { const endArray: any[] = [] array.forEach(v => { endArray.push(v[attribute]) }) return endArray } export const replaceParams = (array: Array<any>, reArray: Array<any>, attribute: string, reAttribute: string) => { const endArray: any[] = [] const endAllArray: any[] = [] array.forEach(v => { reArray.forEach(rv => { if (v === rv[attribute]) { endArray.push(rv[reAttribute]) endAllArray.push(rv) } }) }) return { replace: endArray, all: endAllArray } } export const copyObject = (ob: Object) => { return JSON.parse(JSON.stringify(ob)) } export const arrayToMap = (array: Array<any>, key: any) => { const map = new Map() array.forEach((v: any) => { map.set(v[key], v) }) return map } /** * 通过某个字段在一个多级数组中查找数据 * @param data 目标数组,不能包含函数 * @param current 目标数据 * @param key 查找的字段 * @param children 子集集合字段 */ export const findInListData = (data: Array<any>, current:any, key = "id", children = 'children') => { for(let item of data){ if(item[key] && JSON.parse(JSON.stringify(item[key])) == JSON.parse(JSON.stringify(current))) return item if(!!item[children] && Array.isArray(item[children]) && item[children].length > 0){ const findChildData: any = findInListData(item[children], current, key, children) if(findChildData) return findChildData } } return null } export const formatGetParam = (params: any) => { let paramUrl = '' Object.keys(params).forEach((v, i) => { paramUrl += i === 0 ? `${v}=${encodeURIComponent(params[v])}` : `&${v}=${encodeURIComponent(params[v])}` }) return paramUrl } export const formatTableHeadFilters = (arr: Array<any>, text = 'dictLabel', value = 'dictValue') => { return arr.map(v => { v.value = v[value] v.text = v[text] return v }) } export const YMDHms = (date: any) => { const _date = new Date(date) const Y = `${_date.getFullYear()}`; const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`; const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`; const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`; const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`; const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds(); return `${Y}-${M}-${D} ${H}:${m}:${s}`; } export const YMD = (date: any, format = false) => { const _date = new Date(date) const Y = `${_date.getFullYear()}`; const M = `${_date.getMonth() + 1 < 10 ? `0${_date.getMonth() + 1}` : _date.getMonth() + 1}`; const D = `${_date.getDate() < 10 ? `0${_date.getDate()}` : _date.getDate()}`; return format ? `${Y}年${M}月${D}日` : `${Y}-${M}-${D}`; } export const Hms = (date: any, format = false) => { const _date = new Date(date) const H = `${_date.getHours() < 10 ? `0${_date.getHours()}` : _date.getHours()}`; const m = `${_date.getMinutes() < 10 ? `0${_date.getMinutes()}` : _date.getMinutes()}`; const s = _date.getSeconds() < 10 ? `0${_date.getSeconds()}` : _date.getSeconds(); return format ? `${H}时${m}分${s}秒` : `${H}:${m}:${s}`; } //防抖 export const debounce = function (cb: any, ms = 0) { let timer: any = null return function () { if (timer) clearTimeout(timer) timer = setTimeout(() => { // @ts-ignore cb.apply(this, arguments) timer = null }, ms) } } export const comTime = (time: any, format = false) => { const sAll = time const d = Math.floor(sAll / (1000 * 60 * 60 * 24)) const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60)) const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000) let result: any = {d, h, m ,s} if (format) { result = '' if (d > 0) { result += d + '天' } if (d > 0 || h > 0) { result += h + '时' } if (d > 0 || h > 0 || m > 0) { result += m + '分' } result += s + '秒' } return result } export const comTimeByArea = (start: any, end: any, format = false) => { const sAll = new Date(end).getTime() - new Date(start).getTime() const d = Math.floor(sAll / (1000 * 60 * 60 * 24)) const h = Math.floor((sAll - d * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)) const m = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60)) / (1000 * 60)) const s = Math.floor((sAll - d * (1000 * 60 * 60 * 24) - h * (1000 * 60 * 60) - m * (1000 * 60)) / 1000) let result: any = {d, h, m ,s} if (format) { result = '' if (d > 0) { result += d + '天' } if (d > 0 || h > 0) { result += h + '时' } if (d > 0 || h > 0 || m > 0) { result += m + '分' } result += s + '秒' } return result } export const deepAssign = (...obj: any) => { const result = Object.assign({}, ...obj) for (let item of obj) { for (let [idx, val] of Object.entries(item)) { if (val instanceof Array) { result[idx] = val } else if (val instanceof Object) { result[idx] = deepAssign(result[idx], val) } } } return result } export const copy = (value: any) => { const str = document.createElement('input') str.setAttribute('value', value) document.body.appendChild(str) str.select() document.execCommand('copy') document.body.removeChild(str) console.log(value) } /** * * @param precision 精度 1、0.1 、0.01…… * @param colorArr * [ * [20.1, '#111111'], * [20.3, '#dddddd'], * [20.7, '#eeeeee'], * ] * @return colorMap * new Map([ * [20.1, '#111111'] * …… * [20.3, '#dddddd'] * …… * [20.7, '#eeeeee'] * ]) */ export const getGradientColorArray = (precision: any, colorArr: any) => { // 将hex表示方式转换为rgb表示方式(这里返回rgb数组模式) const colorRgb = (sColor: any) => { const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; let _sColor = sColor.toLowerCase(); if (_sColor && reg.test(_sColor)) { if (_sColor.length === 4) { let sColorNew = "#"; for (let i = 1; i < 4; i += 1) { sColorNew += _sColor.slice(i, i + 1).concat(_sColor.slice(i, i + 1)); } _sColor = sColorNew; } //处理六位的颜色值 const sColorChange = []; for (let i = 1; i < 7; i += 2) { // @ts-ignore sColorChange.push(parseInt("0x" + _sColor.slice(i, i + 2))); } return sColorChange; } else { return _sColor; } }; // 将rgb表示方式转换为hex表示方式 const colorHex = (rgb: any) => { const _this = rgb; const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/; if (/^(rgb|RGB)/.test(_this)) { const aColor = _this.replace(/(?:(|)|rgb|RGB)*/g, "").split(","); let strHex = "#"; for (let i = 0; i < aColor.length; i++) { let hex = Number(aColor[i]).toString(16); hex = Number(hex) < 10 ? 0 + '' + hex : hex;// 保证每个rgb的值为2位 if (hex === "0") { hex += hex; } strHex += hex; } if (strHex.length !== 7) { strHex = _this; } return strHex; } else if (reg.test(_this)) { const aNum = _this.replace(/#/, "").split(""); if (aNum.length === 6) { return _this; } else if (aNum.length === 3) { let numHex = "#"; for (let i = 0; i < aNum.length; i += 1) { numHex += (aNum[i] + aNum[i]); } return numHex; } } else { return _this; } } const rgb2hex = (sRGB: any) => { const reg = /^(RGB|rgb)\((\d+),\s*(\d+),\s*(\d+)\)$/ if (!reg.test(sRGB)) return sRGB const rgbArr = sRGB.match(/\d+/g) const resultRgbArr = rgbArr.map((v: any) => { if (+v > 16) return (+v).toString(16) return '0' + (+v).toString(16) }) return '#' + resultRgbArr.join('') } const gradientColor = (startColor: any, endColor: any, step: any) => { const startRGB = colorRgb(startColor);//转换为rgb数组模式 const startR = startRGB[0]; const startG = startRGB[1]; const startB = startRGB[2]; const endRGB = colorRgb(endColor); const endR = endRGB[0]; const endG = endRGB[1]; const endB = endRGB[2]; const sR = (endR - startR) / step;//总差值 const sG = (endG - startG) / step; const sB = (endB - startB) / step; const colorArr = []; for (let i = 0; i <= step; i++) { //计算每一步的hex值 const hex = colorHex('rgb(' + parseInt((sR * i + startR)) + ',' + parseInt((sG * i + startG)) + ',' + parseInt((sB * i + startB)) + ')'); // @ts-ignore colorArr.push(rgb2hex(hex)); } return colorArr; } const colorMap = new Map() colorArr.forEach((v: any, i: number) => { if (i < colorArr.length - 1) { const _arr = gradientColor(v[1], colorArr[i + 1][1], (Number(colorArr[i + 1][0]) - Number(v[0])) / precision) _arr.forEach((cV, cI) => { colorMap.set((Number(v[0]) + cI * precision).toFixed(String(precision).split('').filter(p => p === '0').length), cV) }) } else { colorMap.set(Number(v[0]).toFixed(String(precision).split('').filter(p => p === '0').length), v[1]) } }) return colorMap } // 根据部门名称 寻找上级所有父节点名称 //data:要遍历的数据, target:查找目标, result用于装查找结果的数组 export const findParent = (data: any, target: any, result: any) => { for (let item of data) { if (item.deptName === target) { //将查找到的目标数据加入结果数组中 //可根据需求unshift(item.id)或unshift(item) result.unshift(item.deptName); return true; } if (item.children && item.children.length > 0) { let isFind = findParent(item.children, target, result); if (isFind) { result.unshift(item.deptName); return true; } } } return false; }; // 涉及到跨域问题,需要配nginx代理的url地址处理 export const proxyNginxUrl = (url: string) => { if (url) { // @ts-ignore const apiMapper = window.cusConfig?.nginxApiMapper || new Map() let newUrl = url apiMapper.forEach((v: any, k: any) => { if (url.includes(k)) { newUrl = v + url.substring(url.indexOf(k) + k.length, url.length) } }) return newUrl } return url }; // 后端接口拦截 < > 内的文本,正反向格式化为 /《/ /》/ res = true 为接收返回值进行格式化 export const formatInputHtmlInterceptor = (html: string, res = true) => { if (html) { const map = new Map() map.set('<', '_《_') map.set('>', '_》_') let nHtml = html.toString() map.forEach((v, k) => { if (res) { nHtml = nHtml.replace(eval(`/${v}/g`), k) } else { nHtml = nHtml.replace(eval(`/${k}/g`), v) } }) return nHtml } return html } //生成从minNum到maxNum的随机数 // export const randomNum = (minNum: number,maxNum: number) => { // return parseInt(String(Math.random() * (maxNum - minNum + 1) + minNum),10); // } export const randomNum = (min = 0, max = 0, decimal=0) => { // 获取数值的小数部分 const getDecimalNum = (data: number) => { return Number(data.toString().split('.')[1]); } let min_z = Math.trunc(min); // 最小值的整数部分 let max_z = Math.trunc(max); // 最大值的整数部分 // 判断是否存在小数部分,不存在的话为0 let min_x = isNaN(getDecimalNum(min)) ? 0 : getDecimalNum(min); // 最小值的小数部分 let max_x = isNaN(getDecimalNum(max)) ? 0 : getDecimalNum(max); // 最大值的小数部分 // 区分有小数和没小数的情况 if (min_x > 0 || max_x > 0 || decimal > 0) { // 整数部分随机数 let z = parseInt(String(Math.random() * (max_z - min_z + 1) + min_z), 10); // 小数部分随机数 let x = 0; // 小数部分随机数最大位数 let max_decimal = min_x.toString().length > max_x.toString().length ? min_x.toString().length : max_x.toString().length; max_decimal = decimal > max_decimal ? decimal : max_decimal; // 判断随机出的整数部分,是否等于最小值或者最大值 if(z == min_z || z == max_z){ if(z == min_z){ // 整数部分随机数等于最小值,那么应该从最小值的小数部分开始,到小数位数的最大值随机就可以 x = parseInt(String(Math.random() * (Math.pow(10, max_decimal) - min_x) + min_x), 10); }else{ // 整数部分随机数等于最大值,那么应该从0开始,到最大值小数部分 x = parseInt(String(Math.random() * (max_x + 1)), 10); } }else{ // 整数部分在最大最小值区间的,就从0到小数位数的最大值随机就可以 x = parseInt(String(Math.random() * (Math.pow(10, max_decimal))), 10); } return Number(`${z}.${x}`); } else { return parseInt(String(Math.random() * (max_z - min_z + 1) + min_z), 10); } } /** * * @param val 处理的数值 * @param unit 单位,默认空字符 * @param digits 小数,默认1 */ export const formatNumberUnit = (val, unit = '', digits = 1) => { let num = '' let str = '' if (String(val).length < 5) { num = val str = unit } else if (String(val).length < 9) { num = Number((Number(val) / 10000).toFixed(digits)) / 1 + '' str = '万' + unit } else if (String(val).length < 13) { num = Number((Number(val) / 10000 / 10000).toFixed(digits)) / 1 + '' str = '亿' + unit } return { num: num, unit: str } }