feat: 使用静态行业数据

This commit is contained in:
zdl
2025-10-24 12:32:14 +08:00
parent f0d30244d2
commit bcf81f4d47
2 changed files with 36 additions and 153 deletions

View File

@@ -1,161 +1,12 @@
// src/contexts/IndustryContext.js
// 行业分类数据全局上下文
// 行业分类数据全局上下文 - 直接使用静态数据
import React, { createContext, useContext, useState, useCallback } from 'react';
import { industryService } from '../services/industryService';
import React, { createContext, useContext } from 'react';
import { industryData as staticIndustryData } from '../data/industryData';
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
* 在任何组件中使用行业数据
@@ -167,3 +18,34 @@ export const useIndustry = () => {
}
return context;
};
/**
* IndustryProvider 组件
* 提供全局行业数据管理 - 直接使用静态数据,无需加载
*/
export const IndustryProvider = ({ children }) => {
// 直接使用静态数据,无需状态管理
const industryData = staticIndustryData;
logger.debug('IndustryContext', '使用静态行业数据', {
count: industryData?.length || 0
});
const value = {
industryData, // 行业数据(静态)
loading: false, // 静态数据无需加载
error: null, // 静态数据无错误
loadIndustryData: () => { // 兼容旧接口,返回数据
return Promise.resolve(industryData);
},
refreshIndustryData: () => { // 兼容旧接口,返回数据
return Promise.resolve(industryData);
}
};
return (
<IndustryContext.Provider value={value}>
{children}
</IndustryContext.Provider>
);
};