feat(frontend): 店铺概览页添加导出按钮

- 新增 exportByPost 通用导出工具(处理 blob 下载)
- 店铺概览页导出按钮绑定事件,支持当前页面日期范围导出
- shop/apis.ts 新增导出 API 函数
This commit is contained in:
小蟑螂
2026-03-26 10:05:13 +08:00
parent 8867903e7b
commit f172c12e4f
7 changed files with 220 additions and 1 deletions
@@ -80,3 +80,29 @@ export async function getShopOverviewStatisticsApi(
params,
);
}
/**
* 导出店铺概览Excel
*/
export async function exportShopOverviewApi(params: {
shopId: number;
dateRange: string[];
exportType?: string;
}) {
return requestClient.post('/ec/shop/export-overview', params, {
responseType: 'blob',
});
}
/**
* 导出公司概览Excel
*/
export async function exportCompanyOverviewApi(params: {
dateRange: string[];
shopIds?: number[];
exportType?: string;
}) {
return requestClient.post('/ec/company-overview/export', params, {
responseType: 'blob',
});
}
@@ -0,0 +1,72 @@
/**
* 通用文件导出工具
* 用于下载 Excel、PDF 等二进制文件
*/
import { useAccessStore } from '@vben/stores';
import { message } from 'ant-design-vue';
import { useAppConfig } from '@vben/hooks';
const { apiURL } = useAppConfig(import.meta.env, import.meta.env.PROD);
function formatToken(token: null | string) {
return token ? `Bearer ${token}` : null;
}
export async function exportByPost(
url: string,
params: Record<string, unknown>,
filename: string,
) {
const accessStore = useAccessStore();
const fullUrl = `${apiURL}${url}`;
try {
const response = await fetch(fullUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: formatToken(accessStore.accessToken) || '',
},
body: JSON.stringify(params),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('Content-Type') || '';
const blob = await response.blob();
if (blob.size === 0) {
throw new Error('下载文件为空');
}
// 如果返回的是 JSON 错误(而非文件),提示错误信息
if (contentType.includes('application/json')) {
const text = await blob.text();
try {
const json = JSON.parse(text);
message.error(json.message || '导出失败');
} catch {
message.error('导出失败');
}
return;
}
// 触发浏览器下载
const downloadUrl = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = downloadUrl;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
message.success('导出成功');
} catch (error) {
console.error('导出失败:', error);
message.error('导出失败,请稍后重试');
}
}
@@ -12,6 +12,7 @@ import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
import { Button, DatePicker, message, Spin } from 'ant-design-vue';
import dayjs from 'dayjs';
import { exportByPost } from '#/utils/export';
import { getShopOverviewStatisticsApi } from '#/api/ecommerce/shop';
defineOptions({ name: 'ShopOverview' });
@@ -27,6 +28,7 @@ const shopId = computed(() => {
// 响应式数据
const loading = ref(false);
const exportLoading = ref(false);
const overviewData = ref<null | ShopVO.OverviewStatistics>(null);
// 日期范围
@@ -222,6 +224,23 @@ const loadOverviewData = async () => {
}
};
// 导出店铺概览
const handleExportShopOverview = async () => {
if (!shopId.value) return;
try {
exportLoading.value = true;
const shopName = overviewData.value?.shopName || `店铺${shopId.value}`;
const [startDate, endDate] = dateRange.value;
const filename = `${shopName}_概览_${startDate.format('YYYYMMDD')}_${endDate.format('YYYYMMDD')}.xlsx`;
await exportByPost('/ec/shop/export-overview', {
shopId: shopId.value,
dateRange: [startDate.format('YYYY-MM-DD'), endDate.format('YYYY-MM-DD')],
}, filename);
} finally {
exportLoading.value = false;
}
};
const refreshOverviewByCurrentParams = async (force = false) => {
applyDateRangeFromRouteQuery();
if (!shopId.value) return;
@@ -716,8 +735,10 @@ onActivated(async () => {
<Button
type="primary"
class="!h-auto !rounded-2xl !border-none !bg-slate-900 !px-8 !py-3 !text-sm !font-black !shadow-xl !shadow-slate-200 !transition-all hover:!scale-105 active:!scale-95"
:loading="exportLoading"
@click="handleExportShopOverview"
>
导出商品明细报表
导出店铺概览
</Button>
</div>
View File
View File