mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
fix(ecom-dmb): 店铺概览切换日期6次→1次,消除重复tab;history.replaceState配套window.location读取URL
This commit is contained in:
@@ -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());
|
||||
};
|
||||
|
||||
// 加载店铺数量
|
||||
|
||||
@@ -74,7 +74,7 @@ const customDateRange = ref<[Dayjs, Dayjs] | undefined>([
|
||||
dateRange.value[1],
|
||||
]);
|
||||
const lastRequestKey = ref('');
|
||||
const isSyncingRouteQuery = ref(false);
|
||||
const isRequesting = ref(false);
|
||||
|
||||
// 税率(持久化)
|
||||
const TAX_RATE_KEY = 'overview-tax-rate';
|
||||
@@ -94,11 +94,15 @@ const timeRangeState = computed(() => ({
|
||||
}));
|
||||
|
||||
// 监听子组件的时间范围变化
|
||||
const handleTimeRangeChange = (val: typeof timeRangeState.value) => {
|
||||
const handleTimeRangeChange = async (val: typeof timeRangeState.value) => {
|
||||
taxRate.value = val.taxRate;
|
||||
activeTimeRange.value = val.activeTimeRange;
|
||||
customDateRange.value = val.customDateRange;
|
||||
dateRange.value = val.dateRange;
|
||||
// 手动切换时间时清除请求缓存,确保强制刷新
|
||||
lastRequestKey.value = '';
|
||||
syncDateRangeToRouteQuery();
|
||||
await refreshOverviewByCurrentParams();
|
||||
};
|
||||
|
||||
const setTimeRangeTypeByDates = (
|
||||
@@ -146,11 +150,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 =
|
||||
@@ -168,28 +175,25 @@ 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;
|
||||
}
|
||||
|
||||
isSyncingRouteQuery.value = true;
|
||||
try {
|
||||
await router.replace({
|
||||
path: route.path,
|
||||
query: {
|
||||
...route.query,
|
||||
endDate,
|
||||
startDate,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
isSyncingRouteQuery.value = false;
|
||||
}
|
||||
const newUrl = new URL(window.location.href);
|
||||
newUrl.searchParams.set('startDate', startDate);
|
||||
newUrl.searchParams.set('endDate', endDate);
|
||||
window.history.replaceState({}, '', newUrl.toString());
|
||||
};
|
||||
|
||||
// 加载概览数据
|
||||
const loadOverviewData = async () => {
|
||||
if (!shopId.value) return;
|
||||
if (isRequesting.value) return;
|
||||
isRequesting.value = true;
|
||||
try {
|
||||
loading.value = true;
|
||||
const params: ShopDTO.OverviewQuery = {
|
||||
@@ -207,6 +211,7 @@ const loadOverviewData = async () => {
|
||||
message.error('获取店铺概览数据失败');
|
||||
} finally {
|
||||
loading.value = false;
|
||||
isRequesting.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -282,20 +287,10 @@ const refreshOverviewByCurrentParams = async (force = false) => {
|
||||
await loadOverviewData();
|
||||
};
|
||||
|
||||
// 监听日期范围变化
|
||||
watch(
|
||||
() => dateRange.value,
|
||||
async () => {
|
||||
await syncDateRangeToRouteQuery();
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
// 监听路由变化
|
||||
watch(
|
||||
() => [route.params.id, route.query.startDate, route.query.endDate],
|
||||
async () => {
|
||||
if (isSyncingRouteQuery.value) return;
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user