diff --git a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/agent/PddPublishAgentPanel.vue b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/agent/PddPublishAgentPanel.vue index b4c4f812..a95288ab 100644 --- a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/agent/PddPublishAgentPanel.vue +++ b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/agent/PddPublishAgentPanel.vue @@ -35,7 +35,8 @@ const PanelCloseIcon = createIconifyIcon('lucide:panel-left-close'); const PanelOpenIcon = createIconifyIcon('lucide:panel-left'); const props = defineProps<{ - draftId: number; + /** 系统草稿 ID;null 表示尚未落库的新建编辑 */ + draftId: number | null; collapsed?: boolean; /** 编辑器命令面(ref) */ editorApi: PublishEditorAgentApi | null; @@ -48,6 +49,25 @@ const emit = defineEmits<{ const SCENE = 'ecom_goods_publish' as const; +/** 未落库时用本地 sceneRef,落库后切到草稿 ID */ +const localSceneKey = `local-${ + typeof crypto !== 'undefined' && crypto.randomUUID + ? crypto.randomUUID() + : `${Date.now()}-${Math.random().toString(36).slice(2)}` +}`; + +const sceneRef = computed(() => + props.draftId != null && props.draftId > 0 + ? String(props.draftId) + : localSceneKey, +); + +const sessionTitle = computed(() => + props.draftId != null && props.draftId > 0 + ? `发布草稿 #${props.draftId}` + : '发布草稿(未保存)', +); + const starterPrompts = [ { label: '按编码生成标题', @@ -222,11 +242,11 @@ async function ensureSession(): Promise { if (session.value?.threadId) return session.value.threadId; const parsed = parseModelKey(selectedModelKey.value); const s = await createAgentSessionApi({ - title: `发布草稿 #${props.draftId}`, + title: sessionTitle.value, llmProviderId: parsed?.providerId, llmModel: parsed?.model, scene: SCENE, - sceneRef: String(props.draftId), + sceneRef: sceneRef.value, reuseBySceneRef: true, }); session.value = s; @@ -275,7 +295,11 @@ async function bootstrap() { watch( () => props.draftId, - () => { + (id, prev) => { + // 新建落库后仅 URL 从 new→id:沿用当前会话,避免打断 + if (prev == null && id != null && id > 0 && session.value?.threadId) { + return; + } abort?.abort(); void bootstrap(); }, diff --git a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/PddPublishEditor.vue b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/PddPublishEditor.vue index 12b1957c..c79804b4 100644 --- a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/PddPublishEditor.vue +++ b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/PddPublishEditor.vue @@ -255,6 +255,24 @@ onMounted(() => { } }); +/** 路由 draftId 变化(含 new→真实 ID):已绑定同一草稿则跳过 hydrate */ +watch( + () => props.draftId, + (id, prev) => { + if ( + (prev == null || prev === undefined) && + id != null && + id > 0 && + draft.boundDraftId.value === id + ) { + return; + } + if (id != null && id > 0 && id !== draft.boundDraftId.value) { + void draft.hydrateFromDraft(id); + } + }, +); + function resetAll() { draft.submitting.value = false; Object.assign(form, createEmptyPublishForm()); diff --git a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/composables/use-publish-draft.ts b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/composables/use-publish-draft.ts index ce20b1e0..6b35ae2d 100644 --- a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/composables/use-publish-draft.ts +++ b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/composables/use-publish-draft.ts @@ -779,8 +779,12 @@ export function usePublishDraft(options: { async function handleSubmit() { if (!validateForm()) return; submitting.value = true; - const hide = message.loading('正在上传图片并发布…', 0); + const hide = message.loading('正在保存并发布…', 0); try { + // 先落/更新系统草稿(含 publishPayload),再发布以便回写 goods_commit_id / goods_id + const saved = await saveDeskDraft(true, { silentSuccess: true }); + if (!saved?.id) return; + const pendingThumbs = skuRows.value .map((row) => ({ rowKey: row.key, file: row.thumbFileList[0] })) .filter((x): x is { rowKey: number; file: UploadFile } => !!x.file); @@ -799,8 +803,8 @@ export function usePublishDraft(options: { const thumbPersisted = persistedAll.slice(p); const thumbFileIdBySkuKey = new Map(); pendingThumbs.forEach((item, i) => { - const saved = thumbPersisted[i]; - if (saved) thumbFileIdBySkuKey.set(item.rowKey, saved.fileId); + const savedThumb = thumbPersisted[i]; + if (savedThumb) thumbFileIdBySkuKey.set(item.rowKey, savedThumb.fileId); }); const payload = await buildPublishPayload( @@ -808,17 +812,13 @@ export function usePublishDraft(options: { detailFileIds, thumbFileIdBySkuKey, ); - // 确保有系统草稿并回传 ID,发布结果才能写入 goods_commit_id - if (boundDraftId.value) { - payload.systemDraftId = boundDraftId.value; - } else if (form.pddShopId) { - const saved = await saveDeskDraft(true, { silentSuccess: true }); - if (saved?.id) { - payload.systemDraftId = saved.id; - } - } + payload.systemDraftId = saved.id; const res = await publishPddGoodsApi(payload); - message.success(`发布成功,goodsId=${res.goodsId}`); + message.success( + res.goodsId != null + ? `发布成功,goodsId=${res.goodsId}` + : '发布成功', + ); emit('success'); closeEditor(); } catch (e: any) { diff --git a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/index.vue b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/index.vue index 2a9c0315..6cc1d921 100644 --- a/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/index.vue +++ b/apps/web/apps/web-antd/src/views/channel/pdd/goods/publish/editor/index.vue @@ -2,6 +2,7 @@ /** * 发布编辑页路由入口(editor/index):Page 布局 + 左侧表单 + 右侧 Agent。 * 表单实现见同目录 PddPublishEditor;智能体见 ../agent。 + * draftId=new 表示尚未落库的空编辑;保存/推送/发布时再创建系统草稿。 */ import { computed, ref } from 'vue'; import { useRoute, useRouter } from 'vue-router'; @@ -19,10 +20,20 @@ defineOptions({ name: 'PddGoodsPublishDeskEdit' }); const route = useRoute(); const router = useRouter(); +/** 数字 ID;new / 非法 → null(尚未创建系统草稿) */ const draftId = computed(() => { const raw = route.params.draftId; - const n = Number(Array.isArray(raw) ? raw[0] : raw); - return Number.isFinite(n) ? n : null; + const s = String(Array.isArray(raw) ? raw[0] : raw ?? ''); + if (!s || s === 'new') return null; + const n = Number(s); + return Number.isFinite(n) && n > 0 ? n : null; +}); + +const presetPddShopId = computed(() => { + const raw = route.query.shopId; + const s = String(Array.isArray(raw) ? raw[0] : raw ?? ''); + const n = Number(s); + return Number.isFinite(n) && n > 0 ? n : null; }); const editorRef = ref(null); @@ -47,6 +58,15 @@ const agentPaneStyle = computed(() => { function goBack() { void router.push('/pdd/goods/publish'); } + +/** 首次落库后把 URL 换成真实草稿 ID,便于刷新续编与 Agent 绑定 */ +function onDraftSaved(id: number) { + if (!id || draftId.value === id) return; + void router.replace({ + path: `/pdd/goods/publish/editor/${id}`, + query: {}, + }); +} -
+
0 + ? { shopId: String(selectedShopId.value) } + : undefined; + // 先进空编辑页,用户保存/推送/发布时再落系统草稿 + await router.push({ + path: '/pdd/goods/publish/editor/new', + query, + }); } function openEdit(id: number) {