工具 utils
/** * 接口返回200的数据 */ export const verifyResIsSuccess = function(res) { let resCode = parseInt(res.code) return resCode == 200 } /** * 深拷贝 */ export const clone = obj => { let copy // Handle the 3 simple types, and null or undefined if (obj == null || typeof obj !== 'object') return obj // Handle Date if (obj instanceof Date) { copy = new Date() copy.setTime(obj.getTime()) return copy } // Handle Array if (obj instanceof Array) { copy = [] for (let i = 0, len = obj.length; i < len; i++) { copy[i] = clone(obj[i]) } return copy } // Handle Object if (obj instanceof Object) { copy = {} for (const attr in obj) { if (Object.prototype.hasOwnProperty.call(obj, attr)) copy[attr] = clone(obj[attr]) } return copy } throw new Error("Unable to copy obj! Its type isn't supported.") } /** * 总体路由处理器 */ export const resolveUrlPath = (url, name) => { let reqUrl = url if (url.indexOf('#') !== -1 && url.indexOf('http') === -1) { const port = reqUrl.substr(reqUrl.indexOf(':')) reqUrl = `/myiframe?src=${platformBase}${port}${reqUrl .replace('#', '') .replace(port, '')}}&name=${name}` } else if (url.indexOf('http') !== -1) { reqUrl = `/myiframe?src=${reqUrl}&name=${name}` } else { reqUrl = `${reqUrl}` } return reqUrl } /** * 返回高亮Html字符串 * @param {String} string 文本 * @param {String} searchText 搜索文字 * @param {String} background 高亮背景 * @param {String} color 高亮文字颜色 * @return {String} * */ export const getHighlightHtmlStrByString = function( string = '', searchText = '', background = 'rgba(244,0,0,0.5)', color = 'white' ) { if (searchText) { const HighlightHtmlStr = `<span style="background: ${background};color:${color}">${searchText}</span>` return string.replace(new RegExp(searchText, 'g'), HighlightHtmlStr) } else { return string } }; /** * 数字1,2 转成 一 ,二 * @param {n} n 输入的数字 * */ var cnum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; export const upper = (n) => { var s = ''; n = '' + n; // 数字转为字内符串 for (let i = 0; i < n.length; i++) { s += cnum[parseInt(n.charAt(i))]; } return s; }; /** * 格式化树 */ export const buildTree = (list, idKey, pidKey, childrenKey) => { const temp = {}; const tree = []; list.forEach(item => { temp[item[idKey]] = item; }); Object.keys(temp).forEach(key => { // 节点数据 const item = temp[key]; // 父级节点数据 const parentId = item[pidKey]; if (parentId && temp[parentId]) { const parentNode = temp[parentId]; if (!parentNode[childrenKey]) { parentNode[childrenKey] = []; } parentNode[childrenKey].push(item); } else { tree.push(item); } }); return tree; }; /** * 检测JS变量类型 * @param {any} value 传入类型值 * @return String 返回js所属类型值 Object、Array、Undefined、Null、Number、String、Boolean、Date */ export const toRawType = (value) => { return Object.prototype.toString.call(value).slice(8, -1); }; /** * 顺延推导工作日、获取工作日 * @param {Number} nextWorkDays 工作日时长 * @param {Date} currentDate 传入的开始时间,无默认当前时间为开始时间 * @return {Date} 返回第 nextWorkDays 个工作日的时间 */ export const getWorkDate = (nextWorkDays, currentDate = new Date()) => { let T = 24 * 60 * 60 * 1000; // 一天有多少毫秒 let current = currentDate.getTime(); // 外部传入当前时间用外部,没用按当前时间来 let start = current + T; // 当前时间的第二天 开始时间 let end = start + nextWorkDays * T; // 当前时间的第二天 + 工作日时长 结束时间 let calDate = new Date(); // 设置计算时间变量 const en = function(start, end) { // 计算开始时间到结束时间 有几个节假日时间 let holidays = 0; for (let d = start; d < end; d += T) { calDate.setTime(d); let day = calDate.getDay(); if (day == 0 || day == 6) { // 此处为满足节假日逻辑 holidays++; } } return holidays ? holidays + en(end, end + holidays * T) : 0; }; calDate.setTime(current + (nextWorkDays + en(start, end)) * T); return calDate; };// 累计金额格式转换 export function transforMoney(vall) { // 格式转换 let val = vall; const strInput1 = val.toString().split(".")[0]; // 整数部分 const strInput2 = val.toString().split(".")[1]; // 小数 const numbers = strInput1.toString().split("").reverse(); const valuearr = []; while (numbers.length) valuearr.push(numbers.splice(0, 3).join("")); let value = valuearr.join(",").split("").reverse().join("") + "." + strInput2; return value; } // echarts 自动轮播 export function echartsLoop(echartsName, time) { var currentIndex = -1; setInterval(function() { var dataLen = echartsName._model.option.series[0].data.length; // 取消之前高亮的图形 echartsName.dispatchAction({ type: "downplay", seriesIndex: 0, // 表示series中的第几个data数据循环展示 dataIndex: currentIndex, }); currentIndex = (currentIndex + 1) % dataLen; // +1表示每次跳转一个 // 高亮当前图形 echartsName.dispatchAction({ type: "highlight", seriesIndex: 0, dataIndex: currentIndex, }); // 显示 tooltip echartsName.dispatchAction({ type: "showTip", seriesIndex: 0, dataIndex: currentIndex, }); }, time); } // 动态加载css export function dynamicCss(path) { if (!path || path.length === 0) { throw new Error('argument "path" is required !'); } var head = document.getElementsByTagName('head')[0]; var link = document.createElement('link'); link.href = path; link.rel = 'stylesheet'; link.type = 'text/css'; head.appendChild(link); } // 动态加载js export function dynamicJs(path) { if (!path || path.length === 0) { throw new Error('argument "path" is required !'); } var head = document.getElementsByTagName('head')[0]; var script = document.createElement('script'); script.src = path; script.type = 'text/javascript'; head.appendChild(script); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!