feat: 调整行业请求数据结构

This commit is contained in:
zdl
2025-10-23 14:24:26 +08:00
parent d2988d1a33
commit 9dcd4bfbf3
7 changed files with 848 additions and 179 deletions

View File

@@ -0,0 +1,169 @@
// src/contexts/IndustryContext.js
// 行业分类数据全局上下文
import React, { createContext, useContext, useState, useCallback } from 'react';
import { industryService } from '../services/industryService';
import { logger } from '../utils/logger';
const IndustryContext = createContext();
// localStorage 缓存配置
const CACHE_KEY = 'industry_data';
const CACHE_TIME_KEY = 'industry_cache_time';
const CACHE_DURATION = 24 * 60 * 60 * 1000; // 1天毫秒
/**
* 从 localStorage 读取缓存
* @returns {Array|null} 缓存的行业数据,如果无效则返回 null
*/
const loadFromCache = () => {
try {
const cachedData = localStorage.getItem(CACHE_KEY);
const cacheTime = localStorage.getItem(CACHE_TIME_KEY);
if (!cachedData || !cacheTime) {
logger.debug('IndustryContext', '无缓存数据');
return null;
}
const now = Date.now();
const cacheAge = now - parseInt(cacheTime, 10);
if (cacheAge > CACHE_DURATION) {
logger.debug('IndustryContext', '缓存已过期', {
cacheAge: Math.floor(cacheAge / 1000 / 60), // 分钟
maxAge: CACHE_DURATION / 1000 / 60 // 分钟
});
// 清除过期缓存
localStorage.removeItem(CACHE_KEY);
localStorage.removeItem(CACHE_TIME_KEY);
return null;
}
logger.debug('IndustryContext', '读取缓存成功', {
cacheAge: Math.floor(cacheAge / 1000 / 60) // 分钟
});
return JSON.parse(cachedData);
} catch (error) {
logger.error('IndustryContext', '读取缓存失败', error);
// 清除损坏的缓存
localStorage.removeItem(CACHE_KEY);
localStorage.removeItem(CACHE_TIME_KEY);
return null;
}
};
/**
* 保存到 localStorage
* @param {Array} data - 行业数据
*/
const saveToCache = (data) => {
try {
localStorage.setItem(CACHE_KEY, JSON.stringify(data));
localStorage.setItem(CACHE_TIME_KEY, Date.now().toString());
logger.debug('IndustryContext', '缓存保存成功', {
count: data?.length || 0
});
} catch (error) {
logger.error('IndustryContext', '缓存保存失败', error);
}
};
/**
* 清除缓存
*/
export const clearIndustryCache = () => {
localStorage.removeItem(CACHE_KEY);
localStorage.removeItem(CACHE_TIME_KEY);
logger.info('IndustryContext', '缓存已清除');
};
/**
* IndustryProvider 组件
* 提供全局行业数据管理
*/
export const IndustryProvider = ({ children }) => {
const [industryData, setIndustryData] = useState(null);
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
/**
* 加载行业数据
* 优先从缓存读取,缓存无效时调用 API
*/
const loadIndustryData = useCallback(async () => {
// 如果已有数据,不重复加载
if (industryData && industryData.length > 0) {
logger.debug('IndustryContext', '数据已加载,跳过请求');
return industryData;
}
// 尝试从缓存读取
const cachedData = loadFromCache();
if (cachedData) {
setIndustryData(cachedData);
return cachedData;
}
// 缓存无效,调用 API
setLoading(true);
setError(null);
try {
logger.debug('IndustryContext', '开始请求行业数据');
const response = await industryService.getClassifications();
const data = response.data;
setIndustryData(data);
saveToCache(data); // 保存到缓存
logger.debug('IndustryContext', '行业数据加载成功', {
count: data?.length || 0
});
return data;
} catch (err) {
logger.error('IndustryContext', '行业数据加载失败', err);
setError(err);
return null;
} finally {
setLoading(false);
}
}, [industryData]);
/**
* 强制刷新数据(清除缓存并重新请求)
*/
const refreshIndustryData = useCallback(async () => {
clearIndustryCache();
setIndustryData(null);
return await loadIndustryData();
}, [loadIndustryData]);
const value = {
industryData, // 行业数据
loading, // 加载状态
error, // 错误信息
loadIndustryData, // 加载数据方法
refreshIndustryData // 刷新数据方法
};
return (
<IndustryContext.Provider value={value}>
{children}
</IndustryContext.Provider>
);
};
/**
* useIndustry Hook
* 在任何组件中使用行业数据
*/
export const useIndustry = () => {
const context = useContext(IndustryContext);
if (!context) {
throw new Error('useIndustry must be used within IndustryProvider');
}
return context;
};