import request from "./request";
import {
    setStorageWithExpiry,
    getStorageWithExpiry
} from './auth'

export async function getDictDataByType(type) {
    let data = getStorageWithExpiry(type);
    if (data === null || data === undefined) {
        try {
            const response = await request({
                url: '/app-api/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) {
    // 将时间戳转换为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}`;
}

