fix(ecom-dmb): 公司概览页切换日期从6次→1次,消除重复tab创建问题

This commit is contained in:
小蟑螂
2026-04-24 13:28:48 +08:00
parent 4c5239237d
commit 7e53ebcd25
@@ -106,13 +106,13 @@ const customDateRange = ref<[dayjs.Dayjs, dayjs.Dayjs] | undefined>([
dateRange.value[1], dateRange.value[1],
]); ]);
const lastRequestKey = ref(''); const lastRequestKey = ref('');
const isSyncingRouteQuery = ref(false); const isRequesting = ref(false);
// 图表引用 // 图表引用
const shopChartsRef = ref<{ renderShopBarChart: () => void; renderShopScatterChart: () => void }>(); const shopChartsRef = ref<{ renderShopBarChart: () => void; renderShopScatterChart: () => void }>();
// 切换时间范围 // 切换时间范围
const switchTimeRange = ( const switchTimeRange = async (
type: '7days' | '30days' | 'custom' | 'today' | 'yesterday', type: '7days' | '30days' | 'custom' | 'today' | 'yesterday',
) => { ) => {
activeTimeRange.value = type; activeTimeRange.value = type;
@@ -148,10 +148,12 @@ const switchTimeRange = (
dateRange.value = [startDate, endDate]; dateRange.value = [startDate, endDate];
customDateRange.value = [startDate, endDate]; customDateRange.value = [startDate, endDate];
syncDateRangeToRouteQuery();
await refreshOverviewByCurrentParams();
}; };
// 自定义日期范围变化 // 自定义日期范围变化
const handleCustomDateRangeChange = ( const handleCustomDateRangeChange = async (
value: [dayjs.Dayjs, dayjs.Dayjs] | [string, string] | null, value: [dayjs.Dayjs, dayjs.Dayjs] | [string, string] | null,
_dateString?: [string, string], _dateString?: [string, string],
) => { ) => {
@@ -163,6 +165,8 @@ const handleCustomDateRangeChange = (
customDateRange.value = dates; customDateRange.value = dates;
activeTimeRange.value = 'custom'; activeTimeRange.value = 'custom';
dateRange.value = dates; dateRange.value = dates;
syncDateRangeToRouteQuery();
await refreshOverviewByCurrentParams();
} }
}; };
@@ -237,19 +241,11 @@ const syncDateRangeToRouteQuery = async () => {
return; return;
} }
isSyncingRouteQuery.value = true; // 用 history.replaceState 直接改 URL,避免触发 Vue Router 导航和 Vben tab 重复创建
try { const url = new URL(window.location.href);
await router.replace({ url.searchParams.set('startDate', startDate);
path: route.path, url.searchParams.set('endDate', endDate);
query: { window.history.replaceState({}, '', url.toString());
...route.query,
endDate,
startDate,
},
});
} finally {
isSyncingRouteQuery.value = false;
}
}; };
// 加载店铺数量 // 加载店铺数量
@@ -268,6 +264,8 @@ const loadShopCount = async () => {
// 加载概览数据 // 加载概览数据
const loadOverviewData = async () => { const loadOverviewData = async () => {
if (isRequesting.value) return;
isRequesting.value = true;
try { try {
loading.value = true; loading.value = true;
const params: CompanyOverviewDTO.Query = { const params: CompanyOverviewDTO.Query = {
@@ -283,6 +281,7 @@ const loadOverviewData = async () => {
console.error('获取全店概览数据失败:', error); console.error('获取全店概览数据失败:', error);
} finally { } finally {
loading.value = false; loading.value = false;
isRequesting.value = false;
} }
}; };
@@ -312,23 +311,6 @@ watch(
{ deep: true }, { deep: true },
); );
// 监听日期范围变化
watch(
() => dateRange.value,
async () => {
await syncDateRangeToRouteQuery();
await refreshOverviewByCurrentParams();
},
);
watch(
() => [route.query.startDate, route.query.endDate],
async () => {
if (isSyncingRouteQuery.value) return;
await refreshOverviewByCurrentParams();
},
);
// 深色模式 class 变化时重绘图表 // 深色模式 class 变化时重绘图表
let darkThemeObserver: MutationObserver | null = null; let darkThemeObserver: MutationObserver | null = null;