fix(frontend): 修复 keep-alive 场景下路由参数串用导致的接口错乱

- 商品/店铺概览页增加 isProductOverviewRoute / isShopOverviewRoute 路由路径校验
- 防止 keep-alive 切换标签时 route.params.id 被其他页复用导致错误请求
- 全店概览页 Page 标题改为 template slot 方式,移除冗余的店铺数量拉取逻辑
- 同步优化各概览页 header 样式对齐
This commit is contained in:
小蟑螂
2026-05-06 01:50:41 +08:00
parent 5445dc5a22
commit 655a1025b3
3 changed files with 59 additions and 37 deletions
@@ -20,7 +20,6 @@ import { Button, DatePicker, InputNumber, Spin } from 'ant-design-vue';
import dayjs from 'dayjs';
import { getCompanyOverviewApi } from '#/api/ecommerce/store-overview/apis';
import { getShopPageApi } from '#/api/ecommerce/shop';
import { exportByPost } from '#/utils/export';
import ComparisonSwitcher from './components/ComparisonSwitcher.vue';
@@ -36,7 +35,6 @@ const router = useRouter();
const loading = ref(false);
const exportLoading = ref(false);
const overviewData = ref<CompanyOverviewVO.Info | null>(null);
const shopCount = ref(0);
// 指定店铺导出模式
const shopExportMode = ref(false);
@@ -275,20 +273,6 @@ const syncDateRangeToRouteQuery = async () => {
window.history.replaceState({}, '', newUrl.toString());
};
// 加载店铺数量
const loadShopCount = async () => {
try {
const response = await getShopPageApi({
page: 1,
pageSize: 1,
filters: {},
});
shopCount.value = response.total || 0;
} catch (error) {
console.error('获取店铺数量失败:', error);
}
};
// 加载概览数据
const loadOverviewData = async () => {
if (isRequesting.value) return;
@@ -342,7 +326,6 @@ watch(
let darkThemeObserver: MutationObserver | null = null;
onMounted(async () => {
await loadShopCount();
await refreshOverviewByCurrentParams(true);
darkThemeObserver = new MutationObserver(() => {
@@ -467,14 +450,15 @@ const navigateToShopOverview = (shopId: number) => {
<template>
<Page
title="全店铺经营概览大盘"
auto-content-height
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
header-class="store-overview-page-header"
>
<template #description>
<div class="mt-1 text-xs font-bold leading-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] sm:text-sm">
数据范围:公司旗下所有店铺汇总统计 · 共 {{ shopCount }} 家在营店铺
<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"
>
全店铺经营概览大盘
</div>
</template>
@@ -641,6 +625,10 @@ const navigateToShopOverview = (shopId: number) => {
padding-bottom: 0;
}
:deep(.store-overview-page-header) {
align-items: center;
}
@media (max-width: 640px) {
:deep(.store-overview-page-header) {
flex-direction: column;
@@ -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;