ui库合并版本完成
This commit is contained in:
215
ui/lib/js/utils.js
Normal file
215
ui/lib/js/utils.js
Normal file
@@ -0,0 +1,215 @@
|
||||
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))
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 数组转tree
|
||||
* @param list 待转化的数组
|
||||
* @param config 配置
|
||||
*/
|
||||
const $arr2tree = (list, config = {}) => {
|
||||
const {key = 'id', parent = 'parentId', children = 'children'} = config, result = [], 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)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装提示框
|
||||
*/
|
||||
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 => {
|
||||
fetch(url).then(res => res.blob()).then(blob => {
|
||||
const link = document.createElement('a')
|
||||
link.style.display = 'none'
|
||||
link.href = URL.createObjectURL(blob)
|
||||
document.body.appendChild(link)
|
||||
link.click()
|
||||
document.body.removeChild(link)
|
||||
})
|
||||
},
|
||||
$debounce,
|
||||
$checkJson,
|
||||
$arr2tree,
|
||||
$load,
|
||||
$reg,
|
||||
Area,
|
||||
ID
|
||||
}
|
||||
Reference in New Issue
Block a user