detection-business/utils/utils.js

93 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-12-12 18:51:08 +08:00
import request from "./request";
import {
2025-04-23 17:34:22 +08:00
setStorageWithExpiry,
getStorageWithExpiry
2024-12-12 18:51:08 +08:00
} from './auth'
2024-12-20 19:32:47 +08:00
export async function getDictDataByType(type) {
2025-04-23 17:34:22 +08:00
let data = getStorageWithExpiry(type);
2025-05-28 16:02:29 +08:00
if (data === null || data === undefined || data.length == 0) {
2025-04-23 17:34:22 +08:00
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;
2024-12-12 18:51:08 +08:00
}
2024-12-16 16:11:30 +08:00
export function formatDate(timestamp) {
2025-04-23 17:34:22 +08:00
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}`;
2024-12-16 16:11:30 +08:00
}
2025-02-19 18:01:29 +08:00
export function formatDateChinese(timestamp) {
2025-04-23 17:34:22 +08:00
// 将时间戳转换为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}`;
2025-02-19 18:01:29 +08:00
}
2025-02-10 18:01:00 +08:00
export function formatDateTimeToMinute(timestamp) {
2025-04-23 17:34:22 +08:00
// 将时间戳转换为 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}`;
2025-05-28 16:02:29 +08:00
}
/**
* 构造树型结构数据
* @param {*} data 数据源
* @param {*} id id字段 默认 'id'
* @param {*} parentId 父节点字段 默认 'parentId'
* @param {*} children 孩子节点字段 默认 'children'
* @param {*} rootId 根Id 默认 0
*/
export function handleTree(data, id, parentId, children, rootId) {
id = id || 'id'
parentId = parentId || 'parentId'
children = children || 'children'
rootId = rootId || Math.min.apply(Math, data.map(item => {
return item[parentId]
})) || 0
//对源数据深度克隆
const cloneData = JSON.parse(JSON.stringify(data))
//循环所有项
const treeData = cloneData.filter(father => {
let branchArr = cloneData.filter(child => {
//返回每一项的子级数组
return father[id] === child[parentId]
});
branchArr.length > 0 ? father.children = branchArr : '';
//返回第一层
return father[parentId] === rootId;
});
return treeData !== '' ? treeData : data;
2025-04-23 17:34:22 +08:00
}