| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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> | void;
- }
- const ANALYSIS_REFRESH_VERSION_KEY_PREFIX = 'analysis-refresh-version:';
- const ls = createLocalStorage({ timeout: null });
- const analysisRefreshStateMap = new Map<string, AnalysisRefreshState>();
- 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<RouteLocationNormalized> | 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> | 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<boolean>((resolve) => {
- Modal.confirm({
- title: '数据更新提醒',
- content: '数据发生变化,是否更新?',
- centered: true,
- okText: '更新',
- cancelText: '暂不更新',
- onOk: async () => {
- markAnalysisRefreshPending(routeKey);
- resolve(true);
- },
- onCancel: () => {
- markAnalysisRefreshPrompted(routeKey);
- resolve(true);
- },
- });
- });
|