diff --git a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarArea/index.tsx b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarArea/index.tsx index aa28003f1a6e730ab00d21733a81af77aa48c76c..dd3e018be03f4bd4b4736bf04430b4065853511d 100644 --- a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarArea/index.tsx +++ b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarArea/index.tsx @@ -5,7 +5,8 @@ import AreaBarList, { AreaBarItem } from '@/components/V2504/AreaBarList' import ModuleTitleWithTabs from '@/pages/Home_v_2504/components/common/ModuleTitleWithTabs' import useLatestValue from '@/pages/Home_v_2504/hooks/useLatestValue'; import { useDataType, useMapState } from '@/pages/Home_v_2504/components/MapStateContext'; -import useApiControl, { formatDateTime } from '@/pages/Home_v_2504/hooks/useApiControl'; +import { formatDateTime } from '@/pages/Home_v_2504/hooks/useApiControl'; +import usePageAwareApi from '@/pages/Home_v_2504/hooks/usePageAwareApi'; import { getDistrictHighFrequencyArea } from '@/services/CityProblem'; // 类型定义 @@ -13,6 +14,7 @@ export interface CarAreaProps { dateType?: string; dateRange?: { startTime: string; endTime: string }; dataTypeKey?: string; + pageId?: string; } // 区域类型定义 @@ -20,7 +22,8 @@ export type AreaType = 'all' | 'urban' | 'central' | 'county'; const CarArea: React.FC = ({ dateType, dateRange, - dataTypeKey = '环卫事件' + dataTypeKey = '环卫事件', + pageId = 'environment', }) => { // 地图状态 @@ -101,8 +104,17 @@ const CarArea: React.FC = ({ return null; } + // 获取当前页面的时间设置 + const pageTimeSettings = currentMapState.pageTimeSettings?.[pageId]; + if (!pageTimeSettings) { + console.log(`缺少${pageId}页面的时间设置,使用全局设置`); + } + + // 使用页面级别的时间设置,如果存在的话 + const leftPanelTime = pageTimeSettings?.leftPanelTime || currentMapState.leftPanelTime; + // 使用当前时间类型,如果不存在则使用props或默认值 - const effectiveType = currentMapState.leftPanelTime.type || dateType || 'month'; + const effectiveType = leftPanelTime.type || dateType || 'month'; // 基础参数对象 const params: any = { @@ -111,11 +123,11 @@ const CarArea: React.FC = ({ // 处理日期参数 if (effectiveType === 'range') { - params.customizeStart = formatDateTime(currentMapState.rightPanelTime.startTime); - params.customizeEnd = formatDateTime(currentMapState.rightPanelTime.endTime, true); + params.customizeStart = formatDateTime(leftPanelTime.startTime); + params.customizeEnd = formatDateTime(leftPanelTime.endTime, true); } else { // 年、月、日视图使用time参数 - params.time = formatDateTime(currentMapState.rightPanelTime.startTime); + params.time = formatDateTime(leftPanelTime.startTime); } // 获取区域编码列表 @@ -142,21 +154,21 @@ const CarArea: React.FC = ({ params.subTypeCode = useDataType(dataTypeKey); return params; - }, [mapStateRef, currentTabRef, dateType, translateDateType, dataTypeKey]); + }, [mapStateRef, currentTabRef, dateType, translateDateType, dataTypeKey, pageId]); - // 使用useApiControl管理环卫人员区域API调用 - const highFrequencyApi = useApiControl({ - apiFunction: async (params) => { - console.log('发起环卫人员区域请求,参数:', params); + // 使用usePageAwareApi管理环卫车辆区域API调用 + const highFrequencyApi = usePageAwareApi({ + apiFunction: async (params: any) => { + console.log('发起环卫车辆区域请求,参数:', params); const response = await getDistrictHighFrequencyArea(params); if (response.code === 200 && response.data) { return response.data; } - throw new Error('获取环卫人员区域数据失败'); + throw new Error('获取环卫车辆区域数据失败'); }, - onSuccess: (data) => { + onSuccess: (data: any) => { if (!data || !Array.isArray(data) || data.length === 0) { setData([]); return; @@ -177,7 +189,7 @@ const CarArea: React.FC = ({ // { name: '郫都区', value: 118, percent: '10.2' }, // { name: '都江堰市', value: 117, percent: '10.1' } // ]; - // console.log('使用模拟环卫人员区域数据'); + // console.log('使用模拟环卫车辆区域数据'); // } // 转换接口返回的数据格式为图表需要的格式 @@ -187,60 +199,33 @@ const CarArea: React.FC = ({ count: parseInt(item.value) || 0 })); - console.log('环卫人员区域数据成功:'); + console.log('环卫车辆区域数据成功:'); setData(formattedData); }, - onError: (error) => { - console.error('获取环卫人员区域数据出错:', error); + onError: (error: any) => { + console.error('获取环卫车辆区域数据出错:', error); setData([]); }, - apiName: '环卫人员区域', + apiName: '环卫车辆区域', debounceTime: 300, - cacheTime: 5000 // 缓存5秒,避免频繁刷新时重复请求 + cacheTime: 5000, // 缓存5秒,避免频繁刷新时重复请求 + pageId: pageId, // 传入页面ID用于页面感知 }); - // 使用 useMemo 跟踪 rightPanelTime 的实际变化 - const memoizedRightPanelTime = useMemo(() => { - return { - type: mapState.rightPanelTime.type, - startTime: mapState.rightPanelTime.startTime, - endTime: mapState.rightPanelTime.endTime - }; - }, [ - mapState.rightPanelTime.type, - mapState.rightPanelTime.startTime, - mapState.rightPanelTime.endTime - ]); - - // 跟踪上一次的API参数,避免重复调用 - const prevApiParamsRef = useRef(''); - // 在地图状态变化和Tab变化时加载数据 useEffect(() => { const params = buildApiParams(); // 如果没有必要的参数,不发送请求 if (!params) { - console.log('缺少必要的日期参数,不加载环卫人员区域数据'); - return; - } - - // 参数签名,用于比较参数是否有实际变化 - const paramsSignature = JSON.stringify(params); - - // 检查参数是否真正变化,防止重复调用 - if (paramsSignature === prevApiParamsRef.current) { - console.log('环卫人员区域参数未变化,跳过重复加载'); + console.log('缺少必要的日期参数,不加载环卫车辆区域数据'); return; } - console.log('加载环卫人员区域数据,参数:', params); - // highFrequencyApi.callApi(params); - - // 更新上一次参数签名 - prevApiParamsRef.current = paramsSignature; + // 调用API - 现在usePageAwareApi内部会处理参数比较和页面状态 + highFrequencyApi.callApi(params); - }, [mapState.regionCode, memoizedRightPanelTime, currentTab, buildApiParams, highFrequencyApi]); + }, [mapState.regionCode, mapState.leftPanelTime, mapState.pageTimeSettings, currentTab, buildApiParams, highFrequencyApi]); return (
diff --git a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarType/index.tsx b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarType/index.tsx index c1542a30069ae49a955b1f16a13ec9c6717ead4a..e4e7d91937939eee1db1fef298e24a9ee22025d9 100644 --- a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarType/index.tsx +++ b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/CarType/index.tsx @@ -4,7 +4,8 @@ import ChartWithLegend, { ChartDataItem } from '@/components/V2504/ChartWithLege import styles from './index.less'; import { useDataType, useMapState } from '@/pages/Home_v_2504/components/MapStateContext'; import { getSourceSystemRank } from '@/services/CityProblem'; -import useApiControl, { formatDateTime } from '@/pages/Home_v_2504/hooks/useApiControl'; +import { formatDateTime } from '@/pages/Home_v_2504/hooks/useApiControl'; +import usePageAwareApi from '@/pages/Home_v_2504/hooks/usePageAwareApi'; import useLatestValue from '@/pages/Home_v_2504/hooks/useLatestValue'; import classNames from 'classnames'; @@ -13,6 +14,7 @@ interface CarTypeProps { dateRange?: { startTime: string; endTime: string }; dataTypeKey?: string; isThinLayout?: boolean; + pageId?: string; } const CarType: React.FC = ({ @@ -20,6 +22,7 @@ const CarType: React.FC = ({ dateRange, dataTypeKey = '环卫事件', isThinLayout = true, + pageId = 'environment', }) => { // 地图状态 const { mapState } = useMapState(); @@ -48,12 +51,21 @@ const CarType: React.FC = ({ const currentMapState = mapStateRef.current; // 如果没有必要的时间参数,返回null表示不应该发起请求 - if (!currentMapState.rightPanelTime?.startTime || !currentMapState.rightPanelTime?.endTime) { + if (!currentMapState.leftPanelTime?.startTime || !currentMapState.leftPanelTime?.endTime) { return null; } + // 获取当前页面的时间设置 + const pageTimeSettings = currentMapState.pageTimeSettings?.[pageId]; + if (!pageTimeSettings) { + console.log(`缺少${pageId}页面的时间设置,使用全局设置`); + } + + // 使用页面级别的时间设置,如果存在的话 + const leftPanelTime = pageTimeSettings?.leftPanelTime || currentMapState.leftPanelTime; + // 使用当前时间类型,如果不存在则使用props或默认值 - const effectiveType = currentMapState.rightPanelTime.type || dateType || 'month'; + const effectiveType = leftPanelTime.type || dateType || 'month'; // 基础参数对象 const params: any = { @@ -62,11 +74,11 @@ const CarType: React.FC = ({ // 处理日期参数 if (effectiveType === 'range') { - params.customizeStart = formatDateTime(currentMapState.rightPanelTime.startTime); - params.customizeEnd = formatDateTime(currentMapState.rightPanelTime.endTime, true); + params.customizeStart = formatDateTime(leftPanelTime.startTime); + params.customizeEnd = formatDateTime(leftPanelTime.endTime, true); } else { // 年、月、日视图使用time参数 - params.time = formatDateTime(currentMapState.rightPanelTime.startTime); + params.time = formatDateTime(leftPanelTime.startTime); } // 处理区域参数 @@ -87,21 +99,21 @@ const CarType: React.FC = ({ params.subTypeCode = useDataType(dataTypeKey); return params; - }, [mapStateRef, dateType, translateDateType, dataTypeKey]); + }, [mapStateRef, dateType, translateDateType, dataTypeKey, pageId]); - // 使用useApiControl管理问题来源API调用 - const sourceSystemRankApi = useApiControl({ - apiFunction: async (params) => { - console.log('发起问题来源请求,参数:', params); + // 使用usePageAwareApi管理环卫车辆类型API调用 + const sourceSystemRankApi = usePageAwareApi({ + apiFunction: async (params: any) => { + console.log('发起环卫车辆类型请求,参数:', params); const response = await getSourceSystemRank(params); if (response.code === 200 && response.data) { return response.data; } - throw new Error('获取问题来源数据失败'); + throw new Error('获取环卫车辆类型数据失败'); }, - onSuccess: (data) => { + onSuccess: (data: any) => { if (!data || !Array.isArray(data) || data.length === 0) { let useMockData = true; if (useMockData) { @@ -118,7 +130,7 @@ const CarType: React.FC = ({ { name: '郫都区', value: 118, percent: '10.2' }, { name: '都江堰市', value: 117, percent: '10.1' } ]; - console.log('使用模拟问题来源数据'); + console.log('使用模拟环卫车辆类型数据'); }else{ setData([]); return; @@ -137,16 +149,17 @@ const CarType: React.FC = ({ percentage: parseFloat(item.percent) || 0 })); - console.log('问题来源数据成功:', formattedData); + console.log('环卫车辆类型数据成功:', formattedData); setData(formattedData); }, - onError: (error) => { - console.error('获取问题来源数据出错:', error); + onError: (error: any) => { + console.error('获取环卫车辆类型数据出错:', error); setData([]); }, - apiName: '问题来源', + apiName: '环卫车辆类型', debounceTime: 300, - cacheTime: 5000 // 缓存5秒,避免频繁刷新时重复请求 + cacheTime: 5000, // 缓存5秒,避免频繁刷新时重复请求 + pageId: pageId, // 传入页面ID用于页面感知 }); // 在地图状态变化时加载数据 @@ -155,14 +168,14 @@ const CarType: React.FC = ({ // 如果没有必要的参数,不发送请求 if (!params) { - console.log('缺少必要的日期参数,不加载问题来源数据'); + console.log('缺少必要的日期参数,不加载环卫车辆类型数据'); return; } - console.log('加载问题来源数据,参数:', params); + // 调用API - 现在usePageAwareApi内部会处理参数比较和页面状态 sourceSystemRankApi.callApi(params); - }, [mapState.regionCode, mapState.rightPanelTime, buildApiParams, sourceSystemRankApi]); + }, [mapState.regionCode, mapState.leftPanelTime, mapState.pageTimeSettings, buildApiParams, sourceSystemRankApi]); return (
diff --git a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/WorkersArea/index.tsx b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/WorkersArea/index.tsx index 8f089cbf1a557ba4a1c7f4f3c50105883bbbed0c..78238fca75175db39fe69ac7f9c4545801ac0aed 100644 --- a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/WorkersArea/index.tsx +++ b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/components/WorkersArea/index.tsx @@ -5,7 +5,8 @@ import AreaBarList, { AreaBarItem } from '@/components/V2504/AreaBarList' import ModuleTitleWithTabs from '@/pages/Home_v_2504/components/common/ModuleTitleWithTabs' import useLatestValue from '@/pages/Home_v_2504/hooks/useLatestValue'; import { useDataType, useMapState } from '@/pages/Home_v_2504/components/MapStateContext'; -import useApiControl, { formatDateTime } from '@/pages/Home_v_2504/hooks/useApiControl'; +import { formatDateTime } from '@/pages/Home_v_2504/hooks/useApiControl'; +import usePageAwareApi from '@/pages/Home_v_2504/hooks/usePageAwareApi'; import { getDistrictHighFrequencyArea } from '@/services/CityProblem'; // 类型定义 @@ -13,6 +14,7 @@ export interface HighFrequencyAreaProps { dateType?: string; dateRange?: { startTime: string; endTime: string }; dataTypeKey?: string; + pageId?: string; } // 区域类型定义 @@ -20,7 +22,8 @@ export type AreaType = 'all' | 'urban' | 'central' | 'county'; const WorkersArea: React.FC = ({ dateType, dateRange, - dataTypeKey = '环卫事件' + dataTypeKey = '环卫事件', + pageId = 'environment', }) => { // 地图状态 @@ -101,8 +104,17 @@ const WorkersArea: React.FC = ({ return null; } + // 获取当前页面的时间设置 + const pageTimeSettings = currentMapState.pageTimeSettings?.[pageId]; + if (!pageTimeSettings) { + console.log(`缺少${pageId}页面的时间设置,使用全局设置`); + } + + // 使用页面级别的时间设置,如果存在的话 + const leftPanelTime = pageTimeSettings?.leftPanelTime || currentMapState.leftPanelTime; + // 使用当前时间类型,如果不存在则使用props或默认值 - const effectiveType = currentMapState.leftPanelTime.type || dateType || 'month'; + const effectiveType = leftPanelTime.type || dateType || 'month'; // 基础参数对象 const params: any = { @@ -111,11 +123,11 @@ const WorkersArea: React.FC = ({ // 处理日期参数 if (effectiveType === 'range') { - params.customizeStart = formatDateTime(currentMapState.rightPanelTime.startTime); - params.customizeEnd = formatDateTime(currentMapState.rightPanelTime.endTime, true); + params.customizeStart = formatDateTime(leftPanelTime.startTime); + params.customizeEnd = formatDateTime(leftPanelTime.endTime, true); } else { // 年、月、日视图使用time参数 - params.time = formatDateTime(currentMapState.rightPanelTime.startTime); + params.time = formatDateTime(leftPanelTime.startTime); } // 获取区域编码列表 @@ -142,11 +154,11 @@ const WorkersArea: React.FC = ({ params.subTypeCode = useDataType(dataTypeKey); return params; - }, [mapStateRef, currentTabRef, dateType, translateDateType, dataTypeKey]); + }, [mapStateRef, currentTabRef, dateType, translateDateType, dataTypeKey, pageId]); - // 使用useApiControl管理环卫人员区域API调用 - const highFrequencyApi = useApiControl({ - apiFunction: async (params) => { + // 使用usePageAwareApi管理环卫人员区域API调用 + const highFrequencyApi = usePageAwareApi({ + apiFunction: async (params: any) => { console.log('发起环卫人员区域请求,参数:', params); const response = await getDistrictHighFrequencyArea(params); @@ -156,7 +168,7 @@ const WorkersArea: React.FC = ({ throw new Error('获取环卫人员区域数据失败'); }, - onSuccess: (data) => { + onSuccess: (data: any) => { if (!data || !Array.isArray(data) || data.length === 0) { setData([]); return; @@ -190,31 +202,16 @@ const WorkersArea: React.FC = ({ console.log('环卫人员区域数据成功:'); setData(formattedData); }, - onError: (error) => { + onError: (error: any) => { console.error('获取环卫人员区域数据出错:', error); setData([]); }, apiName: '环卫人员区域', debounceTime: 300, - cacheTime: 5000 // 缓存5秒,避免频繁刷新时重复请求 + cacheTime: 5000, // 缓存5秒,避免频繁刷新时重复请求 + pageId: pageId, // 传入页面ID用于页面感知 }); - // 使用 useMemo 跟踪 rightPanelTime 的实际变化 - const memoizedRightPanelTime = useMemo(() => { - return { - type: mapState.rightPanelTime.type, - startTime: mapState.rightPanelTime.startTime, - endTime: mapState.rightPanelTime.endTime - }; - }, [ - mapState.rightPanelTime.type, - mapState.rightPanelTime.startTime, - mapState.rightPanelTime.endTime - ]); - - // 跟踪上一次的API参数,避免重复调用 - const prevApiParamsRef = useRef(''); - // 在地图状态变化和Tab变化时加载数据 useEffect(() => { const params = buildApiParams(); @@ -225,22 +222,10 @@ const WorkersArea: React.FC = ({ return; } - // 参数签名,用于比较参数是否有实际变化 - const paramsSignature = JSON.stringify(params); - - // 检查参数是否真正变化,防止重复调用 - if (paramsSignature === prevApiParamsRef.current) { - console.log('环卫人员区域参数未变化,跳过重复加载'); - return; - } - - console.log('加载环卫人员区域数据,参数:', params); - // highFrequencyApi.callApi(params); - - // 更新上一次参数签名 - prevApiParamsRef.current = paramsSignature; + // 调用API - 现在usePageAwareApi内部会处理参数比较和页面状态 + highFrequencyApi.callApi(params); - }, [mapState.regionCode, memoizedRightPanelTime, currentTab, buildApiParams, highFrequencyApi]); + }, [mapState.regionCode, mapState.leftPanelTime, mapState.pageTimeSettings, currentTab, buildApiParams, highFrequencyApi]); return (
diff --git a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/index.tsx b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/index.tsx index cf65401c6758dba8c5f3fc19158b6fc3dcec9151..cea6ab9cb06ff82f1db0109f331b12219dea4025 100644 --- a/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/index.tsx +++ b/src/pages/Home_v_2504/components/SubPages/Environment/components/LeftContent/index.tsx @@ -35,13 +35,18 @@ const LeftContent: React.FC = () => {
- + - +
);