import { getArrValue } from 'js-fast-way' import { getDictionary, getDictionaryBiz } from '~api/other' /** * 效验是否为数字或小数的数字 * @param text 字符串内容 * @param lose 是否允许负数,默认允许 * @returns {boolean} */ export const isNumberReg = (text, lose = true) => { let pattern = lose ? /^-?\d+(.\d+)?$/ : /^\d+(.\d+)?$/ return pattern.test(text) } //获取字典数据 export const getDictionaryData = async (code, biz = false) => { let res = [] if (biz) { const { data } = await getDictionaryBiz({ code: code }) res = getArrValue(data) } else { const { data } = await getDictionary({ code: code }) res = getArrValue(data) } //处理数据 let newArr = [] for (let i = 0; i < res.length; i++) { const val = isNumberReg(res[i]['dictKey']) ? Number(res[i]['dictKey']) : res[i]['dictKey'] newArr.push({ label: res[i]['dictValue'], value: val, }) } return newArr } //操作确认 export const actionConfirm = (cbk) => { window?.$messageBox?.alert('请确认要执行此操作吗?', '操作确认', { showCancelButton: true, confirmButtonText: '确认', cancelButtonText: '取消', type: 'warning', callback: (action) => { if (action === 'confirm') { cbk() } }, }) } //删除提醒 export const delMessage = (cbk) => { window?.$messageBox?.alert('请谨慎考虑后,确认是否需要删除?', '删除提醒', { showCancelButton: true, confirmButtonText: '确认删除', cancelButtonText: '取消', type: 'warning', callback: (action) => { if (action === 'confirm') { cbk() } }, }) } //动态加载线上js文件 export const addDocumentsJs = () => { return new Promise((resolve) => { const script = document.createElement('script') script.src = 'http://47.110.251.215:6831/web-apps/apps/api/documents/api.js' script.type = 'text/javascript' document.head.appendChild(script) script.onload = () => { resolve() } }) } //判断是否为网址 export const isPathUrl = (path) => { return /^(https?:|mailto:|tel:)/.test(path) } //获取当前域名 export const getTopUrl = () => { return window.location.href.split('/#/')[0] } //设置系统名称 export const setAppName = (name) => { const title = window.document.title window.document.title = `${title}${name ? ' - ' + name : ''}` } //保留两位小数并返回 export const keepdecimal = (str)=>{ // 判断是否有小数点 if (str.indexOf('.') !== -1) { // 将数字字符串拆分为整数部分和小数部分 let parts = str.split('.') let decimalPart = parts[1] // 判断小数部分是否超过两位 if (decimalPart.length > 2) { // 保留两位小数并返回 return parseFloat(Number(str).toFixed(2)).toString() } else { return str } } else { return str } } //是否重复 export const hasDuplicates = (array) =>{ let counts = {} // 用于存储每个元素的出现次数 for (let i = 0; i < array.length; i++) { let element = array[i] // 如果 counts 中已经有了这个元素,则将其出现次数加 1;否则,将其出现次数设为 1 counts[element] = (counts[element] || 0) + 1 // 如果某个元素的出现次数大于 1,则数组中存在重复元素,返回 true if (counts[element] > 1) { return { isWarn:true, element:array[i], } } } // 遍历完成后未发现重复元素,返回 false return false }