mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 06:29:20 +08:00
fix(frontend): 修复 keep-alive 场景下路由参数串用导致的接口错乱
- 商品/店铺概览页增加 isProductOverviewRoute / isShopOverviewRoute 路由路径校验 - 防止 keep-alive 切换标签时 route.params.id 被其他页复用导致错误请求 - 全店概览页 Page 标题改为 template slot 方式,移除冗余的店铺数量拉取逻辑 - 同步优化各概览页 header 样式对齐
This commit is contained in:
@@ -20,8 +20,13 @@ defineOptions({ name: 'ProductOverview' });
|
||||
|
||||
const route = useRoute();
|
||||
|
||||
/** 当前是否为商品概览路由(keep-alive 下勿用其它页的 params.id 请求商品接口) */
|
||||
const isProductOverviewRoute = () =>
|
||||
/\/ecom\/product\/overview\/[^/]+\/?$/.test(route.path);
|
||||
|
||||
// 商品ID(从路由参数获取)
|
||||
const productId = computed(() => {
|
||||
if (!isProductOverviewRoute()) return 0;
|
||||
const id = route.params.id;
|
||||
const parsed = id ? Number(id) : 0;
|
||||
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
|
||||
@@ -101,7 +106,7 @@ const handleTimeRangeChange = async (val: typeof timeRangeState.value) => {
|
||||
|
||||
// 获取概览数据
|
||||
async function fetchOverview() {
|
||||
if (!productId.value) return;
|
||||
if (!isProductOverviewRoute() || !productId.value) return;
|
||||
|
||||
loading.value = true;
|
||||
try {
|
||||
@@ -119,10 +124,15 @@ async function fetchOverview() {
|
||||
}
|
||||
}
|
||||
|
||||
// 初始加载
|
||||
watch(productId, (val) => {
|
||||
if (val) fetchOverview();
|
||||
}, { immediate: true });
|
||||
// 初始加载(依赖 route.path,避免 keep-alive 下切到店铺页仍用店铺 id 请求商品)
|
||||
watch(
|
||||
() => [route.path, productId.value] as const,
|
||||
([path, id]) => {
|
||||
if (!/\/ecom\/product\/overview\/[^/]+\/?$/.test(path) || !id) return;
|
||||
fetchOverview();
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
// 计算 page title 用 productName
|
||||
const pageTitle = computed(() => overviewData.value?.productName || '商品概览');
|
||||
@@ -130,14 +140,15 @@ const pageTitle = computed(() => overviewData.value?.productName || '商品概
|
||||
|
||||
<template>
|
||||
<Page
|
||||
:title="pageTitle"
|
||||
auto-content-height
|
||||
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||||
header-class="product-overview-page-header"
|
||||
>
|
||||
<template #description>
|
||||
<div class="mt-1 text-xs font-bold italic leading-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] sm:text-sm">
|
||||
数据更新于: {{ dayjs().format('YYYY-MM-DD HH:mm:ss') }}
|
||||
<template #title>
|
||||
<div
|
||||
class="text-xl font-black tracking-tight text-slate-900 dark:text-[hsl(0deg_0%_98%)] sm:text-2xl md:text-3xl"
|
||||
>
|
||||
{{ pageTitle }}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -177,6 +188,10 @@ const pageTitle = computed(() => overviewData.value?.productName || '商品概
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
:deep(.product-overview-page-header) {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
/* 表格滚动条优化 */
|
||||
.table-container::-webkit-scrollbar {
|
||||
height: 6px;
|
||||
|
||||
@@ -28,8 +28,13 @@ defineOptions({ name: 'ShopOverview' });
|
||||
|
||||
const route = useRoute();
|
||||
|
||||
/** 当前是否为店铺概览路由(keep-alive 下 route.params.id 会随其它标签变化,不可仅凭 id 请求) */
|
||||
const isShopOverviewRoute = () =>
|
||||
/\/ecom\/shop\/overview\/[^/]+\/?$/.test(route.path);
|
||||
|
||||
// 店铺ID(从路由参数获取)
|
||||
const shopId = computed(() => {
|
||||
if (!isShopOverviewRoute()) return 0;
|
||||
const id = route.params.id;
|
||||
return id ? Number(id) : 0;
|
||||
});
|
||||
@@ -187,7 +192,7 @@ const syncDateRangeToRouteQuery = async () => {
|
||||
|
||||
// 加载概览数据
|
||||
const loadOverviewData = async () => {
|
||||
if (!shopId.value) return;
|
||||
if (!isShopOverviewRoute() || !shopId.value) return;
|
||||
if (isRequesting.value) return;
|
||||
isRequesting.value = true;
|
||||
try {
|
||||
@@ -281,6 +286,7 @@ const handleCategorySelectAllChange = (event: Event) => {
|
||||
};
|
||||
|
||||
const refreshOverviewByCurrentParams = async (force = false) => {
|
||||
if (!isShopOverviewRoute()) return;
|
||||
applyDateRangeFromRouteQuery();
|
||||
if (!shopId.value) return;
|
||||
|
||||
@@ -293,34 +299,43 @@ const refreshOverviewByCurrentParams = async (force = false) => {
|
||||
await loadOverviewData();
|
||||
};
|
||||
|
||||
// 监听路由变化
|
||||
// 监听路由变化(仅在本页路径下响应,避免切到商品概览等仍用当前 id 请求店铺接口)
|
||||
watch(
|
||||
() => [route.params.id, route.query.startDate, route.query.endDate],
|
||||
() => [
|
||||
route.path,
|
||||
route.params.id,
|
||||
route.query.startDate,
|
||||
route.query.endDate,
|
||||
],
|
||||
async () => {
|
||||
if (!isShopOverviewRoute()) return;
|
||||
await refreshOverviewByCurrentParams();
|
||||
},
|
||||
);
|
||||
|
||||
// 初始化
|
||||
onMounted(async () => {
|
||||
if (!isShopOverviewRoute()) return;
|
||||
await refreshOverviewByCurrentParams(true);
|
||||
});
|
||||
|
||||
onActivated(async () => {
|
||||
if (!isShopOverviewRoute()) return;
|
||||
await refreshOverviewByCurrentParams(true);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page
|
||||
:title="overviewData?.shopName ? `店铺经营概览(${overviewData.shopName})` : '店铺经营概览'"
|
||||
auto-content-height
|
||||
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||||
header-class="shop-overview-page-header"
|
||||
>
|
||||
<template #description>
|
||||
<div class="mt-1 text-xs font-bold italic leading-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] sm:text-sm">
|
||||
数据更新于:{{ dayjs().format('今日 HH:mm:ss') }}
|
||||
<template #title>
|
||||
<div
|
||||
class="text-xl font-black tracking-tight text-slate-900 dark:text-[hsl(0deg_0%_98%)] sm:text-2xl md:text-3xl"
|
||||
>
|
||||
{{ overviewData?.shopName ? `店铺经营概览(${overviewData.shopName})` : '店铺经营概览' }}
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -364,6 +379,10 @@ onActivated(async () => {
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
:deep(.shop-overview-page-header) {
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
:deep(.shop-overview-page-header) {
|
||||
flex-direction: column;
|
||||
|
||||
Reference in New Issue
Block a user