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
+58
View File
@@ -0,0 +1,58 @@
# ecom-dmb 完善计划
> 创建于 2026-03-26
> 顺序按「先理解架构 → 前端安全 → 后端安全 → 基础设施」排列
---
## 阶段一:理解前端架构(避免样式错乱的第一步)
- [x] 1.1 读懂 frontend 技术栈(Vue3/Vben Admin 5.5.9、Ant Design Vue、Tailwind CSS、ECharts、dayjs)
- [x] 1.2 梳理现有页面的目录结构和组件复用方式
- [x] 1.3 确认主题/样式方案:统一用 Tailwind 类名 + scoped style,无混用
- [x] 1.4 发现店铺概览页已有「导出商品明细报表」按钮,只缺 API 对接
---
## 阶段二:前端安全与功能完善(用户最担心的部分)
- [ ] 2.1 对接公司概览导出 API(前端加导出按钮 + 下载逻辑)
- [ ] 2.2 对接店铺概览导出 API(前端加导出按钮 + 下载逻辑)
- [ ] 2.3 确保新加的导出 UI 样式和现有页面一致(避免样式错乱)
- [ ] 2.4 检查前端所有表单/表格是否有 companyId 归属校验(接口调用前)
- [ ] 2.5 前后端联调测试导出功能
---
## 阶段三:后端安全加固
- [ ] 3.1 全局拦截器:所有涉及多店铺数据的 API,强校验 companyId 归属
- [ ] 3.2 重点检查:订单、推广费、刷单、商品、店铺接口
- [ ] 3.3 移除或加密硬编码 JWT secret,改用环境变量
- [ ] 3.4 后端接口联调测试
---
## 阶段四:功能补充
- [ ] 4.1 公司概览 Dashboard 页面(KPI 卡片:销售额/单量/ROI/环比增长)
- [ ] 4.2 操作日志表 + AOP 记录(谁在什么时间改了啥)
- [ ] 4.3 店铺月目标管理页面完善(前端入口 + CRUD)
- [ ] 4.4 前端各模块入口检查(config、order-status 等是否有遗漏)
---
## 阶段五:基础设施
- [ ] 5.1 Redis 超时配置修复(timeout vs leftPop 等待时间对齐)
- [ ] 5.2 后端 README 完善(启动方式、环境变量、依赖说明)
- [ ] 5.3 前端 README 完善
- [ ] 5.4 制定 MySQL 数据备份策略
- [ ] 5.5 后端单元测试(重点:盈亏/ROI/税费计算逻辑)
---
## 完成记录
| 任务 | 完成时间 | 备注 |
|------|----------|------|
@@ -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
+42
View File
@@ -0,0 +1,42 @@
# EXAMPLE USAGE:
#
# Refer for explanation to following link:
# https://lefthook.dev/configuration/
#
# pre-push:
# jobs:
# - name: packages audit
# tags:
# - frontend
# - security
# run: yarn audit
#
# - name: gems audit
# tags:
# - backend
# - security
# run: bundle audit
#
# pre-commit:
# parallel: true
# jobs:
# - run: yarn eslint {staged_files}
# glob: "*.{js,ts,jsx,tsx}"
#
# - name: rubocop
# glob: "*.rb"
# exclude:
# - config/application.rb
# - config/routes.rb
# run: bundle exec rubocop --force-exclusion {all_files}
#
# - name: govet
# files: git ls-files -m
# glob: "*.go"
# run: go vet {files}
#
# - script: "hello.js"
# runner: node
#
# - script: "hello.go"
# runner: go run