import type { RouteLocationNormalized } from 'vue-router'; import { Modal } from 'ant-design-vue'; import { createLocalStorage } from '@jeesite/core/utils/cache'; import { useUserStoreWithOut } from '@jeesite/core/store/modules/case'; interface AnalysisRefreshState { hasData: boolean; syncedVersion: number; promptedVersion: number; pendingRefreshVersion: number; refreshHandler?: () => Promise | void; } const ANALYSIS_REFRESH_VERSION_KEY_PREFIX = 'analysis-refresh-version:'; const ls = createLocalStorage({ timeout: null }); const analysisRefreshStateMap = new Map(); const normalizeCaseId = () => { const caseId = String(useUserStoreWithOut().getCaseInfo?.id || '').trim(); return caseId || 'default'; }; const getVersionCacheKey = (caseId = normalizeCaseId()) => `${ANALYSIS_REFRESH_VERSION_KEY_PREFIX}${caseId}`; export const resolveAnalysisRefreshRouteKey = (route?: Partial | null) => String(route?.path || '').trim(); const getCurrentVersion = () => { const version = Number(ls.get(getVersionCacheKey(), 0)); return Number.isFinite(version) ? version : 0; }; const getOrCreateState = (routeKey: string) => { const normalizedRouteKey = String(routeKey || '').trim(); if (!normalizedRouteKey) return null; let state = analysisRefreshStateMap.get(normalizedRouteKey); if (!state) { state = { hasData: false, syncedVersion: 0, promptedVersion: 0, pendingRefreshVersion: 0, }; analysisRefreshStateMap.set(normalizedRouteKey, state); } return state; }; export const registerAnalysisRefreshHandler = (routeKey: string, refreshHandler: () => Promise | void) => { const state = getOrCreateState(routeKey); if (!state) return; state.refreshHandler = refreshHandler; }; export const unregisterAnalysisRefreshHandler = (routeKey: string) => { const normalizedRouteKey = String(routeKey || '').trim(); if (!normalizedRouteKey) return; analysisRefreshStateMap.delete(normalizedRouteKey); }; export const syncAnalysisRefreshState = (routeKey: string, hasData: boolean) => { const state = getOrCreateState(routeKey); if (!state) return; const currentVersion = getCurrentVersion(); state.hasData = hasData; state.syncedVersion = currentVersion; state.promptedVersion = currentVersion; state.pendingRefreshVersion = 0; }; export const markAnalysisDataChanged = () => { const currentVersion = getCurrentVersion(); const nextVersion = Math.max(Date.now(), currentVersion + 1); ls.set(getVersionCacheKey(), nextVersion, null); return nextVersion; }; export const shouldPromptAnalysisRefresh = (routeKey: string) => { const normalizedRouteKey = String(routeKey || '').trim(); if (!normalizedRouteKey) return false; const state = analysisRefreshStateMap.get(normalizedRouteKey); if (!state?.hasData) return false; const currentVersion = getCurrentVersion(); return currentVersion > state.syncedVersion && state.promptedVersion < currentVersion; }; export const markAnalysisRefreshPrompted = (routeKey: string) => { const state = getOrCreateState(routeKey); if (!state) return; state.promptedVersion = getCurrentVersion(); }; export const markAnalysisRefreshPending = (routeKey: string) => { const state = getOrCreateState(routeKey); if (!state) return; const currentVersion = getCurrentVersion(); state.promptedVersion = currentVersion; state.pendingRefreshVersion = currentVersion; }; export const consumePendingAnalysisRefresh = (routeKey: string) => { const normalizedRouteKey = String(routeKey || '').trim(); if (!normalizedRouteKey) return null; const state = analysisRefreshStateMap.get(normalizedRouteKey); if (!state?.pendingRefreshVersion || !state.refreshHandler) return null; state.pendingRefreshVersion = 0; return state.refreshHandler; }; export const createAnalysisRefreshConfirm = (routeKey: string) => new Promise((resolve) => { Modal.confirm({ title: '数据更新提醒', content: '数据发生变化,是否更新?', centered: true, okText: '更新', cancelText: '暂不更新', onOk: async () => { markAnalysisRefreshPending(routeKey); resolve(true); }, onCancel: () => { markAnalysisRefreshPrompted(routeKey); resolve(true); }, }); });