mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 05:09: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];
|
dateRange.value = [startDate, endDate];
|
||||||
customDateRange.value = [startDate, endDate];
|
customDateRange.value = [startDate, endDate];
|
||||||
|
// 手动切换时间时清除请求缓存,确保强制刷新
|
||||||
|
lastRequestKey.value = '';
|
||||||
syncDateRangeToRouteQuery();
|
syncDateRangeToRouteQuery();
|
||||||
await refreshOverviewByCurrentParams();
|
await refreshOverviewByCurrentParams();
|
||||||
};
|
};
|
||||||
@@ -165,6 +167,7 @@ const handleCustomDateRangeChange = async (
|
|||||||
customDateRange.value = dates;
|
customDateRange.value = dates;
|
||||||
activeTimeRange.value = 'custom';
|
activeTimeRange.value = 'custom';
|
||||||
dateRange.value = dates;
|
dateRange.value = dates;
|
||||||
|
lastRequestKey.value = '';
|
||||||
syncDateRangeToRouteQuery();
|
syncDateRangeToRouteQuery();
|
||||||
await refreshOverviewByCurrentParams();
|
await refreshOverviewByCurrentParams();
|
||||||
}
|
}
|
||||||
@@ -215,11 +218,14 @@ const setTimeRangeTypeByDates = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const applyDateRangeFromRouteQuery = () => {
|
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;
|
if (!startDate || !endDate) return false;
|
||||||
|
|
||||||
const parsedStartDate = dayjs(startDate as string);
|
const parsedStartDate = dayjs(startDate);
|
||||||
const parsedEndDate = dayjs(endDate as string);
|
const parsedEndDate = dayjs(endDate);
|
||||||
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
||||||
|
|
||||||
const isSameRange =
|
const isSameRange =
|
||||||
@@ -237,15 +243,18 @@ const syncDateRangeToRouteQuery = async () => {
|
|||||||
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
||||||
const endDate = dateRange.value[1].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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用 history.replaceState 直接改 URL,避免触发 Vue Router 导航和 Vben tab 重复创建
|
const newUrl = new URL(window.location.href);
|
||||||
const url = new URL(window.location.href);
|
newUrl.searchParams.set('startDate', startDate);
|
||||||
url.searchParams.set('startDate', startDate);
|
newUrl.searchParams.set('endDate', endDate);
|
||||||
url.searchParams.set('endDate', endDate);
|
window.history.replaceState({}, '', newUrl.toString());
|
||||||
window.history.replaceState({}, '', url.toString());
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 加载店铺数量
|
// 加载店铺数量
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ const customDateRange = ref<[Dayjs, Dayjs] | undefined>([
|
|||||||
dateRange.value[1],
|
dateRange.value[1],
|
||||||
]);
|
]);
|
||||||
const lastRequestKey = ref('');
|
const lastRequestKey = ref('');
|
||||||
const isSyncingRouteQuery = ref(false);
|
const isRequesting = ref(false);
|
||||||
|
|
||||||
// 税率(持久化)
|
// 税率(持久化)
|
||||||
const TAX_RATE_KEY = 'overview-tax-rate';
|
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;
|
taxRate.value = val.taxRate;
|
||||||
activeTimeRange.value = val.activeTimeRange;
|
activeTimeRange.value = val.activeTimeRange;
|
||||||
customDateRange.value = val.customDateRange;
|
customDateRange.value = val.customDateRange;
|
||||||
dateRange.value = val.dateRange;
|
dateRange.value = val.dateRange;
|
||||||
|
// 手动切换时间时清除请求缓存,确保强制刷新
|
||||||
|
lastRequestKey.value = '';
|
||||||
|
syncDateRangeToRouteQuery();
|
||||||
|
await refreshOverviewByCurrentParams();
|
||||||
};
|
};
|
||||||
|
|
||||||
const setTimeRangeTypeByDates = (
|
const setTimeRangeTypeByDates = (
|
||||||
@@ -146,11 +150,14 @@ const setTimeRangeTypeByDates = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
const applyDateRangeFromRouteQuery = () => {
|
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;
|
if (!startDate || !endDate) return false;
|
||||||
|
|
||||||
const parsedStartDate = dayjs(startDate as string);
|
const parsedStartDate = dayjs(startDate);
|
||||||
const parsedEndDate = dayjs(endDate as string);
|
const parsedEndDate = dayjs(endDate);
|
||||||
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
||||||
|
|
||||||
const isSameRange =
|
const isSameRange =
|
||||||
@@ -168,28 +175,25 @@ const syncDateRangeToRouteQuery = async () => {
|
|||||||
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
||||||
const endDate = dateRange.value[1].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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
isSyncingRouteQuery.value = true;
|
const newUrl = new URL(window.location.href);
|
||||||
try {
|
newUrl.searchParams.set('startDate', startDate);
|
||||||
await router.replace({
|
newUrl.searchParams.set('endDate', endDate);
|
||||||
path: route.path,
|
window.history.replaceState({}, '', newUrl.toString());
|
||||||
query: {
|
|
||||||
...route.query,
|
|
||||||
endDate,
|
|
||||||
startDate,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
} finally {
|
|
||||||
isSyncingRouteQuery.value = false;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 加载概览数据
|
// 加载概览数据
|
||||||
const loadOverviewData = async () => {
|
const loadOverviewData = async () => {
|
||||||
if (!shopId.value) return;
|
if (!shopId.value) return;
|
||||||
|
if (isRequesting.value) return;
|
||||||
|
isRequesting.value = true;
|
||||||
try {
|
try {
|
||||||
loading.value = true;
|
loading.value = true;
|
||||||
const params: ShopDTO.OverviewQuery = {
|
const params: ShopDTO.OverviewQuery = {
|
||||||
@@ -207,6 +211,7 @@ const loadOverviewData = async () => {
|
|||||||
message.error('获取店铺概览数据失败');
|
message.error('获取店铺概览数据失败');
|
||||||
} finally {
|
} finally {
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
|
isRequesting.value = false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -282,20 +287,10 @@ const refreshOverviewByCurrentParams = async (force = false) => {
|
|||||||
await loadOverviewData();
|
await loadOverviewData();
|
||||||
};
|
};
|
||||||
|
|
||||||
// 监听日期范围变化
|
|
||||||
watch(
|
|
||||||
() => dateRange.value,
|
|
||||||
async () => {
|
|
||||||
await syncDateRangeToRouteQuery();
|
|
||||||
await refreshOverviewByCurrentParams();
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// 监听路由变化
|
// 监听路由变化
|
||||||
watch(
|
watch(
|
||||||
() => [route.params.id, route.query.startDate, route.query.endDate],
|
() => [route.params.id, route.query.startDate, route.query.endDate],
|
||||||
async () => {
|
async () => {
|
||||||
if (isSyncingRouteQuery.value) return;
|
|
||||||
await refreshOverviewByCurrentParams();
|
await refreshOverviewByCurrentParams();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user