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

580 lines
16 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { BrushOrderDTO, BrushOrderVO } from '#/api/ecommerce/brush-order';
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import type { ShopVO } from '#/api/ecommerce/shop/types';
import type { PageQueryDTO } from '#/types/common';
import { computed, onMounted, ref, watch } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import {
Button,
Card,
DatePicker,
Empty,
Flex,
InputNumber,
message,
Popconfirm,
Select,
Space,
Spin,
} from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteBrushOrderApi,
getBrushOrderPageApi,
} from '#/api/ecommerce/brush-order';
import { getProductPageApi } from '#/api/ecommerce/product';
import { getShopPageApi } from '#/api/ecommerce/shop/apis';
import BrushOrderFormModal from './components/BrushOrderFormModal.vue';
import {
SIDEBAR_LIST_SHELL_CLASS,
sidebarListRowClass,
} from '#/utils/sidebar-list-styles';
defineOptions({ name: 'BrushOrderManagement' });
const PlusIcon = createIconifyIcon('lucide:plus');
const StoreIcon = createIconifyIcon('lucide:store');
const loading = ref(false);
const shopsLoading = ref(false);
const shops = ref<ShopVO.Info[]>([]);
/** 未选店铺:undefined 表示全部 */
const selectedShopId = ref<number | undefined>(undefined);
const formModalVisible = ref(false);
const currentBrushOrderData = ref<BrushOrderVO.Info | null>(null);
const presetShopIdForModal = ref<number | null>(null);
const searchForm = ref({
productId: undefined as number | undefined,
minBrushAmount: undefined as number | undefined,
maxBrushAmount: undefined as number | undefined,
startBrushTime: undefined as string | undefined,
endBrushTime: undefined as string | undefined,
});
const productOptions = ref<{ label: string; value: number }[]>([]);
const productPage = ref(1);
const productPageSize = 20;
const productTotal = ref(0);
const productLoading = ref(false);
const productSearchKeyword = ref('');
const productLoaded = ref(false);
let productSearchTimer: null | ReturnType<typeof setTimeout> = null;
function productOptionLabel(p: ProductVO.Info) {
const code = p.productCode ? ` · ${p.productCode}` : '';
return `${p.name}${code}`;
}
async function fetchProducts(page = 1, append = false) {
if (productLoading.value) return;
productLoading.value = true;
try {
const kw = productSearchKeyword.value.trim();
const params: PageQueryDTO<ProductDTO.Filters> = {
page,
pageSize: productPageSize,
filters: kw ? { keywords: kw } : {},
};
const data = await getProductPageApi(params);
const mapped = data.records.map((p: ProductVO.Info) => ({
label: productOptionLabel(p),
value: p.id,
}));
productOptions.value = append ? [...productOptions.value, ...mapped] : mapped;
productTotal.value = data.total;
productPage.value = page;
productLoaded.value = true;
} catch {
message.error('获取商品列表失败');
if (!append) productOptions.value = [];
} finally {
productLoading.value = false;
}
}
function handleProductSearch(value: string) {
if (productSearchTimer) clearTimeout(productSearchTimer);
productSearchTimer = setTimeout(() => {
productSearchKeyword.value = value;
productPage.value = 1;
void fetchProducts(1, false);
}, 300);
}
function handleProductPopupScroll(e: Event) {
const target = e.target as HTMLElement;
const { scrollTop, scrollHeight, clientHeight } = target;
if (scrollHeight - scrollTop - clientHeight < 50) {
const hasMore = productPage.value * productPageSize < productTotal.value;
if (hasMore && !productLoading.value) {
void fetchProducts(productPage.value + 1, true);
}
}
}
async function loadShops() {
shopsLoading.value = true;
try {
const response = await getShopPageApi({
page: 1,
pageSize: 500,
filters: {},
});
shops.value = response.records ?? [];
} catch {
message.error('获取店铺列表失败');
shops.value = [];
} finally {
shopsLoading.value = false;
}
}
const selectedShopLabel = computed(() => {
if (selectedShopId.value == null) {
return '全部店铺';
}
const s = shops.value.find((x) => x.id === selectedShopId.value);
return s?.name ?? `店铺 #${selectedShopId.value}`;
});
function selectShopAll() {
selectedShopId.value = undefined;
}
function selectShop(id: number) {
selectedShopId.value = id;
}
const gridOptions: VxeGridProps<BrushOrderVO.Info> = {
columns: [
{
type: 'seq' as const,
width: 56,
title: '序号',
},
{
field: 'shopName',
title: '店铺名称',
minWidth: 120,
showOverflow: 'tooltip' as const,
},
{
title: '商品信息',
minWidth: 160,
slots: { default: 'productInfo' },
},
{
field: 'quantity',
title: '刷单数量',
width: 100,
},
{
title: '刷单金额',
width: 120,
slots: { default: 'brushAmount' },
},
{
field: 'brushTime',
title: '刷单时间',
minWidth: 160,
},
{
field: 'createTime',
title: '创建时间',
minWidth: 160,
},
{
title: '操作',
width: 140,
fixed: 'right' as const,
slots: { default: 'action' },
},
],
height: '100%',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
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 }) => {
try {
loading.value = true;
const params: PageQueryDTO<BrushOrderDTO.Filters> = {
page: page.currentPage,
pageSize: page.pageSize,
filters: {
shopId: selectedShopId.value,
productId: searchForm.value.productId || undefined,
minBrushAmount: searchForm.value.minBrushAmount,
maxBrushAmount: searchForm.value.maxBrushAmount,
startBrushTime: searchForm.value.startBrushTime || undefined,
endBrushTime: searchForm.value.endBrushTime || undefined,
},
};
const data = await getBrushOrderPageApi(params);
return { items: data.records, total: data.total };
} catch {
message.error('获取刷单记录列表失败');
return { items: [], total: 0 };
} finally {
loading.value = false;
}
},
},
},
};
function refreshTable() {
gridApi?.reload();
}
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
showSearchForm: false,
tableTitle: '刷单记录',
});
watch(selectedShopId, () => {
gridApi.reload();
});
onMounted(() => {
void loadShops();
});
function resetModalPreset() {
presetShopIdForModal.value = null;
}
function shopRowClass(id: number | 'all') {
const active =
id === 'all'
? selectedShopId.value === undefined
: selectedShopId.value === id;
const layout =
id === 'all'
? 'mb-1.5 flex w-full cursor-pointer items-center gap-2 text-left'
: 'group flex w-full cursor-pointer items-center gap-2';
return sidebarListRowClass(active, layout);
}
const handleAdd = () => {
currentBrushOrderData.value = null;
resetModalPreset();
formModalVisible.value = true;
};
function handleAddForShop(shopId: number) {
selectShop(shopId);
currentBrushOrderData.value = null;
presetShopIdForModal.value = shopId;
formModalVisible.value = true;
}
const handleEdit = (record: BrushOrderVO.Info) => {
resetModalPreset();
currentBrushOrderData.value = record;
formModalVisible.value = true;
};
const handleDelete = async (id: number) => {
try {
await deleteBrushOrderApi(id);
message.success('删除成功');
refreshTable();
} catch {
message.error('删除失败');
}
};
const handleFormSuccess = () => {
resetModalPreset();
refreshTable();
};
const handleFormCancel = () => {
resetModalPreset();
formModalVisible.value = false;
};
const formatAmount = (amount: number) => `¥${amount.toFixed(2)}`;
</script>
<template>
<Page title="刷单记录管理" :auto-content-height="true">
<template #description>
<div class="text-foreground/80 mt-2">
左侧选择店铺筛选右侧记录;未选店铺时展示全部。可在店铺行快捷新增该店铺刷单记录。
</div>
</template>
<template #extra>
<Space>
<Button type="primary" @click="handleAdd">
<PlusIcon class="mr-2 h-4 w-4" />
新增刷单记录
</Button>
</Space>
</template>
<Flex gap="middle" class="h-full min-h-0">
<Card
class="flex h-full w-[420px] shrink-0 flex-col"
:body-style="{
height: '100%',
display: 'flex',
flexDirection: 'column',
gap: '8px',
minHeight: 0,
padding: '12px',
}"
>
<template #title>
<span class="flex items-center gap-2 text-base font-semibold">
<StoreIcon class="h-4 w-4" />
店铺
</span>
</template>
<Spin :spinning="shopsLoading" class="flex min-h-0 flex-1 flex-col">
<div :class="SIDEBAR_LIST_SHELL_CLASS">
<button
type="button"
:class="shopRowClass('all')"
@click="selectShopAll"
>
<span class="min-w-0 flex-1 truncate">全部店铺</span>
<span class="shrink-0 text-xs font-normal text-muted-foreground">
全部记录
</span>
</button>
<Empty
v-if="!shopsLoading && shops.length === 0"
class="py-8"
description="暂无店铺"
/>
<div v-else class="space-y-1">
<div
v-for="shop in shops"
:key="shop.id"
:class="shopRowClass(shop.id)"
class="group w-full"
>
<button
type="button"
class="min-w-0 flex-1 truncate text-left"
@click="selectShop(shop.id)"
>
{{ shop.name }}
</button>
<Button
type="link"
size="small"
class="!h-7 shrink-0 !px-1 !text-xs font-semibold opacity-80 group-hover:opacity-100"
@click.stop="handleAddForShop(shop.id)"
>
<PlusIcon class="mr-0.5 inline h-3.5 w-3.5" />
添加
</Button>
</div>
</div>
</div>
</Spin>
</Card>
<Card
class="flex h-full min-h-0 min-w-0 flex-1 flex-col"
:body-style="{
height: '100%',
minHeight: 0,
padding: 0,
display: 'flex',
flexDirection: 'column',
}"
>
<template #title>
<span class="text-base font-semibold">刷单记录</span>
</template>
<div
class="shrink-0 space-y-2 border-t border-border px-4 py-3"
>
<div class="flex flex-wrap items-end gap-2">
<div class="flex min-w-0 flex-col gap-0.5">
<span class="text-xs text-muted-foreground">商品</span>
<Select
v-model:value="searchForm.productId"
allow-clear
show-search
:filter-option="false"
placeholder="名称/编码搜索"
class="min-w-[220px] max-w-[320px]"
:options="productOptions"
:loading="productLoading"
:dropdown-match-select-width="false"
:dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
@search="handleProductSearch"
@popup-scroll="handleProductPopupScroll"
@focus="
() => {
if (!productLoaded) void fetchProducts(1, false);
}
"
@change="() => refreshTable()"
/>
</div>
<div class="flex flex-col gap-0.5">
<span class="text-xs text-muted-foreground">金额 ≥</span>
<InputNumber
v-model:value="searchForm.minBrushAmount"
:min="0"
:precision="2"
placeholder="最小"
class="!w-[110px]"
allow-clear
@press-enter="refreshTable"
@blur="refreshTable"
/>
</div>
<div class="flex flex-col gap-0.5">
<span class="text-xs text-muted-foreground">金额 ≤</span>
<InputNumber
v-model:value="searchForm.maxBrushAmount"
:min="0"
:precision="2"
placeholder="最大"
class="!w-[110px]"
allow-clear
@press-enter="refreshTable"
@blur="refreshTable"
/>
</div>
<div class="flex flex-col gap-0.5">
<span class="text-xs text-muted-foreground">刷单时间起</span>
<DatePicker
v-model:value="searchForm.startBrushTime"
show-time
value-format="YYYY-MM-DD HH:mm:ss"
format="YYYY-MM-DD HH:mm:ss"
placeholder="开始"
class="w-[180px]"
allow-clear
@change="() => refreshTable()"
/>
</div>
<div class="flex flex-col gap-0.5">
<span class="text-xs text-muted-foreground">刷单时间止</span>
<DatePicker
v-model:value="searchForm.endBrushTime"
show-time
value-format="YYYY-MM-DD HH:mm:ss"
format="YYYY-MM-DD HH:mm:ss"
placeholder="结束"
class="w-[180px]"
allow-clear
@change="() => refreshTable()"
/>
</div>
<Button type="default" size="small" class="mb-0.5" @click="refreshTable">
查询
</Button>
</div>
<div class="text-xs text-muted-foreground">
当前:{{ selectedShopLabel }}
</div>
</div>
<div class="min-h-0 flex-1 overflow-hidden px-2 pb-2 pt-0 sm:px-3 sm:pb-3">
<Grid :loading="loading" class="h-full min-h-0">
<template #toolbar-tools>
<span class="text-xs text-muted-foreground">
修改筛选后点击「查询」或失焦/回车刷新;左侧切换店铺立即刷新
</span>
</template>
<template #productInfo="{ row }">
<div v-if="row.productName">
<div class="font-semibold">{{ row.productName }}</div>
<div
v-if="row.productSpecName"
class="text-xs text-muted-foreground"
>
{{ row.productSpecName }}
</div>
</div>
<span v-else class="text-muted-foreground">—</span>
</template>
<template #brushAmount="{ row }">
<span class="font-semibold text-green-600">
{{ formatAmount(row.brushAmount ?? 0) }}
</span>
</template>
<template #action="{ row }">
<Space>
<Button size="small" @click="handleEdit(row)">编辑</Button>
<Popconfirm
title="确定要删除这条刷单记录吗?"
@confirm="handleDelete(row.id)"
>
<Button size="small" danger>删除</Button>
</Popconfirm>
</Space>
</template>
</Grid>
</div>
</Card>
</Flex>
<BrushOrderFormModal
v-model:open="formModalVisible"
:brush-order-data="currentBrushOrderData"
:preset-shop-id="presetShopIdForModal"
@success="handleFormSuccess"
@cancel="handleFormCancel"
/>
</Page>
</template>