This commit is contained in:
ah--desktop
2025-12-11 08:04:56 +08:00
parent a3b8797955
commit 5129657f7f
69 changed files with 1695 additions and 545 deletions
@@ -0,0 +1,367 @@
<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
import type { PageQueryDTO } from '#/types/common';
import { computed, onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
import { Page } from '@vben/common-ui';
import {
Card,
Col,
DatePicker,
Image,
message,
Row,
Space,
Statistic,
} from 'ant-design-vue';
import dayjs from 'dayjs';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
getShopProductPageApi,
getShopStatisticsApi,
} from '#/api/ecommerce/shop';
defineOptions({ name: 'ShopDetails' });
const route = useRoute();
// 店铺ID(从路由参数获取)
const shopId = computed(() => {
const id = route.params.id;
return id ? Number(id) : 0;
});
// 响应式数据
const loading = ref(false);
const tableLoading = ref(false);
const customStats = ref<null | ShopVO.Statistics>(null);
const dateRange = ref<[dayjs.Dayjs, dayjs.Dayjs]>([
dayjs().startOf('month'),
dayjs(),
]);
// 格式化金额
function formatCurrency(value: number): string {
return `¥${value.toLocaleString('zh-CN', {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
})}`;
}
// 格式化百分比
function formatPercent(value: number): string {
return `${(value * 100).toFixed(2)}%`;
}
// 获取自定义日期范围统计信息
async function fetchCustomStatistics() {
if (!shopId.value || !dateRange.value) return;
try {
loading.value = true;
const params: ShopDTO.StatisticsQuery = {
shopId: shopId.value,
startDate: dateRange.value[0].format('YYYY-MM-DD'),
endDate: dateRange.value[1].format('YYYY-MM-DD'),
};
const data = await getShopStatisticsApi(params);
customStats.value = data;
} catch (error) {
console.error('获取统计信息失败:', error);
message.error('获取统计信息失败');
} finally {
loading.value = false;
}
}
// 处理日期范围变化
function handleDateRangeChange() {
fetchCustomStatistics();
}
// 店铺ID的稳定引用,避免响应式循环
const stableShopId = computed(() => {
const id = route.params.id;
return id ? Number(id) : 0;
});
// VbenVxeGrid 表格配置 - 商品列表
const gridOptions: VxeGridProps<ShopVO.ProductDetail> = {
columns: [
{
type: 'seq' as const,
width: 80,
title: '序号',
},
{
title: '商品主图',
width: 100,
align: 'center' as const,
slots: { default: 'mainImage' },
},
{
field: 'productName',
title: '商品名称',
minWidth: 200,
showOverflow: 'tooltip' as const,
},
{
field: 'productCode',
title: '商品编码',
minWidth: 150,
showOverflow: 'tooltip' as const,
},
{
field: 'salesAmount',
title: '销售额',
width: 150,
align: 'right' as const,
formatter: ({ cellValue }) => formatCurrency(cellValue || 0),
},
{
field: 'orderCount',
title: '订单数',
width: 120,
align: 'right' as const,
},
{
field: 'averageOrderValue',
title: '客单价',
width: 150,
align: 'right' as const,
formatter: ({ cellValue }) => formatCurrency(cellValue || 0),
},
{
field: 'createTime',
title: '创建时间',
width: 180,
},
],
height: 'auto',
border: true,
stripe: true,
rowConfig: {
isHover: true,
},
showOverflow: true,
size: 'small',
toolbarConfig: {
custom: true,
refresh: true,
zoom: true,
},
pagerConfig: {
enabled: true,
pageSizes: [10, 20, 50, 100],
layouts: [
'PrevJump',
'PrevPage',
'Number',
'NextPage',
'NextJump',
'Sizes',
'FullJump',
'Total',
] as (
| 'FullJump'
| 'NextJump'
| 'NextPage'
| 'Number'
| 'PrevJump'
| 'PrevPage'
| 'Sizes'
| 'Total'
)[],
},
// 配置远程加载
proxyConfig: {
autoLoad: false, // 禁用自动加载,手动控制
ajax: {
query: async ({ page }, formValues) => {
// 确保 shopId 有值才查询,使用稳定的 shopId 引用
const currentShopId = stableShopId.value;
if (!currentShopId) {
return {
items: [],
total: 0,
};
}
try {
tableLoading.value = true;
const params: PageQueryDTO<ShopDTO.ProductFilters> = {
page: page.currentPage,
pageSize: page.pageSize,
filters: {
shopId: currentShopId,
productName: formValues?.productName || undefined,
productCode: formValues?.productCode || undefined,
},
};
const data = await getShopProductPageApi(params);
return {
items: data.records || [],
total: data.total || 0,
};
} catch (error) {
console.error('获取商品列表失败:', error);
message.error('获取商品列表失败');
return {
items: [],
total: 0,
};
} finally {
tableLoading.value = false;
}
},
},
},
};
// 搜索表单配置 - 使用稳定的对象引用
const formOptions = {
schema: [
{
component: 'Input',
componentProps: {
placeholder: '请输入商品名称',
},
fieldName: 'productName',
label: '商品名称',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入商品编码',
},
fieldName: 'productCode',
label: '商品编码',
},
],
showDefaultActions: true,
collapsed: true,
collapsedRows: 1,
};
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions,
showSearchForm: true,
tableTitle: '商品列表',
});
// 初始化数据
onMounted(() => {
if (shopId.value) {
fetchCustomStatistics();
// 手动触发表格加载,确保 shopId 有值
setTimeout(() => {
if (gridApi && shopId.value) {
gridApi.reload();
}
}, 100);
} else {
message.error('店铺ID不存在');
}
});
</script>
<template>
<Page title="店铺详情" auto-content-height>
<!-- 店铺统计信息 -->
<Card title="店铺统计信息" class="mb-4" :loading="loading">
<div class="mb-4">
<Space>
<span>选择日期范围:</span>
<DatePicker.RangePicker
v-model:value="dateRange"
format="YYYY-MM-DD"
@change="handleDateRangeChange"
/>
</Space>
</div>
<Row :gutter="16" v-if="customStats">
<Col :span="6">
<Statistic
title="销售额"
:value="customStats.salesAmount"
:formatter="({ value }) => formatCurrency(value as number)"
/>
</Col>
<Col :span="6">
<Statistic title="订单数" :value="customStats.orderCount" />
</Col>
<Col :span="6">
<Statistic
title="客单价"
:value="customStats.averageOrderValue"
:formatter="({ value }) => formatCurrency(value as number)"
/>
</Col>
<Col :span="6">
<Statistic
title="成本"
:value="customStats.costAmount"
:formatter="({ value }) => formatCurrency(value as number)"
/>
</Col>
<Col :span="6" class="mt-4">
<Statistic
title="退款金额"
:value="customStats.refundAmount"
:formatter="({ value }) => formatCurrency(value as number)"
/>
</Col>
<Col :span="6" class="mt-4">
<Statistic
title="推广费"
:value="customStats.promotionFee"
:formatter="({ value }) => formatCurrency(value as number)"
/>
</Col>
<Col :span="6" class="mt-4">
<Statistic
title="费比"
:value="customStats.feeRatio"
:formatter="({ value }) => formatPercent(value as number)"
/>
</Col>
</Row>
</Card>
<!-- 商品列表表格 -->
<Card title="商品详细信息" :loading="tableLoading">
<Grid>
<!-- 商品主图列 -->
<template #mainImage="{ row }: { row: ShopVO.ProductDetail }">
<div
v-if="row.mainImage"
style="display: flex; justify-content: center; align-items: center"
>
<Image
:src="row.mainImage"
:alt="row.productName"
:preview="false"
style="
width: 50px;
height: 50px;
object-fit: cover;
border-radius: 4px;
"
/>
</div>
<div
v-else
style="display: flex; justify-content: center; align-items: center"
>
<span style="color: #999">暂无图片</span>
</div>
</template>
</Grid>
</Card>
</Page>
</template>
@@ -4,6 +4,7 @@ import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
import type { PageQueryDTO } from '#/types/common';
import { computed, ref } from 'vue';
import { useRouter } from 'vue-router';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
@@ -17,6 +18,8 @@ import ShopFormModal from './components/ShopFormModal.vue';
defineOptions({ name: 'ShopManagement' });
const router = useRouter();
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
@@ -59,6 +62,7 @@ const gridOptions: VxeGridProps<any> = {
title: '店铺名称',
minWidth: 'auto',
showOverflow: 'tooltip' as const,
slots: { default: 'name' },
},
{
field: 'platform',
@@ -220,6 +224,11 @@ async function handleDelete(record: ShopVO.Info) {
function handleModalSuccess() {
refreshTable();
}
// 处理查看详情
function handleViewDetails(record: ShopVO.Info) {
router.push(`/ecom/shop/details/${record.id}`);
}
</script>
<template>
@@ -267,9 +276,22 @@ function handleModalSuccess() {
</div>
</template>
<!-- 店铺名称列 -->
<template #name="{ row }: { row: ShopVO.Info }">
<a
class="text-primary cursor-pointer hover:underline"
@click="handleViewDetails(row)"
>
{{ row.name }}
</a>
</template>
<!-- 操作列 -->
<template #action="{ row }: { row: any }">
<Space>
<Button size="small" type="primary" @click="handleViewDetails(row)">
查看详情
</Button>
<Button size="small" @click="handleEdit(row)"> 编辑 </Button>
<Popconfirm
title="确定要删除这个店铺吗?"