- 为删除按钮添加 v-if 指令,仅在没有子节点时显示 - 为删除按钮添加 del 类,以应用特定样式 - 在 utils.js 中添加 $isEmpty函数,用于检查对象是否为空
		
			
				
	
	
		
			241 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			241 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| import {MessageBox} from 'element-ui'
 | |
| import $moment from './moment'
 | |
| import $dict from './dict'
 | |
| import $encryption from './encryption'
 | |
| import $coin from './coin'
 | |
| import Area from "./area"
 | |
| import ID from "./identity"
 | |
| import $reg from "./regular"
 | |
| 
 | |
| /**
 | |
|  * 生成子节点的递归方法
 | |
|  * @param parent 父元素
 | |
|  * @param pending 待递归的数组
 | |
|  * @param config 配置
 | |
|  */
 | |
| const addChild = (parent, pending, config) => {
 | |
|   let conf = {
 | |
|       key: 'id',
 | |
|       parent: 'parentId',
 | |
|       children: 'children',
 | |
|       ...config
 | |
|     },
 | |
|     doBeforeCount = pending.length
 | |
|   for (let i = pending.length - 1; i >= 0; i--) {
 | |
|     let e = pending[i]
 | |
|     if (e[conf.parent] == parent[conf.key]) {
 | |
|       parent[conf.children] = [...(parent[conf.children] || []), e]
 | |
|       pending.splice(i, 1)
 | |
|     }
 | |
|   }
 | |
|   parent[conf.children] &&
 | |
|   (parent[conf.children] = parent[conf.children].reverse())
 | |
|   if (pending.length > 0 && doBeforeCount > pending.length) {
 | |
|     parent[conf.children].map(c => addChild(c, pending, conf))
 | |
|   }
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * 封装提示框
 | |
|  */
 | |
| const $confirm = (content, options) => {
 | |
|   return MessageBox.confirm(content, {
 | |
|     type: 'warning',
 | |
|     confirmButtonText: '确认',
 | |
|     center: true,
 | |
|     title: '提示',
 | |
|     dangerouslyUseHTMLString: true,
 | |
|     customClass: "AiConfirm",
 | |
|     ...options
 | |
|   }).catch(() => 0)
 | |
| }
 | |
| 
 | |
| 
 | |
| const $decimalCalc = (...arr) => {
 | |
|   // 确认提升精度
 | |
|   let decimalLengthes = arr.map(e => {
 | |
|     let index = ('' + e).indexOf('.')
 | |
|     return ('' + e).length - index
 | |
|   })
 | |
|   let maxDecimal = Math.max(...decimalLengthes),
 | |
|     precision = Math.pow(10, maxDecimal)
 | |
|   // 计算
 | |
|   let intArr = arr.map(e => (Number(e) || 0) * precision)
 | |
|   // 返回计算值
 | |
|   return intArr.reduce((t, a) => t + a) / precision
 | |
| }
 | |
| 
 | |
| /**
 | |
|  * 封装权限判断方法
 | |
|  */
 | |
| const $permissions = flag => {
 | |
|   let buttons = []
 | |
|   if (localStorage.getItem('vuex')) {
 | |
|     const vuex = JSON.parse(localStorage.getItem('vuex'))
 | |
|     buttons = vuex.user.info.buttons
 | |
|   }
 | |
|   if (buttons && buttons.length > 0) {
 | |
|     return buttons.some(b => b.id == flag || b.permission == flag)
 | |
|   } else return false
 | |
| }
 | |
| 
 | |
| const $colorUtils = {
 | |
|   Hex2RGBA(color, alpha = 1) {
 | |
|     let hex = 0
 | |
|     if (color.charAt(0) == '#') {
 | |
|       if (color.length == 4) {
 | |
|         // 检测诸如#FFF简写格式
 | |
|         color =
 | |
|           '#' +
 | |
|           color.charAt(1).repeat(2) +
 | |
|           color.charAt(2).repeat(2) +
 | |
|           color.charAt(3).repeat(2)
 | |
|       }
 | |
|       hex = parseInt(color.slice(1), 16)
 | |
|     }
 | |
|     let r = (hex >> 16) & 0xff
 | |
|     let g = (hex >> 8) & 0xff
 | |
|     let b = hex & 0xff
 | |
|     return `rgba(${r},${g},${b},${alpha})`
 | |
|   },
 | |
|   RGBtoHex(r, g, b) {
 | |
|     let hex = (r << 16) | (g << 8) | b
 | |
|     return '#' + hex.toString(16)
 | |
|   }
 | |
| }
 | |
| export const $copy = any => {
 | |
|   if (any) {
 | |
|     return JSON.parse(JSON.stringify(any))
 | |
|   } else return any
 | |
| }
 | |
| 
 | |
| let debounceWait = null
 | |
| export const $debounce = (fn, wait) => {
 | |
|   if (debounceWait !== null) clearTimeout(debounceWait);
 | |
|   debounceWait = setTimeout(function () {
 | |
|     typeof fn === 'function' && fn();
 | |
|   }, wait);
 | |
| }
 | |
| export const $checkJson = str => {
 | |
|   if (typeof str == 'string') {
 | |
|     try {
 | |
|       let obj = JSON.parse(str);
 | |
|       return !!(typeof obj == 'object' && obj);
 | |
|     } catch (e) {
 | |
|       return false;
 | |
|     }
 | |
|   }
 | |
|   return false;
 | |
| }
 | |
| const $load = (sdk, interval = 200, name = "", c = 0) => {
 | |
|   if (!!sdk) {
 | |
|     return Promise.resolve()
 | |
|   } else if (c < 10) {
 | |
|     return new Promise(resolve => setTimeout(() => resolve($load(sdk, interval, name, ++c)), interval))
 | |
|   } else return Promise.reject("无法加载" + name)
 | |
| }
 | |
| 
 | |
| export {Area, ID}
 | |
| 
 | |
| export default {
 | |
|   addChild,
 | |
|   $confirm,
 | |
|   $decimalCalc,
 | |
|   $dict,
 | |
|   $permissions,
 | |
|   $colorUtils,
 | |
|   $moment,
 | |
|   $encryption,
 | |
|   $coin,
 | |
|   $injectLib: (url, cb = () => 0) => {
 | |
|     const scriptList = document.body.querySelectorAll('script')
 | |
|     if (Object.values(scriptList || {}).findIndex(e => e.src == url) == -1) {
 | |
|       const script = document.createElement('script')
 | |
|       script.type = 'text/javascript'
 | |
|       script.src = url
 | |
|       script.addEventListener('load', () => cb())
 | |
|       document.body.appendChild(script)
 | |
|     } else cb()
 | |
|   },
 | |
|   $injectCss: (url, cb = () => 0) => {
 | |
|     const linkList = document.body.querySelectorAll('link')
 | |
|     if (Object.values(linkList || {}).findIndex(e => e.href == url) == -1) {
 | |
|       const link = document.createElement('link')
 | |
|       link.rel = "stylesheet"
 | |
|       link.type = "text/css"
 | |
|       link.href = url
 | |
|       link.addEventListener('load', () => cb())
 | |
|       document.head.appendChild(link)
 | |
|     } else cb()
 | |
|   },
 | |
|   $dateFormat: (time, format) => {
 | |
|     return $moment(time)
 | |
|       .format(format || 'YYYY-MM-DD')
 | |
|       .replace('Invalid Date', '')
 | |
|   },
 | |
|   $copy,
 | |
|   $download: (url, name) => {
 | |
|     fetch(url).then(res => res.blob()).then(blob => {
 | |
|       const link = document.createElement('a')
 | |
|       link.style.display = 'none'
 | |
|       link.href = URL.createObjectURL(blob)
 | |
|       !!name && link.setAttribute("download", name)
 | |
|       document.body.appendChild(link)
 | |
|       link.click()
 | |
|       document.body.removeChild(link)
 | |
|     })
 | |
|   },
 | |
|   $debounce,
 | |
|   $checkJson,
 | |
|   $arr2tree: (list, config = {}) => {
 | |
|     let result = []
 | |
|     const {key = 'id', parent = 'parentId', children = 'children'} = config, itemMap = {},
 | |
|       ids = list?.map(e => `#${e[key]}#`)?.toString()
 | |
|     for (const e of list) {
 | |
|       const id = e[key], pid = e[parent]
 | |
|       itemMap[id] = {...e, [children]: [itemMap[id]?.[children]].flat().filter(Boolean)}
 | |
|       const treeItem = itemMap[id]
 | |
|       if (!!pid && ids.indexOf(`#${pid}#`) > -1) {
 | |
|         if (!itemMap[pid]) {
 | |
|           itemMap[pid] = {
 | |
|             children: []
 | |
|           }
 | |
|         }
 | |
|         itemMap[pid].children.push(treeItem)
 | |
|       } else result.push(treeItem)
 | |
|     }
 | |
|     const removeNullChildren = node => {
 | |
|       if (node[children] && node[children].length > 0) {
 | |
|         node[children].map(c => removeNullChildren(c))
 | |
|       } else delete node[children]
 | |
|     }
 | |
|     if (!!config.root) {
 | |
|       result = itemMap[config.root]
 | |
|     }
 | |
|     result.forEach(removeNullChildren)
 | |
|     return result
 | |
|   },
 | |
|   $load,
 | |
|   $reg,
 | |
|   Area,
 | |
|   ID,
 | |
|   $arrDepth: (arr) => {
 | |
|     if (Array.isArray(arr)) {
 | |
|       let max = 1
 | |
|       for (let i = 0; i < arr.length; i++) {
 | |
|         const depth = this.$arrDepth(arr[i])
 | |
|         if (depth > max) {
 | |
|           max = depth
 | |
|         }
 | |
|       }
 | |
|       return max
 | |
|     } else return 0
 | |
|   },
 | |
|   $isEmpty(obj) {
 | |
|     if (obj === null || obj === undefined) return true;
 | |
|     if (typeof obj === 'string' && obj.trim() === '') return true;
 | |
|     if (Array.isArray(obj) && obj.length === 0) return true;
 | |
|     return typeof obj === 'object' && Object.keys(obj).length === 0;
 | |
|   }
 | |
| }
 |