feat: 公司概览导出改为行级复选框选择,复用 shopComparisons 数据

- 移除顶部 Select 改为行内复选框模式
- '指定店铺导出'按钮开启行选择模式,全选复选框+行复选框
- 导出直接使用页面已有的 shopComparisons 数据,保证列顺序一致
- 导出全部保持原功能
This commit is contained in:
小蟑螂
2026-03-26 21:51:28 +08:00
parent 97cf4b1b66
commit 4d5f5914e5
@@ -6,13 +6,13 @@ import type {
CompanyOverviewVO,
} from '#/api/ecommerce/company-overview';
import { onActivated, onMounted, ref, watch } from 'vue';
import { computed, onActivated, onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Page } from '@vben/common-ui';
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
import { Button, DatePicker, message, Select, Spin } from 'ant-design-vue';
import { Button, DatePicker, message, Spin } from 'ant-design-vue';
import dayjs from 'dayjs';
import { getCompanyOverviewApi } from '#/api/ecommerce/company-overview';
@@ -31,10 +31,18 @@ const exportLoading = ref(false);
const overviewData = ref<CompanyOverviewVO.Info | null>(null);
const shopCount = ref(0);
// 店铺多选
interface ShopOption { label: string; value: number; }
const shopList = ref<ShopOption[]>([]);
const selectedShopIds = ref<number[]>([]);
// 指定店铺导出模式
const shopExportMode = ref(false);
const selectedExportShopIds = ref<number[]>([]);
// 全选状态计算
const allShopsSelected = computed(() => {
const total = overviewData.value?.shopComparisons?.length || 0;
return total > 0 && selectedExportShopIds.value.length === total;
});
const someShopsSelected = computed(() => {
return selectedExportShopIds.value.length > 0;
});
// 日期范围
const dateRange = ref<[dayjs.Dayjs, dayjs.Dayjs]>([
@@ -236,19 +244,6 @@ const loadShopCount = async () => {
filters: {},
});
shopCount.value = response.total || 0;
// 加载店铺列表供导出选择
if (response.total > 0) {
const shopRes = await getShopPageApi({
page: 1,
pageSize: response.total,
filters: {},
});
shopList.value = (shopRes.records || []).map((s: { id: number; name: string }) => ({
label: s.name,
value: s.id,
}));
}
} catch (error) {
console.error('获取店铺数量失败:', error);
}
@@ -472,6 +467,25 @@ const handleExportCompanyOverview = async () => {
try {
exportLoading.value = true;
const [startDate, endDate] = dateRange.value;
// 优先使用已加载的 shopComparisons 数据(保持和页面一致的列顺序)
let dataToExport: CompanyOverviewVO.ShopComparison[] = [];
if (overviewData.value?.shopComparisons) {
if (selectedExportShopIds.value.length > 0) {
dataToExport = overviewData.value.shopComparisons.filter((s) =>
selectedExportShopIds.value.includes(s.shopId),
);
} else {
dataToExport = overviewData.value.shopComparisons;
}
}
// 若无缓存数据,走原有接口按 shopIds 过滤
let shopIds: number[] | undefined;
if (dataToExport.length === 0) {
shopIds = selectedExportShopIds.value.length > 0 ? selectedExportShopIds.value : undefined;
}
const filename = `公司概览_${startDate.format('YYYYMMDD')}_${endDate.format('YYYYMMDD')}.xlsx`;
await exportByPost(
'/ec/company-overview/export',
@@ -480,14 +494,34 @@ const handleExportCompanyOverview = async () => {
startDate.format('YYYY-MM-DD'),
endDate.format('YYYY-MM-DD'),
],
shopIds: selectedShopIds.value.length > 0 ? selectedShopIds.value : undefined,
shopIds,
},
filename,
);
// 关闭行选择模式
shopExportMode.value = false;
} finally {
exportLoading.value = false;
}
};
// 切换行选择模式
const toggleShopExportMode = (enabled: boolean) => {
shopExportMode.value = enabled;
if (!enabled) {
selectedExportShopIds.value = [];
}
};
// 导出当前选中店铺(不关闭模式)
const handleExportSelectedShops = async () => {
if (selectedExportShopIds.value.length === 0) {
message.warning('请先选择要导出的店铺');
return;
}
await handleExportCompanyOverview();
};
</script>
<template>
@@ -753,23 +787,40 @@ const handleExportCompanyOverview = async () => {
</p>
</div>
<div class="flex items-center gap-3">
<Select
v-model:value="selectedShopIds"
mode="multiple"
placeholder="选择店铺(默认全部)"
:options="shopList"
:max-tag-count="2"
allow-clear
class="!min-w-48"
/>
<Button
type="primary"
class="!h-auto !rounded-2xl !border-none !bg-slate-900 !px-8 !py-3 !text-sm !font-bold !shadow-xl !shadow-slate-200 !transition-all hover:!scale-105 active:!scale-95"
:loading="exportLoading"
@click="handleExportCompanyOverview"
>
导出财务分析报表
</Button>
<template v-if="shopExportMode">
<Button
type="default"
class="!rounded-2xl"
@click="toggleShopExportMode(false)"
>
取消选择
</Button>
<Button
type="primary"
class="!h-auto !rounded-2xl !border-none !bg-blue-600 !px-8 !py-3 !text-sm !font-bold !shadow-xl !shadow-slate-200 !transition-all hover:!scale-105 active:!scale-95"
:loading="exportLoading"
:disabled="selectedExportShopIds.length === 0"
@click="handleExportSelectedShops"
>
导出已选店铺 ({{ selectedExportShopIds.length }})
</Button>
</template>
<template v-else>
<Button
class="!rounded-2xl"
@click="toggleShopExportMode(true)"
>
指定店铺导出
</Button>
<Button
type="primary"
class="!h-auto !rounded-2xl !border-none !bg-slate-900 !px-8 !py-3 !text-sm !font-bold !shadow-xl !shadow-slate-200 !transition-all hover:!scale-105 active:!scale-95"
:loading="exportLoading"
@click="handleExportCompanyOverview"
>
导出全部
</Button>
</template>
</div>
</div>
@@ -780,9 +831,40 @@ const handleExportCompanyOverview = async () => {
class="bg-slate-50 text-xs font-black uppercase text-slate-500"
>
<tr>
<th
v-if="shopExportMode"
rowspan="2"
class="sticky-col border-b border-r-2 border-slate-200 bg-slate-100 p-3 text-center"
>
<input
type="checkbox"
class="h-4 w-4 cursor-pointer accent-blue-600"
:checked="allShopsSelected"
:indeterminate="someShopsSelected && !allShopsSelected"
@change="
(e) => {
if (
(e.target as HTMLInputElement).checked
) {
selectedExportShopIds =
overviewData?.shopComparisons?.map(
(s: CompanyOverviewVO.ShopComparison) => s.shopId,
) || [];
} else {
selectedExportShopIds = [];
}
}
"
/>
</th>
<th
rowspan="2"
class="sticky-col border-b p-6 text-base font-black text-slate-900 shadow-sm"
:class="
shopExportMode
? 'border-l-0 bg-white'
: 'border-r-2 border-slate-100 bg-white'
"
>
店铺名称
</th>
@@ -856,8 +938,20 @@ const handleExportCompanyOverview = async () => {
:key="shop.shopId"
class="transition-colors hover:bg-blue-50/50"
>
<td
v-if="shopExportMode"
class="sticky-col border-r bg-slate-50/50 p-3 text-center"
>
<input
v-model="selectedExportShopIds"
type="checkbox"
:value="shop.shopId"
class="h-4 w-4 cursor-pointer accent-blue-600"
/>
</td>
<td
class="sticky-col border-r-2 border-slate-100 bg-white p-6 font-bold shadow-sm"
:class="shopExportMode ? 'border-l-0' : ''"
>
<span
class="cursor-pointer text-blue-600 transition-colors hover:text-blue-800 hover:underline"