From bcf81f4d47eff3f7f8acd4b20901ddb1a3f37201 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Fri, 24 Oct 2025 12:32:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E9=9D=99=E6=80=81?= =?UTF-8?q?=E8=A1=8C=E4=B8=9A=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/settings.local.json | 3 +- src/contexts/IndustryContext.js | 186 ++++++-------------------------- 2 files changed, 36 insertions(+), 153 deletions(-) diff --git a/.claude/settings.local.json b/.claude/settings.local.json index ee4365a3..3ff26080 100644 --- a/.claude/settings.local.json +++ b/.claude/settings.local.json @@ -4,7 +4,8 @@ "Read(//Users/qiye/**)", "Bash(npm run lint:check)", "Bash(npm run build)", - "Bash(chmod +x /Users/qiye/Desktop/jzqy/vf_react/scripts/*.sh)" + "Bash(chmod +x /Users/qiye/Desktop/jzqy/vf_react/scripts/*.sh)", + "Bash(node scripts/parseIndustryCSV.js)" ], "deny": [], "ask": [] diff --git a/src/contexts/IndustryContext.js b/src/contexts/IndustryContext.js index 72196dbe..9d80b2f0 100644 --- a/src/contexts/IndustryContext.js +++ b/src/contexts/IndustryContext.js @@ -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 ( - - {children} - - ); -}; - /** * 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 ( + + {children} + + ); +};