fix(ecom-dmb): 店铺概览切换日期6次→1次,消除重复tab;history.replaceState配套window.location读取URL

This commit is contained in:
小蟑螂
2026-04-24 13:51:12 +08:00
parent 7e53ebcd25
commit 30728ee711
2 changed files with 42 additions and 38 deletions
@@ -148,6 +148,8 @@ const switchTimeRange = async (
dateRange.value = [startDate, endDate];
customDateRange.value = [startDate, endDate];
// 手动切换时间时清除请求缓存,确保强制刷新
lastRequestKey.value = '';
syncDateRangeToRouteQuery();
await refreshOverviewByCurrentParams();
};
@@ -165,6 +167,7 @@ const handleCustomDateRangeChange = async (
customDateRange.value = dates;
activeTimeRange.value = 'custom';
dateRange.value = dates;
lastRequestKey.value = '';
syncDateRangeToRouteQuery();
await refreshOverviewByCurrentParams();
}
@@ -215,11 +218,14 @@ const setTimeRangeTypeByDates = (
};
const applyDateRangeFromRouteQuery = () => {
const { endDate, startDate } = route.query;
// 用 window.location 而非 route.query,因为 syncDateRangeToRouteQuery 用的是 history.replaceState
const url = new URL(window.location.href);
const startDate = url.searchParams.get('startDate');
const endDate = url.searchParams.get('endDate');
if (!startDate || !endDate) return false;
const parsedStartDate = dayjs(startDate as string);
const parsedEndDate = dayjs(endDate as string);
const parsedStartDate = dayjs(startDate);
const parsedEndDate = dayjs(endDate);
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
const isSameRange =
@@ -237,15 +243,18 @@ const syncDateRangeToRouteQuery = async () => {
const startDate = dateRange.value[0].format('YYYY-MM-DD');
const endDate = dateRange.value[1].format('YYYY-MM-DD');
if (route.query.startDate === startDate && route.query.endDate === endDate) {
// 从 window.location 读取(history.replaceState 不会更新 route.query)
const url = new URL(window.location.href);
const curStart = url.searchParams.get('startDate');
const curEnd = url.searchParams.get('endDate');
if (curStart === startDate && curEnd === endDate) {
return;
}
// 用 history.replaceState 直接改 URL,避免触发 Vue Router 导航和 Vben tab 重复创建
const url = new URL(window.location.href);
url.searchParams.set('startDate', startDate);
url.searchParams.set('endDate', endDate);
window.history.replaceState({}, '', url.toString());
const newUrl = new URL(window.location.href);
newUrl.searchParams.set('startDate', startDate);
newUrl.searchParams.set('endDate', endDate);
window.history.replaceState({}, '', newUrl.toString());
};
// 加载店铺数量