feat: 发布工作台改为保存/推送/发布时再创建系统草稿

This commit is contained in:
2026-09-28 10:11:57 +08:00
parent 92b6026b48
commit e136084794
5 changed files with 93 additions and 37 deletions
@@ -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<string | null> {
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();
},
@@ -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());
@@ -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<number, number>();
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) {
@@ -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<PublishEditorAgentApi | null>(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: {},
});
}
</script>
<template>
@@ -65,19 +85,17 @@ function goBack() {
</div>
</template>
<div
v-if="draftId"
class="flex min-h-0 flex-1 overflow-hidden"
>
<div class="flex min-h-0 flex-1 overflow-hidden">
<div class="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden">
<PddPublishEditor
ref="editorRef"
class="h-full min-h-0"
:open="true"
:draft-id="draftId"
:preset-pdd-shop-id="presetPddShopId"
@close="goBack"
@success="goBack"
@draft-saved="() => undefined"
@draft-saved="onDraftSaved"
/>
</div>
<div
@@ -23,7 +23,6 @@ import {
import {
clonePddPublishDeskDraftApi,
createPddPublishDeskDraftApi,
createPddPublishDeskJobApi,
deletePddPublishDeskDraftApi,
getPddPublishDeskJobApi,
@@ -315,18 +314,15 @@ function selectUnassigned() {
}
async function createDraft() {
try {
const draft = await createPddPublishDeskDraftApi({
title: '未命名草稿',
pddShopId: selectedShopId.value,
status: 'EDITING',
skuCount: 0,
});
message.success('已创建系统草稿');
await router.push(`/pdd/goods/publish/editor/${draft.id}`);
} catch (e: any) {
message.error(e?.message || '创建失败');
}
const query =
selectedShopId.value != null && selectedShopId.value > 0
? { shopId: String(selectedShopId.value) }
: undefined;
// 先进空编辑页,用户保存/推送/发布时再落系统草稿
await router.push({
path: '/pdd/goods/publish/editor/new',
query,
});
}
function openEdit(id: number) {