import React, { useEffect, useMemo, useState, useRef } from "react"; import { useLocation } from "react-router-dom"; import { Col, Row, Spin, Empty, theme, Button, Grid } from "antd"; import { VerticalLeftOutlined } from "@ant-design/icons"; import HydrocarbonService from "../../tmpl/hcservice-v3"; import WebSocket from "../../tmpl/web-socket"; import { CardListConfig } from "../../tmpl/interface"; import CardListItem from "../card-list/card-list-item"; import "../card-list/index.less"; const { useToken } = theme; const { useBreakpoint } = Grid; // 处理关系字段显示 const handleRelationVal = (val: string) => { if (!val) { return val; } if (typeof val === "string" && val?.includes("@R@")) { return val.split("@R@")[1]; } else { return val; } } // 处理数据格式 const handleData = (data: { config: { [key: string]: string }, value: { [key: string]: any }[] }[]) => { if (!data?.length) { return {}; } const { config, value } = data[0] || {}; // 事实数据字段 const vNames = Object.keys(config).filter((r) => r.startsWith("v") && !r.includes("-")).sort((a, b) => parseInt(a.substring(1)) - parseInt(b.substring(1))); const temData = value?.reduce((total, item) => { if (item["d1"]) { total[handleRelationVal(item["d1"])] = vNames.map((k) => ({ name: config[k], value: item[k], color: item[`${k}-color`], suffix: config[`${k}-suffix`] })); } return total; }, {}); return temData; }; type Props = { cardListConfig?: CardListConfig; dataSource?: { [key: string]: any }[]; columns?: any[]; serverKey?: string; sourceId?: string; // 菜单sourceId loading?: boolean; errorText?: string; total?: number; pageInfo?: { pageNo: number, pageSize: number }; onChangePage?: (pageNo: number, pageSize: number) => void; }; const Index = (props: Props) => { const { cardListConfig, dataSource, columns, serverKey, sourceId, loading, errorText, total, pageInfo, onChangePage } = props; const [showNextPage, setShowNextPage] = useState(false); const [relTimeData, setRelTimeData] = useState<{ [key: string]: { name: string, value: any, color?: string, suffix?: string }[] }>({}); // 实时数据 const { token } = useToken(); const location = useLocation(); const screens = useBreakpoint(); const wsRef = useRef(undefined); // 实时数据websocket连接 const subscribeRef = useRef(undefined); // 实时数据订阅 const renderOpt = (item: unknown) => { const actionCol = columns.find(col => col["操作"] == 1); return actionCol?.render(item, item); } const colFlex = useMemo(() => { const rowCardCount = cardListConfig?.rowCardCount || 5; const flex = Math.floor((100 / rowCardCount) * 100) / 100; return `${flex}%`; }, [cardListConfig]); // 数据源Id const chartDataSourceId = useMemo(() => cardListConfig?.chartDataSourceIds?.[0], [cardListConfig]); // 判断是否显示下一页卡片 const handleShowNextPage = () => { const { pageNo, pageSize } = pageInfo; const isNextPage = pageNo * pageSize < total; const col = { xl: Number(colFlex.split("%")[0]), lg: 25, sm: 50 }; if (isNextPage) { const rowCount = Math.round(100 / (screens.xl ? col.xl : (screens.lg ? col.lg : col.sm))); const temShow = pageSize % rowCount !== 0; setShowNextPage(temShow); } else { setShowNextPage(false); } } useEffect(() => { if (onChangePage && total && pageInfo && colFlex) { handleShowNextPage(); } else { setShowNextPage(false); } }, [pageInfo, total, colFlex, screens]); // 默认请求实时数据 const getData = async () => { if (!dataSource?.length) return; const secondCriterion = dataSource.map(r => r.code); // 查询条件-次条件 const { data } = await HydrocarbonService.requestChartData(null, chartDataSourceId, { secondCriterion, pageSize: secondCriterion.length }) || {}; const temData = handleData(data?.value); setRelTimeData(temData); } // 订阅实时数据 const webSocketData = async () => { if (subscribeRef.current) subscribeRef.current.unsubscribe(); if (!dataSource?.length) return; // 订阅查询条件-次条件 const secondCriterion = dataSource.map(r => r.code); wsRef.current = WebSocket.getInstance("v3/kk-ws"); // 订阅实时数据 subscribeRef.current = await wsRef.current.subscribe(`/user/queue/chart-data/${sourceId}`, { dataSourceConfigs: JSON.stringify([{ sourceId: chartDataSourceId, chartCode: chartDataSourceId, secondCriterion, pageSize: secondCriterion.length }]) }, (data) => { const temVal: any = Object.values(data)[0]; const temData = handleData(temVal?.value); setRelTimeData(temData); }); } useEffect(() => { if (sourceId && chartDataSourceId && location.pathname?.includes(sourceId)) { getData(); webSocketData(); } else if (subscribeRef.current) { // 当前tab页不是激活tab页,取消订阅实时数据 subscribeRef.current.unsubscribe(); } return () => { if (subscribeRef.current) subscribeRef.current.unsubscribe(); } }, [chartDataSourceId, sourceId, dataSource, location.pathname]); useEffect(() => { return () => { if (subscribeRef.current) subscribeRef.current.unsubscribe(); } }, []); // 显示下一页卡片 const renderNextPage = () => { return ; } return (
{!dataSource?.length ? : {dataSource?.map((item, index) => )} {showNextPage ? {renderNextPage()} : ""} }
); }; export default Index;