feat:设备使用记录
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import test from './test.js'
|
||||
import { round } from './digit.js'
|
||||
import dayjs from 'dayjs'
|
||||
/**
|
||||
* @description 如果value小于min,取min;如果value大于max,取max
|
||||
* @param {number} min
|
||||
@@ -741,7 +742,76 @@ function isJsonString(str) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 构造树型结构数据
|
||||
* @param {*} data 数据源
|
||||
* @param {*} id id字段 默认 'id'
|
||||
* @param {*} parentId 父节点字段 默认 'parentId'
|
||||
* @param {*} children 孩子节点字段 默认 'children'
|
||||
*/
|
||||
const handleTree = (data, id, parentId, children) => {
|
||||
if (!Array.isArray(data)) {
|
||||
console.warn('data must be an array')
|
||||
return []
|
||||
}
|
||||
const config = {
|
||||
id: id || 'id',
|
||||
parentId: parentId || 'parentId',
|
||||
childrenList: children || 'children'
|
||||
}
|
||||
|
||||
const childrenListMap = {}
|
||||
const nodeIds = {}
|
||||
const tree = []
|
||||
|
||||
for (const d of data) {
|
||||
const parentId = d[config.parentId]
|
||||
if (childrenListMap[parentId] == null) {
|
||||
childrenListMap[parentId] = []
|
||||
}
|
||||
nodeIds[d[config.id]] = d
|
||||
childrenListMap[parentId].push(d)
|
||||
}
|
||||
|
||||
for (const d of data) {
|
||||
const parentId = d[config.parentId]
|
||||
if (nodeIds[parentId] == null) {
|
||||
tree.push(d)
|
||||
}
|
||||
}
|
||||
|
||||
for (const t of tree) {
|
||||
adaptToChildrenList(t)
|
||||
}
|
||||
|
||||
function adaptToChildrenList(o) {
|
||||
if (childrenListMap[o[config.id]] !== null) {
|
||||
o[config.childrenList] = childrenListMap[o[config.id]]
|
||||
}
|
||||
if (o[config.childrenList]) {
|
||||
for (const c of o[config.childrenList]) {
|
||||
adaptToChildrenList(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return tree
|
||||
}
|
||||
|
||||
function formateToDate(date) {
|
||||
if (date) {
|
||||
return dayjs(date).format('YYYY-MM-DD')
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
function formateToDateTime(date) {
|
||||
if (date) {
|
||||
return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
export default {
|
||||
range,
|
||||
getPx,
|
||||
@@ -777,5 +847,8 @@ export default {
|
||||
uuid,
|
||||
replacer,
|
||||
reviver,
|
||||
isJsonString
|
||||
isJsonString,
|
||||
handleTree,
|
||||
formateToDate,
|
||||
formateToDateTime
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user