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 dayjs from 'dayjs';
import { getCompanyOverviewApi } from '#/api/ecommerce/store-overview/apis'; import { getCompanyOverviewApi } from '#/api/ecommerce/store-overview/apis';
import { getShopPageApi } from '#/api/ecommerce/shop';
import { exportByPost } from '#/utils/export'; import { exportByPost } from '#/utils/export';
import ComparisonSwitcher from './components/ComparisonSwitcher.vue'; import ComparisonSwitcher from './components/ComparisonSwitcher.vue';
@@ -36,7 +35,6 @@ const router = useRouter();
const loading = ref(false); const loading = ref(false);
const exportLoading = ref(false); const exportLoading = ref(false);
const overviewData = ref<CompanyOverviewVO.Info | null>(null); const overviewData = ref<CompanyOverviewVO.Info | null>(null);
const shopCount = ref(0);
// 指定店铺导出模式 // 指定店铺导出模式
const shopExportMode = ref(false); const shopExportMode = ref(false);
@@ -275,20 +273,6 @@ const syncDateRangeToRouteQuery = async () => {
window.history.replaceState({}, '', newUrl.toString()); 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 () => { const loadOverviewData = async () => {
if (isRequesting.value) return; if (isRequesting.value) return;
@@ -342,7 +326,6 @@ watch(
let darkThemeObserver: MutationObserver | null = null; let darkThemeObserver: MutationObserver | null = null;
onMounted(async () => { onMounted(async () => {
await loadShopCount();
await refreshOverviewByCurrentParams(true); await refreshOverviewByCurrentParams(true);
darkThemeObserver = new MutationObserver(() => { darkThemeObserver = new MutationObserver(() => {
@@ -467,14 +450,15 @@ const navigateToShopOverview = (shopId: number) => {
<template> <template>
<Page <Page
title="全店铺经营概览大盘"
auto-content-height auto-content-height
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]" class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
header-class="store-overview-page-header" header-class="store-overview-page-header"
> >
<template #description> <template #title>
<div class="mt-1 text-xs font-bold leading-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] sm:text-sm"> <div
数据范围:公司旗下所有店铺汇总统计 · 共 {{ shopCount }} 家在营店铺 class="text-xl font-black tracking-tight text-slate-900 dark:text-[hsl(0deg_0%_98%)] sm:text-2xl md:text-3xl"
>
全店铺经营概览大盘
</div> </div>
</template> </template>
@@ -641,6 +625,10 @@ const navigateToShopOverview = (shopId: number) => {
padding-bottom: 0; padding-bottom: 0;
} }
:deep(.store-overview-page-header) {
align-items: center;
}
@media (max-width: 640px) { @media (max-width: 640px) {
:deep(.store-overview-page-header) { :deep(.store-overview-page-header) {
flex-direction: column; flex-direction: column;
@@ -20,8 +20,13 @@ defineOptions({ name: 'ProductOverview' });
const route = useRoute(); const route = useRoute();
/** 当前是否为商品概览路由(keep-alive 下勿用其它页的 params.id 请求商品接口) */
const isProductOverviewRoute = () =>
/\/ecom\/product\/overview\/[^/]+\/?$/.test(route.path);
// 商品ID(从路由参数获取) // 商品ID(从路由参数获取)
const productId = computed(() => { const productId = computed(() => {
if (!isProductOverviewRoute()) return 0;
const id = route.params.id; const id = route.params.id;
const parsed = id ? Number(id) : 0; const parsed = id ? Number(id) : 0;
return Number.isFinite(parsed) && parsed > 0 ? parsed : 0; return Number.isFinite(parsed) && parsed > 0 ? parsed : 0;
@@ -101,7 +106,7 @@ const handleTimeRangeChange = async (val: typeof timeRangeState.value) => {
// 获取概览数据 // 获取概览数据
async function fetchOverview() { async function fetchOverview() {
if (!productId.value) return; if (!isProductOverviewRoute() || !productId.value) return;
loading.value = true; loading.value = true;
try { try {
@@ -119,10 +124,15 @@ async function fetchOverview() {
} }
} }
// 初始加载 // 初始加载(依赖 route.path,避免 keep-alive 下切到店铺页仍用店铺 id 请求商品)
watch(productId, (val) => { watch(
if (val) fetchOverview(); () => [route.path, productId.value] as const,
}, { immediate: true }); ([path, id]) => {
if (!/\/ecom\/product\/overview\/[^/]+\/?$/.test(path) || !id) return;
fetchOverview();
},
{ immediate: true },
);
// 计算 page title 用 productName // 计算 page title 用 productName
const pageTitle = computed(() => overviewData.value?.productName || '商品概览'); const pageTitle = computed(() => overviewData.value?.productName || '商品概览');
@@ -130,14 +140,15 @@ const pageTitle = computed(() => overviewData.value?.productName || '商品概
<template> <template>
<Page <Page
:title="pageTitle"
auto-content-height auto-content-height
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]" class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
header-class="product-overview-page-header" header-class="product-overview-page-header"
> >
<template #description> <template #title>
<div class="mt-1 text-xs font-bold italic leading-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] sm:text-sm"> <div
数据更新于: {{ dayjs().format('YYYY-MM-DD HH:mm:ss') }} class="text-xl font-black tracking-tight text-slate-900 dark:text-[hsl(0deg_0%_98%)] sm:text-2xl md:text-3xl"
>
{{ pageTitle }}
</div> </div>
</template> </template>
@@ -177,6 +188,10 @@ const pageTitle = computed(() => overviewData.value?.productName || '商品概
</template> </template>
<style scoped> <style scoped>
:deep(.product-overview-page-header) {
align-items: center;
}
/* 表格滚动条优化 */ /* 表格滚动条优化 */
.table-container::-webkit-scrollbar { .table-container::-webkit-scrollbar {
height: 6px; height: 6px;
@@ -28,8 +28,13 @@ defineOptions({ name: 'ShopOverview' });
const route = useRoute(); const route = useRoute();
/** 当前是否为店铺概览路由(keep-alive 下 route.params.id 会随其它标签变化,不可仅凭 id 请求) */
const isShopOverviewRoute = () =>
/\/ecom\/shop\/overview\/[^/]+\/?$/.test(route.path);
// 店铺ID(从路由参数获取) // 店铺ID(从路由参数获取)
const shopId = computed(() => { const shopId = computed(() => {
if (!isShopOverviewRoute()) return 0;
const id = route.params.id; const id = route.params.id;
return id ? Number(id) : 0; return id ? Number(id) : 0;
}); });
@@ -187,7 +192,7 @@ const syncDateRangeToRouteQuery = async () => {
// 加载概览数据 // 加载概览数据
const loadOverviewData = async () => { const loadOverviewData = async () => {
if (!shopId.value) return; if (!isShopOverviewRoute() || !shopId.value) return;
if (isRequesting.value) return; if (isRequesting.value) return;
isRequesting.value = true; isRequesting.value = true;
try { try {
@@ -281,6 +286,7 @@ const handleCategorySelectAllChange = (event: Event) => {
}; };
const refreshOverviewByCurrentParams = async (force = false) => { const refreshOverviewByCurrentParams = async (force = false) => {
if (!isShopOverviewRoute()) return;
applyDateRangeFromRouteQuery(); applyDateRangeFromRouteQuery();
if (!shopId.value) return; if (!shopId.value) return;
@@ -293,34 +299,43 @@ const refreshOverviewByCurrentParams = async (force = false) => {
await loadOverviewData(); await loadOverviewData();
}; };
// 监听路由变化 // 监听路由变化(仅在本页路径下响应,避免切到商品概览等仍用当前 id 请求店铺接口)
watch( watch(
() => [route.params.id, route.query.startDate, route.query.endDate], () => [
route.path,
route.params.id,
route.query.startDate,
route.query.endDate,
],
async () => { async () => {
if (!isShopOverviewRoute()) return;
await refreshOverviewByCurrentParams(); await refreshOverviewByCurrentParams();
}, },
); );
// 初始化 // 初始化
onMounted(async () => { onMounted(async () => {
if (!isShopOverviewRoute()) return;
await refreshOverviewByCurrentParams(true); await refreshOverviewByCurrentParams(true);
}); });
onActivated(async () => { onActivated(async () => {
if (!isShopOverviewRoute()) return;
await refreshOverviewByCurrentParams(true); await refreshOverviewByCurrentParams(true);
}); });
</script> </script>
<template> <template>
<Page <Page
:title="overviewData?.shopName ? `店铺经营概览(${overviewData.shopName})` : '店铺经营概览'"
auto-content-height auto-content-height
class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]" class="bg-[#f8fafc] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
header-class="shop-overview-page-header" header-class="shop-overview-page-header"
> >
<template #description> <template #title>
<div class="mt-1 text-xs font-bold italic leading-5 text-slate-500 dark:text-[hsl(240deg_5%_64.9%)] sm:text-sm"> <div
数据更新于:{{ dayjs().format('今日 HH:mm:ss') }} 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> </div>
</template> </template>
@@ -364,6 +379,10 @@ onActivated(async () => {
</template> </template>
<style scoped> <style scoped>
:deep(.shop-overview-page-header) {
align-items: center;
}
@media (max-width: 640px) { @media (max-width: 640px) {
:deep(.shop-overview-page-header) { :deep(.shop-overview-page-header) {
flex-direction: column; flex-direction: column;