feat: 统一工作台与概览页筛选器,商品概览支持按店铺过滤

This commit is contained in:
小蟑螂
2026-05-23 15:18:17 +08:00
parent 6531523c57
commit 34f2f83a5e
11 changed files with 1038 additions and 124 deletions
@@ -8,10 +8,10 @@ import { createIconifyIcon } from '@vben/icons';
import { Button, DatePicker, InputNumber, message, Select, Spin, Tabs } from 'ant-design-vue';
import dayjs from 'dayjs';
import { getActivePlatformsApi } from '#/api/ecommerce/platform/apis';
import { getPlatformPageApi } from '#/api/ecommerce/platform/apis';
import type { PlatformVO } from '#/api/ecommerce/platform/types';
import { getShopPageApi } from '#/api/ecommerce/shop/apis';
import type { ShopVO } from '#/api/ecommerce/shop/types';
import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
import { getWorkbenchSummaryApi } from '#/api/ecommerce/workbench/apis';
import type { WorkbenchDTO, WorkbenchVO } from '#/api/ecommerce/workbench/types';
import { $t } from '#/locales';
@@ -142,42 +142,198 @@ const parseTaxRate = (value: string | undefined) => {
return Number.isFinite(num) ? Math.round((num / 100) * 1e12) / 1e12 : 0;
};
const FILTER_PAGE_SIZE = 20;
const platforms = ref<PlatformVO.Info[]>([]);
const platformLoading = ref(false);
const platformPage = ref(1);
const platformTotal = ref(0);
const platformSearchKeyword = ref('');
const selectedPlatformLabelCache = ref('');
const shops = ref<ShopVO.Info[]>([]);
const metaLoaded = ref(false);
const shopLoading = ref(false);
const shopPage = ref(1);
const shopTotal = ref(0);
const shopSearchKeyword = ref('');
const selectedShopLabelCache = ref('');
const selectedPlatformSlug = ref<string>('all');
const selectedShopSlug = ref<string>('all');
/** 筛选元数据按需加载,不阻塞首屏 summary */
const metaLoaded = ref(true);
const filteredShops = computed(() => {
if (selectedPlatformSlug.value === 'all') {
return shops.value;
}
const pid = Number(selectedPlatformSlug.value);
return shops.value.filter((s) => s.platformId === pid);
});
/** 未选择时表示「全部」 */
const selectedPlatformSlug = ref<string | undefined>(undefined);
const selectedShopSlug = ref<string | undefined>(undefined);
watch(filteredShops, (list) => {
if (selectedShopSlug.value === 'all') return;
const ok = list.some((s) => String(s.id) === selectedShopSlug.value);
if (!ok) selectedShopSlug.value = 'all';
});
let platformSearchTimer: ReturnType<typeof setTimeout> | undefined;
let shopSearchTimer: ReturnType<typeof setTimeout> | undefined;
const platformSelectOptions = computed(() => [
{ label: '全平台', value: 'all' },
...platforms.value.map((p) => ({
function parseSelectedPlatformId(): number | undefined {
const slug = selectedPlatformSlug.value;
if (!slug) return undefined;
const id = Number(slug);
return Number.isFinite(id) && id > 0 ? id : undefined;
}
function parseSelectedShopId(): number | undefined {
const slug = selectedShopSlug.value;
if (!slug) return undefined;
const id = Number(slug);
return Number.isFinite(id) && id > 0 ? id : undefined;
}
const platformSelectOptions = computed(() => {
const opts = platforms.value.map((p) => ({
label: p.platformName,
value: String(p.id),
})),
]);
}));
const slug = selectedPlatformSlug.value;
if (slug && !opts.some((o) => o.value === slug)) {
opts.unshift({
label: selectedPlatformLabelCache.value || `平台 #${slug}`,
value: slug,
});
}
return opts;
});
const shopSelectOptions = computed(() => [
{ label: '全部门店', value: 'all' },
...filteredShops.value.map((s) => ({
const shopSelectOptions = computed(() => {
const opts = shops.value.map((s) => ({
label: s.name,
value: String(s.id),
})),
]);
}));
const slug = selectedShopSlug.value;
if (slug && !opts.some((o) => o.value === slug)) {
opts.unshift({
label: selectedShopLabelCache.value || `店铺 #${slug}`,
value: slug,
});
}
return opts;
});
watch(selectedPlatformSlug, (slug) => {
if (!slug) {
selectedPlatformLabelCache.value = '';
} else {
const hit = platforms.value.find((p) => String(p.id) === slug);
if (hit) selectedPlatformLabelCache.value = hit.platformName;
}
selectedShopSlug.value = undefined;
selectedShopLabelCache.value = '';
shops.value = [];
shopSearchKeyword.value = '';
});
watch(selectedShopSlug, (slug) => {
if (!slug) {
selectedShopLabelCache.value = '';
return;
}
const hit = shops.value.find((s) => String(s.id) === slug);
if (hit) selectedShopLabelCache.value = hit.name;
});
async function fetchPlatformList(page = 1, append = false) {
if (platformLoading.value) return;
platformLoading.value = true;
try {
const keyword = platformSearchKeyword.value.trim();
const data = await getPlatformPageApi({
page,
pageSize: FILTER_PAGE_SIZE,
filters: {
...(keyword ? { platformName: keyword } : {}),
isActive: 1,
},
});
const records = data.records ?? [];
platforms.value = append ? [...platforms.value, ...records] : records;
platformTotal.value = data.total ?? 0;
platformPage.value = page;
} catch {
message.error('平台列表加载失败');
} finally {
platformLoading.value = false;
}
}
async function fetchShopList(page = 1, append = false) {
if (shopLoading.value) return;
shopLoading.value = true;
try {
const keyword = shopSearchKeyword.value.trim();
const filters: ShopDTO.Filters = {};
if (keyword) filters.name = keyword;
const platformId = parseSelectedPlatformId();
if (platformId != null) {
filters.platformId = platformId;
}
const data = await getShopPageApi({
page,
pageSize: FILTER_PAGE_SIZE,
filters,
});
const records = data.records ?? [];
shops.value = append ? [...shops.value, ...records] : records;
shopTotal.value = data.total ?? 0;
shopPage.value = page;
} catch {
message.error('店铺列表加载失败');
} finally {
shopLoading.value = false;
}
}
function handlePlatformSearch(keyword: string) {
if (platformSearchTimer) clearTimeout(platformSearchTimer);
platformSearchTimer = setTimeout(() => {
platformSearchKeyword.value = keyword;
void fetchPlatformList(1, false);
}, 300);
}
function handleShopSearch(keyword: string) {
if (shopSearchTimer) clearTimeout(shopSearchTimer);
shopSearchTimer = setTimeout(() => {
shopSearchKeyword.value = keyword;
void fetchShopList(1, false);
}, 300);
}
function handlePlatformPopupScroll(e: Event) {
const target = e.target as HTMLElement;
const { scrollTop, scrollHeight, clientHeight } = target;
if (scrollHeight - scrollTop - clientHeight < 50) {
const hasMore = platformPage.value * FILTER_PAGE_SIZE < platformTotal.value;
if (hasMore && !platformLoading.value) {
void fetchPlatformList(platformPage.value + 1, true);
}
}
}
function handleShopPopupScroll(e: Event) {
const target = e.target as HTMLElement;
const { scrollTop, scrollHeight, clientHeight } = target;
if (scrollHeight - scrollTop - clientHeight < 50) {
const hasMore = shopPage.value * FILTER_PAGE_SIZE < shopTotal.value;
if (hasMore && !shopLoading.value) {
void fetchShopList(shopPage.value + 1, true);
}
}
}
function handlePlatformDropdownVisibleChange(open: boolean) {
if (!open) return;
platformSearchKeyword.value = '';
void fetchPlatformList(1, false);
}
function handleShopDropdownVisibleChange(open: boolean) {
if (!open) return;
shopSearchKeyword.value = '';
void fetchShopList(1, false);
}
const workbenchData = ref<WorkbenchVO.Info | null>(null);
const workbenchLoading = ref(false);
@@ -202,12 +358,10 @@ const workbenchExportContext = computed<WorkbenchDTO.ExportPayload>(() => ({
dateRange.value[1].format('YYYY-MM-DD'),
],
taxRate: taxRate.value,
...(selectedPlatformSlug.value !== 'all'
? { platformId: Number(selectedPlatformSlug.value) }
: {}),
...(selectedShopSlug.value !== 'all'
? { shopId: Number(selectedShopSlug.value) }
...(parseSelectedPlatformId() != null
? { platformId: parseSelectedPlatformId()! }
: {}),
...(parseSelectedShopId() != null ? { shopId: parseSelectedShopId()! } : {}),
}));
async function fetchWorkbenchSummary() {
@@ -221,11 +375,13 @@ async function fetchWorkbenchSummary() {
],
taxRate: taxRate.value,
};
if (selectedPlatformSlug.value !== 'all') {
query.platformId = Number(selectedPlatformSlug.value);
const platformId = parseSelectedPlatformId();
if (platformId != null) {
query.platformId = platformId;
}
if (selectedShopSlug.value !== 'all') {
query.shopId = Number(selectedShopSlug.value);
const shopId = parseSelectedShopId();
if (shopId != null) {
query.shopId = shopId;
}
workbenchData.value = await getWorkbenchSummaryApi(query);
} catch (e: unknown) {
@@ -239,20 +395,6 @@ async function fetchWorkbenchSummary() {
}
}
async function loadShopAndPlatformMeta() {
try {
const [platList, shopRes] = await Promise.all([
getActivePlatformsApi(),
getShopPageApi({ page: 1, pageSize: 500, filters: {} }),
]);
platforms.value = platList ?? [];
shops.value = shopRes?.records ?? [];
} catch {
message.error('店铺或平台列表加载失败');
}
metaLoaded.value = true;
}
const activeTab = ref('finance');
function handleShopNavigate(shopId: number) {
@@ -263,9 +405,15 @@ function handleShopNavigate(shopId: number) {
}
function handleProductNavigate(productId: number) {
const shopId = parseSelectedShopId();
router.push({
path: `/ecom/product/overview/${productId}`,
query: buildOverviewRouteQuery(dateRange.value),
query: buildOverviewRouteQuery(
dateRange.value,
shopId != null && Number.isFinite(shopId) && shopId > 0
? { shopId }
: undefined,
),
});
}
@@ -323,7 +471,6 @@ watch(
let darkObserver: MutationObserver | null = null;
onMounted(async () => {
await loadShopAndPlatformMeta();
await fetchWorkbenchSummary();
setTimeout(() => {
void redrawActiveTabCharts();
@@ -338,6 +485,8 @@ onMounted(async () => {
});
onUnmounted(() => {
if (platformSearchTimer) clearTimeout(platformSearchTimer);
if (shopSearchTimer) clearTimeout(shopSearchTimer);
darkObserver?.disconnect();
darkObserver = null;
});
@@ -403,7 +552,7 @@ onUnmounted(() => {
class="workbench-filter-platform-shop w-full min-w-0 rounded-2xl border border-slate-200 bg-slate-200/50 p-1.5 sm:w-auto sm:max-w-[min(calc(100vw-2.5rem),56rem)] dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(240deg_3.7%_15.9%/30%)]"
>
<div
class="flex min-w-0 flex-col gap-2 rounded-xl bg-white/30 p-1 sm:flex-row sm:items-stretch sm:gap-0 sm:divide-x sm:divide-slate-300/50 dark:bg-[hsl(240deg_3.7%_22%/30%)] dark:sm:divide-[hsl(240deg_3.7%_35%)]"
class="flex min-w-0 flex-col gap-2 rounded-xl bg-white/30 p-1 sm:flex-row sm:items-center sm:gap-0 sm:divide-x sm:divide-slate-300/50 dark:bg-[hsl(240deg_3.7%_22%/30%)] dark:sm:divide-[hsl(240deg_3.7%_35%)]"
>
<div
class="flex min-w-0 items-center gap-2 px-2 py-0.5 sm:w-[11.5rem] sm:flex-none sm:shrink-0 sm:py-0"
@@ -416,10 +565,27 @@ onUnmounted(() => {
<Select
v-model:value="selectedPlatformSlug"
size="small"
show-search
allow-clear
:filter-option="false"
:loading="platformLoading"
:options="platformSelectOptions"
placeholder="选择平台"
placeholder="全部平台"
class="workbench-header-select min-w-0 flex-1"
/>
@search="handlePlatformSearch"
@popup-scroll="handlePlatformPopupScroll"
@dropdown-visible-change="handlePlatformDropdownVisibleChange"
>
<template #notFoundContent>
<div v-if="platformLoading" class="py-3 text-center">
<Spin size="small" />
<div class="mt-1 text-xs text-slate-400">加载中...</div>
</div>
<div v-else class="py-3 text-center text-xs text-slate-400">
暂无平台
</div>
</template>
</Select>
</div>
<div
class="flex min-w-0 flex-1 items-center gap-2 border-t border-slate-200/80 px-2 pt-2 sm:min-w-[16rem] sm:border-t-0 sm:py-0 sm:pt-0 dark:border-[hsl(240deg_3.7%_22%)]"
@@ -432,11 +598,28 @@ onUnmounted(() => {
<Select
v-model:value="selectedShopSlug"
size="small"
show-search
allow-clear
:filter-option="false"
:loading="shopLoading"
:options="shopSelectOptions"
placeholder="选择店铺"
placeholder="全部店铺"
:dropdown-match-select-width="false"
class="workbench-header-select workbench-shop-select min-w-0 flex-1"
/>
@search="handleShopSearch"
@popup-scroll="handleShopPopupScroll"
@dropdown-visible-change="handleShopDropdownVisibleChange"
>
<template #notFoundContent>
<div v-if="shopLoading" class="py-3 text-center">
<Spin size="small" />
<div class="mt-1 text-xs text-slate-400">加载中...</div>
</div>
<div v-else class="py-3 text-center text-xs text-slate-400">
暂无店铺
</div>
</template>
</Select>
</div>
</div>
</div>
@@ -566,16 +749,8 @@ onUnmounted(() => {
ref="periodCompareTabRef"
:active-tab="activeTab"
:tax-rate="taxRate"
:platform-id="
selectedPlatformSlug !== 'all'
? Number(selectedPlatformSlug)
: undefined
"
:shop-id="
selectedShopSlug !== 'all'
? Number(selectedShopSlug)
: undefined
"
:platform-id="parseSelectedPlatformId()"
:shop-id="parseSelectedShopId()"
/>
</Tabs.TabPane>
</Tabs>
@@ -645,6 +820,7 @@ onUnmounted(() => {
min-height: 2.125rem !important;
display: flex !important;
align-items: center !important;
padding-inline: 10px 28px !important;
background-color: rgb(255 255 255 / 0.92) !important;
}
@@ -662,7 +838,37 @@ onUnmounted(() => {
color: rgb(15 23 42) !important;
}
/* 搜索态:输入框与光标垂直居中(平台/店铺共用) */
:deep(.workbench-header-select .ant-select-selection-wrap) {
position: relative !important;
display: flex !important;
align-items: center !important;
flex: 1 1 auto !important;
min-width: 0 !important;
min-height: 1.25rem;
align-self: center !important;
}
:deep(.workbench-header-select .ant-select-selection-search) {
position: absolute !important;
inset-inline-start: 10px !important;
inset-inline-end: 28px !important;
top: 50% !important;
bottom: auto !important;
transform: translateY(-50%) !important;
display: flex !important;
align-items: center !important;
height: 1.25rem !important;
margin-inline-start: 0 !important;
}
:deep(.workbench-header-select .ant-select-selection-search-input) {
height: 1.25rem !important;
line-height: 1.25rem !important;
padding: 0 !important;
margin: 0 !important;
vertical-align: middle !important;
}
/* 店铺:在通用样式之后覆盖,选中项可换行以显示完整店名 */
:deep(.workbench-header-select.workbench-shop-select.ant-select) {
@@ -674,9 +880,15 @@ onUnmounted(() => {
) {
height: auto !important;
min-height: 2.125rem !important;
align-items: flex-start !important;
padding-top: 6px !important;
padding-bottom: 6px !important;
align-items: center !important;
padding-block: 6px !important;
padding-inline: 10px 28px !important;
}
:deep(.workbench-header-select.workbench-shop-select .ant-select-arrow) {
top: 50% !important;
margin-top: 0 !important;
transform: translateY(-50%) !important;
}
:deep(
@@ -694,6 +906,7 @@ onUnmounted(() => {
overflow: visible !important;
text-overflow: clip !important;
line-height: 1.35 !important;
align-self: center !important;
}
:deep(.tax-rate-input .ant-input-number-input) {