Files
ecom-dmb/ecom-dmb-frontend/apps/web-antd/src/views/channel/pdd/goods/index.vue
T

432 lines
11 KiB
Vue

<script lang="ts" setup>
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { PddGoodsDTO, PddGoodsVO } from '#/api/channel/pdd/goods';
import type { PddShopVO } from '#/api/channel/pdd/shop';
import type { PageQueryDTO } from '#/types/common';
import { computed, onMounted, ref, watch } from 'vue';
import { Button, Card, Image, Popconfirm, Space, Tag, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deletePddGoodsApi,
getPddGoodsPageApi,
} from '#/api/channel/pdd/goods';
import { getPddShopPageApi } from '#/api/channel/pdd/shop';
import { PermissionGate } from '#/components/permission';
import PddShopColPage from '#/views/channel/pdd/components/PddShopColPage.vue';
import PddShopDetailModal from '#/views/channel/pdd/shop/components/PddShopDetailModal.vue';
import ProductDetailDrawer from '#/views/ec/product/list/components/ProductDetailDrawer.vue';
import PddGoodsDetailDrawer from './components/PddGoodsDetailDrawer.vue';
defineOptions({ name: 'PddGoodsManagement' });
const loading = ref(false);
const detailDrawerRef = ref<InstanceType<typeof PddGoodsDetailDrawer>>();
const shopDetailModalRef = ref<InstanceType<typeof PddShopDetailModal>>();
const productDetailDrawerRef = ref<InstanceType<typeof ProductDetailDrawer>>();
/** 左侧选中的拼多多渠道店铺 ID */
const selectedShopId = ref<number | undefined>(undefined);
const shopNameById = ref<Record<number, string>>({});
const searchForm = ref({
shopId: undefined as number | undefined,
goodsName: '',
goodsId: '',
isOnsale: undefined as number | undefined,
});
const gridReloadRef = ref<null | (() => void)>(null);
async function loadShopNames() {
try {
const data = await getPddShopPageApi({
page: 1,
pageSize: 500,
filters: {},
});
const map: Record<number, string> = {};
for (const s of data.records ?? []) {
map[s.id] = s.mallName || `店铺#${s.id}`;
}
shopNameById.value = map;
} catch {
message.error('获取渠道店铺失败');
}
}
function onShopSelect(shop: PddShopVO.Info | null) {
if (!shop) {
selectedShopId.value = undefined;
searchForm.value.shopId = undefined;
return;
}
selectedShopId.value = shop.id;
searchForm.value.shopId = shop.id;
shopNameById.value[shop.id] = shop.mallName || `店铺#${shop.id}`;
}
const gridOptions: VxeGridProps<PddGoodsVO.Info> = {
columns: [
{ type: 'seq' as const, width: 56, title: '序号' },
{
title: '主图',
width: 80,
align: 'center' as const,
showOverflow: false,
slots: { default: 'thumb' },
},
{
title: '商品名称',
minWidth: 200,
showOverflow: 'tooltip' as const,
slots: { default: 'goodsName' },
},
{
title: '渠道店铺',
minWidth: 140,
showOverflow: 'tooltip' as const,
slots: { default: 'shop' },
},
{
title: '内部商品',
minWidth: 140,
showOverflow: 'tooltip' as const,
slots: { default: 'productName' },
},
{
title: '状态',
width: 90,
slots: { default: 'onsale' },
},
{
title: 'SKU',
width: 80,
slots: { default: 'moreSku' },
},
{
field: 'goodsQuantity',
title: '库存',
width: 80,
},
{
field: 'pddCreatedAt',
title: '拼多多创建时间',
minWidth: 160,
},
{
field: 'lastSyncTime',
title: '最近同步',
minWidth: 160,
},
{
field: 'updateTime',
title: '更新时间',
minWidth: 160,
},
{
title: '操作',
minWidth: 140,
fixed: 'right' as const,
showOverflow: false,
slots: { default: 'action' },
},
],
height: '100%',
border: true,
stripe: true,
rowConfig: { isHover: true, height: 72 },
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 }: any) => {
try {
loading.value = true;
const params: PageQueryDTO<PddGoodsDTO.Filters> = {
page: page.currentPage,
pageSize: page.pageSize,
filters: {
shopId: searchForm.value.shopId,
goodsName: searchForm.value.goodsName || undefined,
goodsId: searchForm.value.goodsId || undefined,
isOnsale: searchForm.value.isOnsale,
},
};
const data = await getPddGoodsPageApi(params);
return {
items: data.records ?? [],
total: data.total ?? 0,
};
} catch (error) {
console.error('获取拼多多商品失败:', error);
message.error('获取拼多多商品失败');
return { items: [], total: 0 };
} finally {
loading.value = false;
}
},
},
},
};
const formOptions = computed(() => ({
schema: [
{
component: 'Input',
componentProps: {
placeholder: '商品名称(模糊)',
allowClear: true,
onChange: (e: any) => {
searchForm.value.goodsName = e?.target?.value ?? '';
},
onPressEnter: () => gridReloadRef.value?.(),
},
fieldName: 'goodsName',
label: '商品名称',
},
{
component: 'Input',
componentProps: {
placeholder: '拼多多商品ID',
allowClear: true,
onChange: (e: any) => {
searchForm.value.goodsId = e?.target?.value ?? '';
},
onPressEnter: () => gridReloadRef.value?.(),
},
fieldName: 'goodsId',
label: 'goods_id',
},
{
component: 'Select',
componentProps: {
placeholder: '全部',
allowClear: true,
options: [
{ label: '在架', value: 1 },
{ label: '下架', value: 0 },
],
onChange: (val: number | undefined) => {
searchForm.value.isOnsale = val;
gridReloadRef.value?.();
},
},
fieldName: 'isOnsale',
label: '在架状态',
},
],
submitButtonOptions: { content: '搜索' },
resetButtonOptions: { content: '重置' },
handleSubmit: async () => {
gridReloadRef.value?.();
},
handleReset: async () => {
searchForm.value = {
shopId: undefined,
goodsName: '',
goodsId: '',
isOnsale: undefined,
};
gridReloadRef.value?.();
},
collapsed: true,
collapsedRows: 1,
}));
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
formOptions: formOptions.value,
showSearchForm: true,
tableTitle: '',
});
gridReloadRef.value = () => gridApi.reload();
watch(selectedShopId, () => {
gridApi.reload();
});
function openDetail(row: PddGoodsVO.Info) {
const shopLabel = row.shopId
? shopNameById.value[row.shopId]
: undefined;
detailDrawerRef.value?.open(row.id, shopLabel);
}
function openShopDetail(row: PddGoodsVO.Info) {
if (!row.shopId) {
message.warning('该商品未关联渠道店铺');
return;
}
shopDetailModalRef.value?.open(row.shopId);
}
async function openInternalProduct(row: PddGoodsVO.Info) {
if (!row.productId) {
message.warning('未关联内部商品');
return;
}
await productDetailDrawerRef.value?.openById(row.productId, row.productName);
}
async function handleDelete(row: PddGoodsVO.Info) {
try {
await deletePddGoodsApi(row.id);
message.success('已删除');
gridApi.reload();
} catch (error: any) {
message.error(error?.message || '删除失败');
}
}
onMounted(async () => {
await loadShopNames();
});
</script>
<template>
<PddShopColPage :selected-shop-id="selectedShopId" @select="onShopSelect">
<Card
class="ml-1 flex h-full min-h-0 min-w-0 flex-1 flex-col sm:ml-2"
:body-style="{
height: '100%',
minHeight: 0,
padding: 0,
display: 'flex',
flexDirection: 'column',
}"
>
<template #title>
<span class="text-base font-semibold">拼多多商品</span>
</template>
<div
class="min-h-0 flex-1 overflow-hidden px-2 pb-2 pt-2 sm:px-3 sm:pb-3"
>
<Grid class="pdd-goods-grid h-full min-h-0" :loading="loading">
<template #thumb="{ row }: { row: PddGoodsVO.Info }">
<Image
v-if="row.thumbUrl || row.imageUrl"
:src="row.thumbUrl || row.imageUrl"
:width="40"
:height="40"
class="goods-thumb"
/>
<span v-else class="text-slate-400">-</span>
</template>
<template #goodsName="{ row }: { row: PddGoodsVO.Info }">
<Button type="link" size="small" class="!px-0" @click="openDetail(row)">
{{ row.goodsName || '-' }}
</Button>
</template>
<template #productName="{ row }: { row: PddGoodsVO.Info }">
<Button
v-if="row.productId"
type="link"
size="small"
class="!px-0"
@click="openInternalProduct(row)"
>
{{ row.productName || `商品#${row.productId}` }}
</Button>
<span v-else class="text-slate-400">-</span>
</template>
<template #shop="{ row }: { row: PddGoodsVO.Info }">
<Button
v-if="row.shopId"
type="link"
size="small"
class="!px-0"
@click="openShopDetail(row)"
>
{{ shopNameById[row.shopId] || `渠道店#${row.shopId}` }}
</Button>
<span v-else>-</span>
</template>
<template #onsale="{ row }: { row: PddGoodsVO.Info }">
<Tag :color="row.isOnsale === 1 ? 'success' : 'default'">
{{ row.isOnsale === 1 ? '在架' : '下架' }}
</Tag>
</template>
<template #moreSku="{ row }: { row: PddGoodsVO.Info }">
<Tag :color="row.isMoreSku === 1 ? 'blue' : 'default'">
{{ row.isMoreSku === 1 ? '多SKU' : '单SKU' }}
</Tag>
</template>
<template #action="{ row }: { row: PddGoodsVO.Info }">
<Space size="small" :wrap="false">
<Button type="link" size="small" @click="openDetail(row)">
详情
</Button>
<PermissionGate code="channel:pdd:goods:delete">
<Popconfirm
title="确定删除该商品及全部 SKU 吗?"
@confirm="handleDelete(row)"
>
<Button type="link" size="small" danger>删除</Button>
</Popconfirm>
</PermissionGate>
</Space>
</template>
</Grid>
</div>
</Card>
<PddGoodsDetailDrawer
ref="detailDrawerRef"
@open-internal-product="openInternalProduct"
/>
<PddShopDetailModal ref="shopDetailModalRef" />
<ProductDetailDrawer ref="productDetailDrawerRef" />
</PddShopColPage>
</template>
<style scoped>
.goods-thumb {
border-radius: 6px;
object-fit: cover;
}
:deep(.pdd-goods-grid .vxe-body--column) {
vertical-align: middle;
}
</style>