Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/order/index.vue
T

446 lines
11 KiB
Vue

<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { OrderDTO, OrderVO } from '#/api/ecommerce/order';
import type { ShopDTO, ShopVO } from '#/api/ecommerce/shop/types';
import type { PageQueryDTO } from '#/types/common';
import { computed, onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { Button, message, Popconfirm, Space, Tag } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteOrderApi,
getOrderPageApi,
} from '#/api/ecommerce/order';
import { getAllOrderStatusListApi } from '#/api/ecommerce/order-status';
import type { OrderStatusVO } from '#/api/ecommerce/order-status/types';
import { getShopPageApi } from '#/api/ecommerce/shop';
import OrderFormModal from './components/OrderFormModal.vue';
defineOptions({ name: 'OrderManagement' });
// 创建图标组件
const PlusIcon = createIconifyIcon('lucide:plus');
// 响应式数据
const loading = ref(false);
const orderFormModalRef = ref();
// 店铺分页数据
const shopOptions = ref<{ label: string; value: number }[]>([]);
const shopPage = ref(1);
const shopPageSize = 20;
const shopTotal = ref(0);
const shopLoading = ref(false);
const shopSearchKeyword = ref('');
let shopSearchTimer: ReturnType<typeof setTimeout> | null = null;
// 订单状态选项
const orderStatusOptions = ref<{ label: string; value: number }[]>([]);
// 搜索表单
const searchForm = ref({
platformOrderNo: '',
shopId: undefined as number | undefined,
statusId: undefined as number | undefined,
});
// 当前排序状态
const currentSort = ref<{ field: string; order: 'asc' | 'desc' } | null>(null);
// 加载店铺列表(支持分页)
async function fetchShops(page = 1, append = false) {
if (shopLoading.value) return;
shopLoading.value = true;
try {
const params: PageQueryDTO<ShopDTO.Filters> = {
page,
pageSize: shopPageSize,
filters: shopSearchKeyword.value ? { name: shopSearchKeyword.value } : {},
};
const data = await getShopPageApi(params);
if (append) {
shopOptions.value = [...shopOptions.value, ...data.records.map((shop: ShopVO.Info) => ({
label: shop.name,
value: shop.id,
}))];
} else {
shopOptions.value = data.records.map((shop: ShopVO.Info) => ({
label: shop.name,
value: shop.id,
}));
}
shopTotal.value = data.total;
shopPage.value = page;
} catch (error) {
console.error('获取店铺列表失败:', error);
} finally {
shopLoading.value = false;
}
}
// 搜索店铺(防抖)
function handleShopSearch(value: string) {
if (shopSearchTimer) clearTimeout(shopSearchTimer);
shopSearchTimer = setTimeout(() => {
shopSearchKeyword.value = value;
shopPage.value = 1;
fetchShops(1, false);
}, 300);
}
// 滚动加载更多店铺
function handleShopPopupScroll(e: Event) {
const target = e.target as HTMLElement;
const { scrollTop, scrollHeight, clientHeight } = target;
// 滚动到底部附近时加载更多
if (scrollHeight - scrollTop - clientHeight < 50) {
const hasMore = shopPage.value * shopPageSize < shopTotal.value;
if (hasMore && !shopLoading.value) {
fetchShops(shopPage.value + 1, true);
}
}
}
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'seq' as const,
minWidth: 'auto',
title: '序号',
},
{
field: 'platformOrderNo',
title: '平台订单号',
minWidth: 'auto',
showOverflow: 'tooltip' as const,
sortable: true,
},
{
field: 'shopName',
title: '店铺名称',
minWidth: 'auto',
showOverflow: 'tooltip' as const,
sortable: true,
},
{
title: '商品信息',
minWidth: 'auto',
slots: { default: 'productInfo' },
},
{
field: 'quantity',
title: '数量',
minWidth: 'auto',
sortable: true,
},
{
field: 'receivedAmount',
title: '实收金额',
minWidth: 'auto',
sortable: true,
slots: { default: 'receivedAmount' },
},
{
title: '订单状态',
minWidth: 'auto',
slots: { default: 'orderStatus' },
},
{
field: 'orderTime',
title: '下单时间',
minWidth: 'auto',
sortable: true,
},
{
field: 'createTime',
title: '创建时间',
minWidth: 'auto',
sortable: true,
},
{
title: '操作',
minWidth: 'auto',
fixed: 'right' as const,
slots: { default: 'action' },
},
],
height: 'auto',
border: true,
stripe: true,
highlightHoverRow: true,
showOverflow: true,
size: 'small',
sortConfig: {
remote: false,
trigger: 'cell',
multiple: false,
orders: ['desc', 'asc'],
},
sortChange: ({ field, order }) => {
// 保存排序状态并触发搜索
currentSort.value = { field, order: order || 'desc' };
gridApi?.reload();
},
toolbarConfig: {
custom: true,
refresh: true,
zoom: true,
},
data: [],
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: {
ajax: {
query: async ({ page, form }) => {
try {
loading.value = true;
// 从表单获取搜索参数
const searchParams = form || {};
const params: PageQueryDTO<OrderDTO.Filters> = {
page: page.currentPage,
pageSize: page.pageSize,
filters: {
platformOrderNo: searchParams.platformOrderNo || undefined,
shopId: searchParams.shopId,
statusId: searchParams.statusId,
},
};
// 处理排序参数
if (currentSort.value) {
params.sorts = [{ field: currentSort.value.field, order: currentSort.value.order }];
}
const data = await getOrderPageApi(params);
return {
items: data.records,
total: data.total,
};
} catch (error) {
console.error('获取订单列表失败:', error);
message.error('获取订单列表失败');
return {
items: [],
total: 0,
};
} finally {
loading.value = false;
}
},
},
},
};
// 搜索表单配置
const formOptions = computed(() => ({
schema: [
{
component: 'Input',
componentProps: {
placeholder: '请输入平台订单号(回车搜索)',
onPressEnter: () => gridApi?.reload(),
},
fieldName: 'platformOrderNo',
label: '平台订单号',
},
{
component: 'Select',
componentProps: {
placeholder: '请选择店铺(支持搜索)',
options: shopOptions,
allowClear: true,
showSearch: true,
filterOption: false,
onSearch: handleShopSearch,
onPopupScroll: handleShopPopupScroll,
onChange: () => gridApi?.reload(),
loading: shopLoading,
dropdownMenuColumnStyle: { maxHeight: '300px', overflow: 'auto' },
},
fieldName: 'shopId',
label: '店铺',
},
{
component: 'Select',
componentProps: {
placeholder: '请选择状态',
options: orderStatusOptions,
allowClear: true,
onChange: () => gridApi?.reload(),
},
fieldName: 'statusId',
label: '订单状态',
},
],
showDefaultActions: true,
collapsed: true,
collapsedRows: 2,
}));
// 刷新表格数据
function refreshTable() {
if (gridApi) {
gridApi.reload();
}
}
// 获取店铺选项(首次加载)
async function fetchShopOptions() {
await fetchShops(1, false);
}
// 获取订单状态选项
async function fetchOrderStatusOptions() {
try {
const data = await getAllOrderStatusListApi();
orderStatusOptions.value = data.map((status: OrderStatusVO.Info) => ({
label: status.statusName,
value: status.id,
}));
} catch (error) {
console.error('获取订单状态列表失败:', error);
}
}
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions: formOptions.value,
showSearchForm: true,
tableTitle: '订单管理',
});
// 处理添加订单
function handleAdd() {
orderFormModalRef.value?.open(false);
}
// 处理编辑订单
function handleEdit(record: OrderVO.Info) {
orderFormModalRef.value?.open(true, record);
}
// 处理删除订单
async function handleDelete(record: OrderVO.Info) {
try {
await deleteOrderApi(record.id);
message.success('删除成功');
refreshTable();
} catch (error) {
console.error('删除订单失败:', error);
message.error('删除订单失败');
}
}
// 处理模态框成功
function handleModalSuccess() {
refreshTable();
}
// 初始化
onMounted(async () => {
await fetchShopOptions();
await fetchOrderStatusOptions();
});
</script>
<template>
<Page auto-content-height>
<!-- VbenVxeGrid 表格 -->
<Grid :loading="loading">
<!-- 工具栏工具 -->
<template #toolbar-tools>
<Space>
<Button type="primary" @click="handleAdd">
<PlusIcon />
添加订单
</Button>
</Space>
</template>
<!-- 商品信息列 -->
<template #productInfo="{ row }: { row: any }">
<div v-if="row.linkProductName">
<div style="font-weight: bold">{{ row.linkProductName }}</div>
<div v-if="row.linkSpecName" style="color: #666; font-size: 12px">
{{ row.linkSpecName }}
</div>
</div>
<span v-else style="color: #999">暂无商品信息</span>
</template>
<!-- 数量列 -->
<template #quantity="{ row }: { row: any }">
{{ row.quantity || 0 }}
</template>
<!-- 实收金额列 -->
<template #receivedAmount="{ row }: { row: any }">
<span style="color: #52c41a; font-weight: bold">
¥{{ (row.receivedAmount || 0).toFixed(2) }}
</span>
</template>
<!-- 订单状态列 -->
<template #orderStatus="{ row }: { row: any }">
<Tag
:color="row.orderStatus?.color || 'blue'"
class="order-status-tag"
>
{{ row.statusName || '未知状态' }}
</Tag>
</template>
<!-- 操作列 -->
<template #action="{ row }: { row: any }">
<Space>
<Button size="small" @click="handleEdit(row)"> 编辑 </Button>
<Popconfirm
title="确定要删除这个订单吗?"
@confirm="handleDelete(row)"
>
<Button size="small" danger> 删除 </Button>
</Popconfirm>
</Space>
</template>
</Grid>
<!-- 订单表单模态框 -->
<OrderFormModal ref="orderFormModalRef" @success="handleModalSuccess" />
</Page>
</template>
<style scoped>
/* 订单状态 tag 描边样式 */
:deep(.order-status-tag.ant-tag) {
background-color: transparent !important;
border: 2px solid currentColor;
}
</style>