refactor(product): 删除列表视图,仅保留瀑布流网格视图,简化架构

This commit is contained in:
小蟑螂
2026-04-28 08:59:19 +08:00
parent b25e01a15c
commit a838b92fec
3 changed files with 253 additions and 798 deletions
@@ -1,32 +1,37 @@
<script lang="ts" setup>
import type { ProductVO } from '#/api/ecommerce/product/types';
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import type { PageQueryDTO } from '#/types/common';
import { ref } from 'vue';
import { nextTick, onMounted, onUnmounted, ref } from 'vue';
import { Waterfall } from 'vue-waterfall-plugin-next';
import 'vue-waterfall-plugin-next/dist/style.css';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { Button, Divider, Flex, Space } from 'ant-design-vue';
import {
Button,
Card,
Empty,
Flex,
Form,
Input,
message,
Space,
Spin,
} from 'ant-design-vue';
import { deleteProductApi } from '#/api/ecommerce/product';
import { deleteProductApi, getProductPageApi } from '#/api/ecommerce/product';
import ProductCard from './components/ProductCard.vue';
import ProductFormModal from './components/ProductFormModal.vue';
import ProductGridView from './views/ProductGridView/index.vue';
import ProductListView from './views/ProductListView/index.vue';
defineOptions({ name: 'ProductManagement' });
// 创建图标组件
const GridIcon = createIconifyIcon('lucide:grid-3x3');
const ListIcon = createIconifyIcon('lucide:list');
const PlusIcon = createIconifyIcon('lucide:plus');
// 响应式数据
const viewMode = ref<'grid' | 'table'>('grid');
const productFormModalRef = ref();
const productListViewRef = ref<null | { refreshTable: () => void }>(null);
const productGridViewRef = ref<null | { refreshTable: () => void }>(null);
// 搜索表单
const searchForm = ref({
name: '',
productCode: '',
@@ -34,26 +39,81 @@ const searchForm = ref({
specName: '',
});
// 刷新表格数据
function refreshTable() {
if (viewMode.value === 'table' && productListViewRef.value) {
productListViewRef.value.refreshTable();
} else if (viewMode.value === 'grid' && productGridViewRef.value) {
productGridViewRef.value.refreshTable();
const loading = ref(false);
const productList = ref<ProductVO.Info[]>([]);
const total = ref(0);
const current = ref(1);
const pageSize = ref(24);
const hasMore = ref(true);
const scrollContainerRef = ref<HTMLElement | null>(null);
const waterfallRef = ref();
const loadProductList = async (reset = false) => {
if (loading.value || (!hasMore.value && !reset)) return;
loading.value = true;
try {
const params: PageQueryDTO<ProductDTO.Filters> = {
page: current.value,
pageSize: pageSize.value,
filters: {
name: searchForm.value.name || undefined,
productCode: searchForm.value.productCode || undefined,
specCode: searchForm.value.specCode || undefined,
specName: searchForm.value.specName || undefined,
},
};
const response = await getProductPageApi(params);
if (reset) {
productList.value = response.records;
} else {
productList.value.push(...response.records);
}
total.value = response.total;
if (productList.value.length >= response.total) {
hasMore.value = false;
} else {
current.value++;
hasMore.value = true;
}
} catch {
message.error('加载商品列表失败');
} finally {
loading.value = false;
}
}
};
// 处理添加商品
function handleAdd() {
productFormModalRef.value?.open(false);
}
const handleSearch = () => {
current.value = 1;
productList.value = [];
hasMore.value = true;
loadProductList(true);
};
// 处理编辑商品
function handleEdit(record: ProductVO.Info) {
productFormModalRef.value?.open(true, record);
}
const handleReset = () => {
searchForm.value = {
name: '',
productCode: '',
specCode: '',
specName: '',
};
handleSearch();
};
const handleScroll = () => {
const container = scrollContainerRef.value;
if (!container || loading.value || !hasMore.value) return;
const { scrollTop, scrollHeight, clientHeight } = container;
if (scrollTop + clientHeight >= scrollHeight - 200) {
loadProductList();
}
};
// 处理删除商品
async function handleDelete(record: ProductVO.Info) {
try {
await deleteProductApi(record.id);
@@ -63,74 +123,181 @@ async function handleDelete(record: ProductVO.Info) {
}
}
// 处理列表视图删除
const handleListViewDelete = (product: ProductVO.Info) => {
handleDelete(product);
function refreshTable(updatedProduct?: ProductVO.Info) {
if (updatedProduct) {
const index = productList.value.findIndex((p) => p.id === updatedProduct.id);
if (index !== -1) {
productList.value[index] = updatedProduct;
}
return;
}
current.value = 1;
productList.value = [];
hasMore.value = true;
loadProductList(true);
}
const handleEditingChange = async () => {
await nextTick();
await nextTick();
if (waterfallRef.value) {
waterfallRef.value.renderer();
}
};
// 切换视图模式
function toggleViewMode() {
viewMode.value = viewMode.value === 'grid' ? 'table' : 'grid';
function handleAdd() {
productFormModalRef.value?.open(false);
}
// 处理模态框成功
function handleModalSuccess() {
refreshTable();
}
onMounted(() => {
loadProductList(true);
setTimeout(() => {
if (scrollContainerRef.value) {
scrollContainerRef.value.addEventListener('scroll', handleScroll);
}
}, 100);
});
onUnmounted(() => {
if (scrollContainerRef.value) {
scrollContainerRef.value.removeEventListener('scroll', handleScroll);
}
});
</script>
<template>
<Page :auto-content-height="true" content-class="flex-col dark:bg-[#0f1623]">
<!-- 工具栏区域 -->
<template #extra>
<Flex gap="small" align="center">
<Space>
<Button type="primary" @click="handleAdd">
<PlusIcon class="mr-2 h-4 w-4" />
添加商品
</Button>
</Space>
<div class="flex min-h-0 flex-1 flex-col dark:bg-[#0f1623]">
<div class="flex min-h-0 flex-1 flex-col">
<Card class="mb-4 shrink-0" :body-style="{ padding: '16px' }">
<Form :model="searchForm" layout="inline" class="w-full">
<Flex class="w-full" justify="end" align="center" gap="4">
<Form.Item label="商品名称" class="mb-0">
<Input
v-model:value="searchForm.name"
placeholder="请输入商品名称"
style="width: 200px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="商品编码" class="mb-0">
<Input
v-model:value="searchForm.productCode"
placeholder="请输入商品编码"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="规格编码" class="mb-0">
<Input
v-model:value="searchForm.specCode"
placeholder="请输入规格编码"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="规格名称" class="mb-0">
<Input
v-model:value="searchForm.specName"
placeholder="请输入规格名称"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item class="mb-0">
<Space>
<Button type="primary" @click="handleSearch">搜索</Button>
<Button @click="handleReset">重置</Button>
</Space>
</Form.Item>
<div class="ml-auto text-sm text-gray-500 dark:text-gray-400">
共 {{ total }} 个商品
</div>
</Flex>
</Form>
</Card>
<Divider type="vertical" />
<div
ref="scrollContainerRef"
class="min-h-0 flex-1 overflow-y-auto"
>
<Card
class="flex min-h-full flex-col dark:bg-[#111827] dark:border-[#374151]"
:body-style="{
padding: '16px',
display: 'flex',
flexDirection: 'column',
minHeight: '100%',
}"
>
<div class="mb-3 flex shrink-0 justify-end">
<Button type="primary" @click="handleAdd">
<PlusIcon class="mr-2 h-4 w-4" />
添加商品
</Button>
</div>
<Spin :spinning="loading && productList.length === 0">
<div
v-if="!loading && productList.length === 0"
class="flex h-full items-center justify-center py-8"
>
<Empty />
</div>
<Space>
<Button
:type="viewMode === 'grid' ? 'primary' : 'default'"
@click="toggleViewMode"
>
<GridIcon class="mr-2 h-4 w-4" />
网格视图
</Button>
<Button
:type="viewMode === 'table' ? 'primary' : 'default'"
@click="toggleViewMode"
>
<ListIcon class="mr-2 h-4 w-4" />
列表视图
</Button>
</Space>
</Flex>
</template>
<Waterfall
v-else-if="productList.length > 0"
ref="waterfallRef"
:list="productList"
row-key="id"
:width="700"
:breakpoints="{
2700: { rowPerView: 3 },
2000: { rowPerView: 2 },
1400: { rowPerView: 2 },
900: { rowPerView: 1 },
500: { rowPerView: 1 },
}"
:gutter="20"
class="product-waterfall"
>
<template #default="{ item }">
<div class="w-full">
<ProductCard
:product="item"
@updated="(p) => refreshTable(p)"
@delete="handleDelete"
@editing-change="handleEditingChange"
/>
</div>
</template>
</Waterfall>
<!-- 列表视图 -->
<ProductListView
v-if="viewMode === 'table'"
ref="productListViewRef"
v-model:search-form="searchForm"
@edit="handleEdit"
@delete="handleListViewDelete"
/>
<!-- 网格视图 -->
<div v-else class="flex-1 overflow-y-auto dark:bg-[#0f1623]">
<ProductGridView
ref="productGridViewRef"
v-model:search-form="searchForm"
@delete="handleDelete"
/>
<div
v-if="productList.length > 0"
class="mt-4 flex justify-center py-4"
>
<Spin v-if="loading" />
<div
v-else-if="!hasMore"
class="text-sm text-gray-500 dark:text-gray-400"
>
没有更多商品了
</div>
</div>
</Spin>
</Card>
</div>
</div>
</div>
<!-- 商品表单模态框 -->
<ProductFormModal ref="productFormModalRef" @success="handleModalSuccess" />
</Page>
</template>
@@ -143,4 +310,8 @@ function handleModalSuccess() {
-webkit-box-orient: vertical;
overflow: hidden;
}
.product-waterfall {
background-color: transparent !important;
}
</style>
@@ -1,343 +0,0 @@
<script setup lang="ts">
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import type { PageQueryDTO } from '#/types/common';
import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
import { Waterfall } from 'vue-waterfall-plugin-next';
import 'vue-waterfall-plugin-next/dist/style.css';
import {
Button,
Card,
Empty,
Flex,
Form,
Input,
message,
Space,
Spin,
} from 'ant-design-vue';
import { getProductPageApi } from '#/api/ecommerce/product';
import ProductCard from '../../components/ProductCard.vue';
defineOptions({ name: 'ProductGridView' });
const props = defineProps<{
searchForm: {
name: string;
productCode: string;
specCode: string;
specName: string;
};
}>();
const emit = defineEmits<{
(e: 'delete', product: ProductVO.Info): void;
(e: 'update:searchForm', value: typeof props.searchForm): void;
}>();
// 响应式数据
const loading = ref(false);
const productList = ref<ProductVO.Info[]>([]);
const total = ref(0);
const current = ref(1);
const pageSize = ref(24); // 无限滚动默认每页24个
const hasMore = ref(true); // 是否还有更多数据
const scrollContainerRef = ref<HTMLElement | null>(null);
const waterfallRef = ref();
// 本地搜索表单副本(用于 v-model)
const localSearchForm = ref({
name: props.searchForm.name,
productCode: props.searchForm.productCode,
specCode: props.searchForm.specCode,
specName: props.searchForm.specName,
});
// 监听 props 变化,同步到本地
watch(
() => props.searchForm,
(newForm) => {
if (
localSearchForm.value.name !== newForm.name ||
localSearchForm.value.productCode !== newForm.productCode ||
localSearchForm.value.specCode !== newForm.specCode ||
localSearchForm.value.specName !== newForm.specName
) {
localSearchForm.value.name = newForm.name;
localSearchForm.value.productCode = newForm.productCode;
localSearchForm.value.specCode = newForm.specCode;
localSearchForm.value.specName = newForm.specName;
// 重置分页和列表
current.value = 1;
productList.value = [];
hasMore.value = true;
loadProductList(true);
}
},
{ deep: true },
);
// 监听本地表单变化,同步到父组件
watch(
localSearchForm,
(newForm) => {
if (
props.searchForm.name !== newForm.name ||
props.searchForm.productCode !== newForm.productCode ||
props.searchForm.specCode !== newForm.specCode ||
props.searchForm.specName !== newForm.specName
) {
emit('update:searchForm', { ...newForm });
}
},
{ deep: true },
);
// 加载商品列表
const loadProductList = async (reset = false) => {
if (loading.value || (!hasMore.value && !reset)) return;
loading.value = true;
try {
const params: PageQueryDTO<ProductDTO.Filters> = {
page: current.value,
pageSize: pageSize.value,
filters: {
name: localSearchForm.value.name || undefined,
productCode: localSearchForm.value.productCode || undefined,
specCode: localSearchForm.value.specCode || undefined,
specName: localSearchForm.value.specName || undefined,
},
};
const response = await getProductPageApi(params);
if (reset) {
productList.value = response.records;
} else {
productList.value.push(...response.records);
}
total.value = response.total;
// 判断是否还有更多数据
if (productList.value.length >= response.total) {
hasMore.value = false;
} else {
current.value++;
hasMore.value = true;
}
} catch {
message.error('加载商品列表失败');
} finally {
loading.value = false;
}
};
// 搜索
const handleSearch = () => {
current.value = 1;
productList.value = [];
hasMore.value = true;
loadProductList(true);
};
// 重置
const handleReset = () => {
localSearchForm.value = {
name: '',
productCode: '',
specCode: '',
specName: '',
};
handleSearch();
};
// 滚动监听
const handleScroll = () => {
const container = scrollContainerRef.value;
if (!container || loading.value || !hasMore.value) return;
const { scrollTop, scrollHeight, clientHeight } = container;
// 当滚动到距离底部 200px 时加载更多
if (scrollTop + clientHeight >= scrollHeight - 200) {
loadProductList();
}
};
// 事件处理
const handleDelete = (product: ProductVO.Info) => {
emit('delete', product);
};
const handleEditingChange = async () => {
// 等待卡片高度动画完成后再触发瀑布流重排
await nextTick();
// 等待下一个 tick 确保 DOM 已更新
await nextTick();
if (waterfallRef.value) {
waterfallRef.value.renderer();
}
};
// 刷新列表
function refreshTable(updatedProduct: ProductVO.Info) {
// 就地更新商品数据,不重新请求列表
const index = productList.value.findIndex((p) => p.id === updatedProduct.id);
if (index !== -1) {
productList.value[index] = updatedProduct;
}
}
// 暴露方法
defineExpose({
refreshTable,
});
// 滚动监听
onMounted(() => {
loadProductList(true);
// 延迟绑定滚动事件,确保 DOM 已渲染
setTimeout(() => {
if (scrollContainerRef.value) {
scrollContainerRef.value.addEventListener('scroll', handleScroll);
}
}, 100);
});
onUnmounted(() => {
if (scrollContainerRef.value) {
scrollContainerRef.value.removeEventListener('scroll', handleScroll);
}
});
</script>
<template>
<div class="flex h-full flex-col">
<!-- 搜索区域 -->
<Card class="mb-4" :body-style="{ padding: '16px' }">
<Form :model="localSearchForm" layout="inline" class="w-full">
<Flex class="w-full" justify="end" align="center" gap="4">
<Form.Item label="商品名称" class="mb-0">
<Input
v-model:value="localSearchForm.name"
placeholder="请输入商品名称"
style="width: 200px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="商品编码" class="mb-0">
<Input
v-model:value="localSearchForm.productCode"
placeholder="请输入商品编码"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="规格编码" class="mb-0">
<Input
v-model:value="localSearchForm.specCode"
placeholder="请输入规格编码"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item label="规格名称" class="mb-0">
<Input
v-model:value="localSearchForm.specName"
placeholder="请输入规格名称"
style="width: 150px"
allow-clear
@press-enter="handleSearch"
/>
</Form.Item>
<Form.Item class="mb-0">
<Space>
<Button type="primary" @click="handleSearch">搜索</Button>
<Button @click="handleReset">重置</Button>
</Space>
</Form.Item>
<div class="ml-auto text-sm text-gray-500 dark:text-gray-400">
共 {{ total }} 个商品
</div>
</Flex>
</Form>
</Card>
<!-- 网格内容 -->
<Card
class="flex flex-1 flex-col overflow-y-auto dark:bg-[#111827] dark:border-[#374151]"
:body-style="{
padding: '16px',
display: 'flex',
flexDirection: 'column',
height: '100%',
}"
>
<Spin :spinning="loading && productList.length === 0">
<!-- 空状态 -->
<div
v-if="!loading && productList.length === 0"
class="flex h-full items-center justify-center py-8"
>
<Empty />
</div>
<!-- 瀑布流 -->
<Waterfall
v-else-if="productList.length > 0"
ref="waterfallRef"
:list="productList"
row-key="id"
:width="700"
:breakpoints="{
2700: { rowPerView: 3 },
2000: { rowPerView: 2 },
1400: { rowPerView: 2 },
900: { rowPerView: 1 },
500: { rowPerView: 1 },
}"
:gutter="20"
class="product-waterfall"
>
<template #default="{ item }">
<div class="w-full">
<ProductCard
:product="item"
@updated="(product) => refreshTable(product)"
@delete="handleDelete"
@editing-change="handleEditingChange"
/>
</div>
</template>
</Waterfall>
<!-- 加载更多提示 -->
<div
v-if="productList.length > 0"
class="mt-4 flex justify-center py-4"
>
<Spin v-if="loading" />
<div
v-else-if="!hasMore"
class="text-sm text-gray-500 dark:text-gray-400"
>
没有更多商品了
</div>
</div>
</Spin>
</Card>
</div>
</template>
<style scoped>
.product-waterfall {
background-color: transparent !important;
}
</style>
@@ -1,373 +0,0 @@
<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
import type { PageQueryDTO } from '#/types/common';
import { computed, ref, watch } from 'vue';
import {
Button,
Image,
message,
Popconfirm,
Popover,
Space,
Tag,
} from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getProductPageApi } from '#/api/ecommerce/product';
defineOptions({ name: 'ProductListView' });
const props = defineProps<{
searchForm: {
name: string;
productCode: string;
specCode: string;
specName: string;
};
}>();
const emit = defineEmits<{
(e: 'edit', product: ProductVO.Info): void;
(e: 'delete', product: ProductVO.Info): void;
(e: 'update:searchForm', value: typeof props.searchForm): void;
}>();
// 响应式数据
const loading = ref(false);
// 本地搜索表单副本(用于 v-model)
const localSearchForm = ref({
name: props.searchForm.name,
productCode: props.searchForm.productCode,
specCode: props.searchForm.specCode,
specName: props.searchForm.specName,
});
// 监听 props 变化,同步到本地
watch(
() => props.searchForm,
(newForm) => {
if (
localSearchForm.value.name !== newForm.name ||
localSearchForm.value.productCode !== newForm.productCode ||
localSearchForm.value.specCode !== newForm.specCode ||
localSearchForm.value.specName !== newForm.specName
) {
localSearchForm.value.name = newForm.name;
localSearchForm.value.productCode = newForm.productCode;
localSearchForm.value.specCode = newForm.specCode;
localSearchForm.value.specName = newForm.specName;
refreshTable();
}
},
{ deep: true },
);
// 监听本地表单变化,同步到父组件
watch(
localSearchForm,
(newForm) => {
if (
props.searchForm.name !== newForm.name ||
props.searchForm.productCode !== newForm.productCode ||
props.searchForm.specCode !== newForm.specCode ||
props.searchForm.specName !== newForm.specName
) {
emit('update:searchForm', { ...newForm });
}
},
{ deep: true },
);
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
{
type: 'seq' as const,
minWidth: 'auto',
title: '序号',
},
{
title: '主图',
width: 60,
slots: { default: 'mainImage' },
},
{
field: 'name',
title: '商品名称',
minWidth: 200,
showOverflow: 'tooltip' as const,
},
{
field: 'productCode',
title: '商品编码',
minWidth: 150,
showOverflow: 'tooltip' as const,
},
{
field: 'categoryName',
title: '类目',
minWidth: 120,
showOverflow: 'tooltip' as const,
},
{
title: '规格数量',
minWidth: 100,
slots: { default: 'specs' },
},
{
field: 'createTime',
title: '创建时间',
minWidth: 180,
},
{
field: 'updateTime',
title: '更新时间',
minWidth: 180,
},
{
title: '操作',
minWidth: 200,
fixed: 'right' as const,
slots: { default: 'action' },
},
],
height: 'auto',
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<ProductDTO.Filters> = {
page: page.currentPage,
pageSize: page.pageSize,
filters: {
name: localSearchForm.value.name || undefined,
productCode: localSearchForm.value.productCode || undefined,
specCode: localSearchForm.value.specCode || undefined,
specName: localSearchForm.value.specName || undefined,
},
};
const data = await getProductPageApi(params);
return {
items: data.records,
total: data.total,
};
} catch (error) {
console.error('获取商品列表失败:', error);
message.error('获取商品列表失败');
return {
items: [],
total: 0,
};
} finally {
loading.value = false;
}
},
},
},
};
// 搜索表单配置
const gridReloadRef = ref<(() => void) | null>(null);
const formOptions = computed(() => ({
schema: [
{
component: 'Input',
componentProps: {
placeholder: '请输入商品名称(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
fieldName: 'name',
label: '商品名称',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入商品编码(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
fieldName: 'productCode',
label: '商品编码',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入规格编码(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
fieldName: 'specCode',
label: '规格编码',
},
{
component: 'Input',
componentProps: {
placeholder: '请输入规格名称(回车搜索)',
onPressEnter: () => gridReloadRef.value?.(),
},
fieldName: 'specName',
label: '规格名称',
},
],
showDefaultActions: true,
collapsed: true,
collapsedRows: 1,
}));
// 刷新表格数据
function refreshTable() {
if (gridApi) {
gridApi.reload();
}
}
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions: formOptions.value,
showSearchForm: true,
tableTitle: '商品管理',
});
gridReloadRef.value = () => gridApi.reload();
// 事件处理
const handleEdit = (product: ProductVO.Info) => {
emit('edit', product);
};
const handleDelete = (product: ProductVO.Info) => {
emit('delete', product);
};
// 暴露刷新方法
defineExpose({
refreshTable,
});
</script>
<template>
<Grid :loading="loading">
<!-- 主图列 -->
<template #mainImage="{ row }: { row: any }">
<Image
:src="row.mainImage"
:alt="row.name"
width="60"
height="60"
style="object-fit: cover; border-radius: 4px"
:preview="true"
/>
</template>
<!-- 规格数量列 -->
<template #specs="{ row }: { row: any }">
<!-- 有规格时显示悬停弹窗 -->
<Popover
v-if="row.specs && row.specs.length > 0"
placement="right"
trigger="hover"
>
<template #content>
<div style="max-width: 300px">
<div
style="
margin-bottom: 8px;
font-size: 14px;
font-weight: 500;
color: #374151;
"
>
规格详情
</div>
<div
v-for="spec in row.specs"
:key="spec.id"
style="
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
padding: 8px 12px;
background-color: #f9fafb;
border-radius: 4px;
font-size: 12px;
"
>
<div style="display: flex; flex-direction: column; gap: 4px">
<span style="font-weight: 500; color: #374151">
{{ spec.name }}
</span>
<span v-if="spec.specCode" style="color: #6b7280">
编码: {{ spec.specCode }}
</span>
</div>
<span
style="
margin-left: 16px;
font-weight: 600;
color: #ef4444;
white-space: nowrap;
"
>
¥{{ spec.productCost.toFixed(2) }}
</span>
</div>
</div>
</template>
<Tag color="blue" style="cursor: pointer">
{{ row.specs.length }} 个规格
</Tag>
</Popover>
<!-- 无规格时显示普通标签 -->
<Tag v-else color="default"> 0 个规格 </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>
</template>