mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 12:29:20 +08:00
419 lines
13 KiB
Vue
419 lines
13 KiB
Vue
<script lang="ts" setup>
|
||
import type { TreeProps } from 'ant-design-vue';
|
||
import type { PddCatVO } from '#/api/channel/pdd/cat';
|
||
|
||
import { computed, onMounted, onUnmounted, ref } from 'vue';
|
||
|
||
import { Page } from '@vben/common-ui';
|
||
import { createIconifyIcon } from '@vben/icons';
|
||
|
||
import {
|
||
Alert,
|
||
Button,
|
||
Input,
|
||
Modal,
|
||
Space,
|
||
Table,
|
||
Tag,
|
||
Tree,
|
||
message,
|
||
notification,
|
||
} from 'ant-design-vue';
|
||
|
||
import {
|
||
getPddCatSyncStatusApi,
|
||
listPddCatChildrenApi,
|
||
searchPddCatsApi,
|
||
syncPddCatsApi,
|
||
} from '#/api/channel/pdd/cat';
|
||
import { PermissionGate } from '#/components/permission';
|
||
|
||
defineOptions({ name: 'PddCatManagement' });
|
||
|
||
const RefreshIcon = createIconifyIcon('lucide:refresh-cw');
|
||
const SearchIcon = createIconifyIcon('lucide:search');
|
||
|
||
type TreeNode = {
|
||
key: number;
|
||
title: string;
|
||
isLeaf?: boolean;
|
||
children?: TreeNode[];
|
||
pathNames?: string;
|
||
level?: number;
|
||
};
|
||
|
||
const syncStatus = ref<null | PddCatVO.SyncStatus>(null);
|
||
const syncing = ref(false);
|
||
const treeLoading = ref(false);
|
||
const treeData = ref<TreeNode[]>([]);
|
||
const rootRows = ref<PddCatVO.Cat[]>([]);
|
||
const expandedKeys = ref<number[]>([]);
|
||
|
||
const searchKeyword = ref('');
|
||
const searchLoading = ref(false);
|
||
const searchRows = ref<PddCatVO.Cat[]>([]);
|
||
|
||
let pollTimer: null | ReturnType<typeof setInterval> = null;
|
||
|
||
const isRunning = computed(() => syncStatus.value?.status === 'RUNNING');
|
||
|
||
function mapTreeNode(c: PddCatVO.Cat): TreeNode {
|
||
// 同步未收尾时 is_leaf 可能尚未标记;先按非叶子懒加载,空子节点再标叶子
|
||
return {
|
||
key: c.catId,
|
||
title: `${c.catName || `类目${c.catId}`}(${c.catId})`,
|
||
isLeaf: !!c.leaf,
|
||
pathNames: c.pathNames,
|
||
level: c.level,
|
||
};
|
||
}
|
||
|
||
async function refreshSyncStatus() {
|
||
try {
|
||
const prev = syncStatus.value?.status;
|
||
syncStatus.value = await getPddCatSyncStatusApi();
|
||
if (syncStatus.value?.status === 'RUNNING') {
|
||
startPoll();
|
||
} else {
|
||
stopPoll();
|
||
if (prev === 'RUNNING' && syncStatus.value?.status === 'SUCCESS') {
|
||
await loadRoot();
|
||
const n = syncStatus.value.syncedCount ?? 0;
|
||
notification.success({
|
||
message: '拼多多类目同步完成',
|
||
description: `共入库 ${n} 条。发布商品现可使用本地跨层类目搜索;同步进行中或未成功时会自动降级为拼多多接口逐级选择。`,
|
||
duration: 10,
|
||
});
|
||
message.success(`类目同步完成,共 ${n} 条`);
|
||
}
|
||
}
|
||
} catch {
|
||
/* 忽略 */
|
||
}
|
||
}
|
||
|
||
function startPoll() {
|
||
if (pollTimer) return;
|
||
pollTimer = setInterval(() => {
|
||
void refreshSyncStatus();
|
||
}, 2000);
|
||
}
|
||
|
||
function stopPoll() {
|
||
if (pollTimer) {
|
||
clearInterval(pollTimer);
|
||
pollTimer = null;
|
||
}
|
||
}
|
||
|
||
async function handleSync() {
|
||
await refreshSyncStatus();
|
||
if (isRunning.value) {
|
||
message.warning('类目同步正在进行中');
|
||
return;
|
||
}
|
||
const hadSuccess = syncStatus.value?.status === 'SUCCESS';
|
||
const hasLocalData =
|
||
(syncStatus.value?.syncedCount ?? 0) > 0 || rootRows.value.length > 0;
|
||
// 已成功过或库内已有数据:全量重同步很慢,先确认
|
||
if (hadSuccess || hasLocalData) {
|
||
const countHint =
|
||
syncStatus.value?.syncedCount != null
|
||
? `当前约 ${syncStatus.value.syncedCount} 条`
|
||
: rootRows.value.length
|
||
? `当前一级类目 ${rootRows.value.length} 个`
|
||
: '本地已有类目数据';
|
||
Modal.confirm({
|
||
title: '确认重新全量同步类目?',
|
||
content: `${countHint}。全量同步通常需要较长时间(受拼多多限流影响可能更久)。同步进行中时,发布商品无法使用本地跨层类目搜索,将自动降级为拼多多接口逐级选择。是否仍要重新同步?`,
|
||
okText: '重新同步',
|
||
okType: 'primary',
|
||
cancelText: '取消',
|
||
centered: true,
|
||
onOk: () => doTriggerSync(),
|
||
});
|
||
return;
|
||
}
|
||
await doTriggerSync();
|
||
}
|
||
|
||
async function doTriggerSync() {
|
||
syncing.value = true;
|
||
try {
|
||
await syncPddCatsApi();
|
||
message.success(
|
||
'已开始同步拼多多类目;同步期间发布商品的类目搜索功能受限',
|
||
);
|
||
notification.info({
|
||
message: '类目同步进行中',
|
||
description:
|
||
'全量同步耗时较长。期间发布商品暂不可用本地跨层类目搜索,将自动降级为拼多多授权类目逐级加载。',
|
||
duration: 8,
|
||
});
|
||
await refreshSyncStatus();
|
||
startPoll();
|
||
} catch (e: any) {
|
||
message.error(e?.message || '触发同步失败');
|
||
} finally {
|
||
syncing.value = false;
|
||
}
|
||
}
|
||
|
||
async function loadRoot() {
|
||
treeLoading.value = true;
|
||
try {
|
||
const list = await listPddCatChildrenApi({ parentCatId: 0 });
|
||
rootRows.value = list;
|
||
treeData.value = list.map(mapTreeNode);
|
||
expandedKeys.value = [];
|
||
} catch (e: any) {
|
||
message.error(e?.message || '加载类目失败,请先同步');
|
||
rootRows.value = [];
|
||
treeData.value = [];
|
||
} finally {
|
||
treeLoading.value = false;
|
||
}
|
||
}
|
||
|
||
const onLoadData: TreeProps['loadData'] = (treeNode) => {
|
||
return new Promise<void>((resolve) => {
|
||
void (async () => {
|
||
const key = Number(treeNode.key);
|
||
if (treeNode.dataRef?.children) {
|
||
resolve();
|
||
return;
|
||
}
|
||
try {
|
||
const children = await listPddCatChildrenApi({ parentCatId: key });
|
||
const nodes = children.map(mapTreeNode);
|
||
if (treeNode.dataRef) {
|
||
// 无子节点则标为叶子,避免展开空白面板
|
||
treeNode.dataRef.isLeaf = nodes.length === 0;
|
||
treeNode.dataRef.children = nodes;
|
||
}
|
||
treeData.value = [...treeData.value];
|
||
} catch (e: any) {
|
||
message.error(e?.message || '加载子类目失败');
|
||
} finally {
|
||
resolve();
|
||
}
|
||
})();
|
||
});
|
||
};
|
||
|
||
async function handleSearch() {
|
||
const kw = searchKeyword.value.trim();
|
||
if (!kw) {
|
||
searchRows.value = [];
|
||
return;
|
||
}
|
||
searchLoading.value = true;
|
||
try {
|
||
searchRows.value = await searchPddCatsApi({
|
||
keyword: kw,
|
||
leafOnly: false,
|
||
limit: 80,
|
||
});
|
||
} catch (e: any) {
|
||
message.error(e?.message || '搜索失败');
|
||
searchRows.value = [];
|
||
} finally {
|
||
searchLoading.value = false;
|
||
}
|
||
}
|
||
|
||
const rootColumns = [
|
||
{ title: '类目ID', dataIndex: 'catId', width: 120 },
|
||
{ title: '名称', dataIndex: 'catName', width: 220 },
|
||
{ title: '层级', dataIndex: 'level', width: 72 },
|
||
{ title: '路径', dataIndex: 'pathNames', ellipsis: true },
|
||
];
|
||
|
||
const searchColumns = [
|
||
{ title: '类目ID', dataIndex: 'catId', width: 120 },
|
||
{ title: '名称', dataIndex: 'catName', width: 160 },
|
||
{ title: '层级', dataIndex: 'level', width: 72 },
|
||
{
|
||
title: '叶子',
|
||
dataIndex: 'leaf',
|
||
width: 72,
|
||
customRender: ({ text }: { text: boolean }) => (text ? '是' : '否'),
|
||
},
|
||
{ title: '完整路径', dataIndex: 'pathNames', ellipsis: true },
|
||
];
|
||
|
||
onMounted(async () => {
|
||
await refreshSyncStatus();
|
||
await loadRoot();
|
||
});
|
||
|
||
onUnmounted(() => stopPoll());
|
||
</script>
|
||
|
||
<template>
|
||
<Page
|
||
title="拼多多类目"
|
||
description="平台标准类目本地缓存;发布商品时可按名称跨层级搜索,无需每次请求拼多多"
|
||
>
|
||
<div class="space-y-4">
|
||
<Alert
|
||
type="info"
|
||
show-icon
|
||
message="类目为拼多多平台级全量树(pdd.goods.cats.get),与店铺授权可发范围无关。发布时仍以平台校验为准。"
|
||
/>
|
||
|
||
<Alert
|
||
v-if="isRunning"
|
||
type="warning"
|
||
show-icon
|
||
message="全量同步进行中:触达拼多多限流时会自动等待,进度可能间歇停顿,属正常现象。已入库数据可在本页浏览/搜索;发布商品暂不可用本地跨层搜索,将自动降级为拼多多授权类目接口。"
|
||
/>
|
||
|
||
<div
|
||
class="flex flex-wrap items-center justify-between gap-3 rounded-xl border border-slate-200/80 bg-white p-4 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||
>
|
||
<div class="min-w-0 text-sm text-slate-600 dark:text-slate-300">
|
||
<div class="flex flex-wrap items-center gap-2">
|
||
<span class="font-medium text-slate-800 dark:text-slate-100">
|
||
同步状态
|
||
</span>
|
||
<Tag v-if="isRunning" color="processing">同步中</Tag>
|
||
<Tag
|
||
v-else-if="syncStatus?.status === 'SUCCESS'"
|
||
color="success"
|
||
>
|
||
成功
|
||
</Tag>
|
||
<Tag
|
||
v-else-if="syncStatus?.status === 'FAILED'"
|
||
color="error"
|
||
>
|
||
失败
|
||
</Tag>
|
||
<Tag v-else color="default">未同步 / 空闲</Tag>
|
||
</div>
|
||
<p class="mb-0 mt-1 text-xs text-slate-500">
|
||
<template v-if="syncStatus?.syncedCount != null">
|
||
已入库 {{ syncStatus.syncedCount }} 条
|
||
</template>
|
||
<template v-if="rootRows.length">
|
||
· 一级类目 {{ rootRows.length }} 个
|
||
</template>
|
||
<template v-if="syncStatus?.startedAt && isRunning">
|
||
· 开始于 {{ syncStatus.startedAt }}
|
||
</template>
|
||
<template v-if="syncStatus?.finishedAt && !isRunning">
|
||
· 完成于 {{ syncStatus.finishedAt }}
|
||
</template>
|
||
<template v-if="syncStatus?.lastError">
|
||
· {{ syncStatus.lastError }}
|
||
</template>
|
||
<template v-if="syncStatus?.message">
|
||
· {{ syncStatus.message }}
|
||
</template>
|
||
</p>
|
||
</div>
|
||
<Space wrap>
|
||
<Button
|
||
@click="
|
||
() => {
|
||
void refreshSyncStatus();
|
||
void loadRoot();
|
||
}
|
||
"
|
||
>
|
||
<template #icon>
|
||
<component :is="RefreshIcon" class="size-4" />
|
||
</template>
|
||
刷新
|
||
</Button>
|
||
<PermissionGate code="channel:pdd:cat:sync">
|
||
<Button
|
||
type="primary"
|
||
:loading="syncing || isRunning"
|
||
:disabled="isRunning"
|
||
@click="handleSync"
|
||
>
|
||
同步类目
|
||
</Button>
|
||
</PermissionGate>
|
||
</Space>
|
||
</div>
|
||
|
||
<div
|
||
class="rounded-xl border border-slate-200/80 bg-white p-4 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||
>
|
||
<h3 class="mb-3 text-sm font-semibold text-slate-800 dark:text-slate-100">
|
||
一级类目({{ rootRows.length }})
|
||
</h3>
|
||
<Table
|
||
size="small"
|
||
row-key="catId"
|
||
:loading="treeLoading"
|
||
:columns="rootColumns"
|
||
:data-source="rootRows"
|
||
:pagination="{ pageSize: 20, hideOnSinglePage: true }"
|
||
:locale="{ emptyText: isRunning ? '同步中,稍候刷新…' : '暂无数据,请先同步' }"
|
||
/>
|
||
</div>
|
||
|
||
<div
|
||
class="rounded-xl border border-slate-200/80 bg-white p-4 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||
>
|
||
<h3 class="mb-3 text-sm font-semibold text-slate-800 dark:text-slate-100">
|
||
类目树浏览
|
||
</h3>
|
||
<div
|
||
v-if="treeData.length"
|
||
class="max-h-[420px] min-h-0 overflow-y-auto rounded-lg border border-slate-100 p-2 dark:border-slate-700"
|
||
>
|
||
<Tree
|
||
v-model:expanded-keys="expandedKeys"
|
||
:tree-data="treeData"
|
||
:load-data="onLoadData"
|
||
:show-line="{ showLeafIcon: false }"
|
||
/>
|
||
</div>
|
||
<p
|
||
v-else
|
||
class="mb-0 text-sm text-slate-500"
|
||
>
|
||
{{ treeLoading ? '加载中…' : isRunning ? '同步中,稍候刷新…' : '暂无类目数据' }}
|
||
</p>
|
||
</div>
|
||
|
||
<div
|
||
class="rounded-xl border border-slate-200/80 bg-white p-4 dark:border-[hsl(240deg_3.7%_22%)] dark:bg-[hsl(222.34deg_10.43%_12.27%)]"
|
||
>
|
||
<h3 class="mb-3 text-sm font-semibold text-slate-800 dark:text-slate-100">
|
||
跨层级搜索
|
||
</h3>
|
||
<Space wrap class="mb-3">
|
||
<Input
|
||
v-model:value="searchKeyword"
|
||
class="!w-72"
|
||
allow-clear
|
||
placeholder="类目名称或路径关键词"
|
||
@press-enter="handleSearch"
|
||
/>
|
||
<Button type="primary" :loading="searchLoading" @click="handleSearch">
|
||
<template #icon>
|
||
<component :is="SearchIcon" class="size-4" />
|
||
</template>
|
||
搜索
|
||
</Button>
|
||
</Space>
|
||
<Table
|
||
size="small"
|
||
row-key="catId"
|
||
:loading="searchLoading"
|
||
:columns="searchColumns"
|
||
:data-source="searchRows"
|
||
:pagination="{ pageSize: 20, hideOnSinglePage: true }"
|
||
:locale="{ emptyText: '输入关键词后搜索' }"
|
||
/>
|
||
</div>
|
||
</div>
|
||
</Page>
|
||
</template>
|