detection-business/utils/utils.js
2025-07-11 10:17:36 +08:00

180 lines
5.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from "./request";
import {
setStorageWithExpiry,
getStorageWithExpiry
} from './auth'
export async function getDictDataByType(type) {
let data = getStorageWithExpiry(type);
if (data === null || data === undefined || data.length == 0) {
try {
const response = await request({
url: '/system/dict-data/type',
method: 'get',
params: {
type
}
});
data = response.data;
setStorageWithExpiry(type, data, 3600); // 存储数据并设置过期时间
} catch (error) {
console.error("请求字典数据时出现了异常:", error);
throw error; // 确保错误能够被外部捕获
}
}
return data;
}
export function formatDate(timestamp) {
if (!timestamp) {
return ""
}
// 将时间戳转换为Date对象
const date = new Date(timestamp);
// 获取年月日时分秒
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
// 组合成日期时间字符串
return `${year}-${month}-${day}`;
}
export function formatDateChinese(timestamp) {
// 将时间戳转换为Date对象
const date = new Date(timestamp);
// 获取年月日时分秒
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
// 组合成日期时间字符串
return `${year}${month}${day}`;
}
export function formatDateTimeToMinute(timestamp) {
// 将时间戳转换为 Date 对象
const date = new Date(timestamp);
// 获取年月日时分
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
// 组合成日期时间字符串格式yyyy-MM-dd hh:mm
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
/**
* 构造树型结构数据
* @param {*} data 数据源
* @param {*} id id字段 默认 'id'
* @param {*} parentId 父节点字段 默认 'parentId'
* @param {*} children 孩子节点字段 默认 'children'
* @param {*} rootId 根Id 默认 0
*/
/**
* 将扁平数组转换为树形结构,并支持按 sort 字段排序
* @param {Array} data 源数据
* @param {String} id 节点ID字段名默认为 'id'
* @param {String} parentId 父节点ID字段名默认为 'parentId'
* @param {String} children 子节点字段名,默认为 'children'
* @param {Number|String} rootId 根节点ID值默认为最小的parentId或0
* @param {String} sortKey 排序字段名,默认为 'sort'
* @returns {Array} 树形结构数据
*/
export function handleTree(data, id, parentId, children, rootId, sortKey) {
// 参数默认值处理
id = id || 'id';
parentId = parentId || 'parentId';
children = children || 'children';
sortKey = sortKey || 'sort';
// 自动计算根节点ID取最小的parentId若无则用0
rootId = rootId || Math.min(...data.map(item => item[parentId])) || 0;
// 深拷贝源数据以避免污染原数组
const cloneData = JSON.parse(JSON.stringify(data));
// 先对所有数据进行排序(确保父节点在前)
cloneData.sort((a, b) => {
const aSort = a[sortKey] ?? 0; // 使用空值合并运算符处理undefined
const bSort = b[sortKey] ?? 0;
return aSort - bSort; // 升序排序
});
// 构建哈希表加速查找
const nodeMap = {};
cloneData.forEach(item => {
nodeMap[item[id]] = item;
item[children] = []; // 初始化children数组
});
// 构建树形结构
const tree = [];
cloneData.forEach(item => {
if (item[parentId] === rootId) {
// 根节点直接加入结果
tree.push(item);
} else {
// 非根节点找到父节点并插入
const parent = nodeMap[item[parentId]];
parent?.[children]?.push(item);
}
});
// 递归排序所有子节点
const sortChildren = (nodes) => {
nodes.forEach(node => {
if (node[children]?.length) {
node[children].sort((a, b) => {
const aSort = a[sortKey] ?? 0;
const bSort = b[sortKey] ?? 0;
return aSort - bSort;
});
sortChildren(node[children]);
}
});
};
sortChildren(tree);
return tree.length ? tree : data; // 空树时返回原数据
}
export function getDateRange(type) {
const now = new Date()
let start, end
if (type === 'week') {
// 获取本周的开始(周一)和结束(周日)
const day = now.getDay() || 7 // Sunday 为 0设为 7
start = new Date(now)
start.setDate(now.getDate() - day + 1)
end = new Date(start)
end.setDate(start.getDate() + 6)
} else if (type === 'month') {
// 获取本月的开始和结束
start = new Date(now.getFullYear(), now.getMonth(), 1)
end = new Date(now.getFullYear(), now.getMonth() + 1, 0) // 本月最后一天
}else if (type === 'day') {
// 获取当天的开始和结束00:00:00 到 23:59:59
start = new Date(now)
start.setHours(0, 0, 0, 0)
end = new Date(now)
end.setHours(23, 59, 59, 999)
} else {
console.warn('不支持的类型:', type)
return []
}
return [
formatDateCus(start),
formatDateCus(end)
]
}
export function formatDateCus(date) {
const y = date.getFullYear()
const m = (date.getMonth() + 1).toString().padStart(2, '0')
const d = date.getDate().toString().padStart(2, '0')
return `${y}-${m}-${d}`
}