2024-06-03 18:35:53 +08:00
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
|
|
2024-06-04 18:43:13 +08:00
|
|
|
|
/**
|
|
|
|
|
* 构造树型结构数据
|
|
|
|
|
* @param {*} data 数据源
|
|
|
|
|
* @param {*} id id字段 默认 'id'
|
|
|
|
|
* @param {*} parentId 父节点字段 默认 'parentId'
|
|
|
|
|
* @param {*} children 孩子节点字段 默认 'children'
|
|
|
|
|
*/
|
|
|
|
|
export 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
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-12 18:33:39 +08:00
|
|
|
|
export const convertToInteger = (num) => {
|
|
|
|
|
if (typeof num === 'undefined') return 0
|
|
|
|
|
const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
|
|
|
|
|
// TODO 分转元后还有小数则四舍五入
|
|
|
|
|
return Math.round(parsedNumber * 100)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将一个整数转换为分数保留两位小数
|
|
|
|
|
* @param num
|
|
|
|
|
*/
|
|
|
|
|
export const formatToFraction = (num) => {
|
|
|
|
|
if (typeof num === 'undefined') return '0.00'
|
|
|
|
|
const parsedNumber = typeof num === 'string' ? parseFloat(num) : num
|
|
|
|
|
return (parsedNumber / 100.0).toFixed(2)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const floatToFixed2 = (num) => {
|
|
|
|
|
let str = '0.00'
|
|
|
|
|
if (typeof num === 'undefined') {
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
const f = formatToFraction(num)
|
|
|
|
|
const decimalPart = f.toString().split('.')[1]
|
|
|
|
|
const len = decimalPart ? decimalPart.length : 0
|
|
|
|
|
switch (len) {
|
|
|
|
|
case 0:
|
|
|
|
|
str = f.toString() + '.00'
|
|
|
|
|
break
|
|
|
|
|
case 1:
|
|
|
|
|
str = f.toString() + '0'
|
|
|
|
|
break
|
|
|
|
|
case 2:
|
|
|
|
|
str = f.toString()
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
return str
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 18:22:58 +08:00
|
|
|
|
export function resetPagination(pagination) {
|
|
|
|
|
pagination.list = []
|
|
|
|
|
pagination.total = 0
|
|
|
|
|
pagination.pageNo = 1
|
|
|
|
|
}
|
2024-06-03 18:35:53 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 时间日期转换
|
|
|
|
|
* @param {dayjs.ConfigType} date 当前时间,new Date() 格式
|
|
|
|
|
* @param {string} format 需要转换的时间格式字符串
|
|
|
|
|
* @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`
|
|
|
|
|
* @description format 季度:"YYYY-mm-dd HH:MM:SS QQQQ"
|
|
|
|
|
* @description format 星期:"YYYY-mm-dd HH:MM:SS WWW"
|
|
|
|
|
* @description format 几周:"YYYY-mm-dd HH:MM:SS ZZZ"
|
|
|
|
|
* @description format 季度 + 星期 + 几周:"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ"
|
|
|
|
|
* @returns {string} 返回拼接后的时间字符串
|
|
|
|
|
*/
|
|
|
|
|
export function formatDate(date, format) {
|
|
|
|
|
// 日期不存在,则返回空
|
|
|
|
|
if (!date) {
|
|
|
|
|
return ''
|
|
|
|
|
}
|
|
|
|
|
// 日期存在,则进行格式化
|
|
|
|
|
if (format === undefined) {
|
|
|
|
|
format = 'YYYY-MM-DD HH:mm:ss'
|
|
|
|
|
}
|
|
|
|
|
return dayjs(date).format(format)
|
|
|
|
|
}
|