diff --git a/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/chartView.tsx b/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/chartView.tsx index 646c8bd93d..8f8079c004 100644 --- a/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/chartView.tsx +++ b/web/app/(commonLayout)/app/(appDetailLayout)/[appId]/overview/chartView.tsx @@ -1,5 +1,5 @@ 'use client' -import React, { useState } from 'react' +import React, { useEffect, useState } from 'react' import dayjs from 'dayjs' import quarterOfYear from 'dayjs/plugin/quarterOfYear' import { useTranslation } from 'react-i18next' @@ -9,6 +9,7 @@ import type { Item } from '@/app/components/base/select' import { SimpleSelect } from '@/app/components/base/select' import { TIME_PERIOD_MAPPING } from '@/app/components/app/log/filter' import { useStore as useAppStore } from '@/app/components/app/store' +import { useRouter } from 'next/navigation' dayjs.extend(quarterOfYear) @@ -21,12 +22,44 @@ export type IChartViewProps = { headerRight: React.ReactNode } +let state = 0 +const useIframeRedirection = () => { + const router = useRouter() + + useEffect(() => { + // 监听父级指定操作 + const handler = (event: MessageEvent) => { + // if (event.origin !== "https://gcgj.ngsk.tech:7001") return; + if ( + event.data.type === 'CHART_VIEW_REDIRECT' + && event.data.redirectUrl + && state < 3 // 防止无限重定向 + ) { + state++ + router.replace(event.data.redirectUrl) + } + } + + // 首页初始化完成后提示iframe操作 + window.parent.postMessage( + { + type: 'CHART_VIEW', + }, + '*', + ) + + window.addEventListener('message', handler) + return () => window.removeEventListener('message', handler) + }, []) +} + export default function ChartView({ appId, headerRight }: IChartViewProps) { const { t } = useTranslation() const appDetail = useAppStore(state => state.appDetail) const isChatApp = appDetail?.mode !== 'completion' && appDetail?.mode !== 'workflow' const isWorkflow = appDetail?.mode === 'workflow' const [period, setPeriod] = useState({ name: t('appLog.filter.period.last7days'), query: { start: today.subtract(7, 'day').startOf('day').format(queryDateFormat), end: today.endOf('day').format(queryDateFormat) } }) + useIframeRedirection() const onSelect = (item: Item) => { if (item.value === -1) {