mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 发布助手可局部修改基础信息、SKU 价格库存和类目属性
This commit is contained in:
@@ -12,8 +12,9 @@ _LABELS: dict[str, str] = {
|
|||||||
"pdd_goods_search": "查找拼多多商品",
|
"pdd_goods_search": "查找拼多多商品",
|
||||||
"pdd_goods_detail": "读取拼多多商品",
|
"pdd_goods_detail": "读取拼多多商品",
|
||||||
"ui_get_publish_snapshot": "读取发布表单",
|
"ui_get_publish_snapshot": "读取发布表单",
|
||||||
"ui_set_goods_name": "填写商品标题",
|
"ui_patch_basic": "更新基础信息",
|
||||||
"ui_set_goods_desc": "填写商品描述",
|
"ui_patch_skus": "更新价格库存",
|
||||||
|
"ui_patch_props": "更新类目属性",
|
||||||
"ui_set_spec_axes": "设置规格轴",
|
"ui_set_spec_axes": "设置规格轴",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,12 +12,47 @@ from langchain_core.tools import StructuredTool
|
|||||||
from pydantic import BaseModel, Field
|
from pydantic import BaseModel, Field
|
||||||
|
|
||||||
|
|
||||||
class SetGoodsNameArgs(BaseModel):
|
class PatchBasicArgs(BaseModel):
|
||||||
goodsName: str = Field(description="商品标题,建议 60 字内")
|
"""只填要改的字段;未出现的字段保持页面原值。"""
|
||||||
|
|
||||||
|
goodsName: str | None = Field(default=None, description="商品标题,60 字内")
|
||||||
|
goodsDesc: str | None = Field(default=None, description="商品描述;空字符串表示清空")
|
||||||
|
marketPriceYuan: float | None = Field(default=None, description="参考价,元")
|
||||||
|
twoPiecesDiscount: float | None = Field(
|
||||||
|
default=None, description="满两件折扣,0–10,9.5 表示 9.5 折,0 表示取消"
|
||||||
|
)
|
||||||
|
shipmentLimitSecond: int | None = Field(
|
||||||
|
default=None, description="承诺发货秒数,只能是快照中的 86400 或 172800"
|
||||||
|
)
|
||||||
|
isFolt: bool | None = Field(default=None, description="是否假一赔十")
|
||||||
|
isRefundable: bool | None = Field(default=None, description="是否 7 天无理由")
|
||||||
|
isPreSale: bool | None = Field(default=None, description="是否预售")
|
||||||
|
secondHand: bool | None = Field(default=None, description="是否二手")
|
||||||
|
costTemplateId: int | None = Field(
|
||||||
|
default=None, description="运费模板 ID,必须来自快照 costTemplates"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class SetGoodsDescArgs(BaseModel):
|
class SkuPatch(BaseModel):
|
||||||
goodsDesc: str = Field(description="商品描述纯文本")
|
outSkuSn: str = Field(description="已有商家编码,勿编造")
|
||||||
|
priceYuan: float | None = Field(default=None, description="单买价,元")
|
||||||
|
multiPriceYuan: float | None = Field(default=None, description="拼单价,元")
|
||||||
|
quantity: int | None = Field(default=None, description="库存")
|
||||||
|
weight: int | None = Field(default=None, description="重量,克")
|
||||||
|
|
||||||
|
|
||||||
|
class PatchSkusArgs(BaseModel):
|
||||||
|
skus: list[SkuPatch] = Field(description="按商家编码局部更新,不增删行")
|
||||||
|
|
||||||
|
|
||||||
|
class PropPatch(BaseModel):
|
||||||
|
refPid: int = Field(description="类目属性 refPid,来自快照 goodsProperties")
|
||||||
|
value: str = Field(description="枚举传 vid 或选项文案;文本/数值传字面值。多选用英文逗号分隔")
|
||||||
|
unit: str | None = Field(default=None, description="单位,必须是该属性 units 中的一项")
|
||||||
|
|
||||||
|
|
||||||
|
class PatchPropsArgs(BaseModel):
|
||||||
|
props: list[PropPatch] = Field(description="只更新列出的类目属性")
|
||||||
|
|
||||||
|
|
||||||
class SpecAxis(BaseModel):
|
class SpecAxis(BaseModel):
|
||||||
@@ -55,11 +90,14 @@ def build_publish_client_tools(
|
|||||||
"""仅注册写工具;读快照已注入 system,避免再占一轮「读取发布表单」。"""
|
"""仅注册写工具;读快照已注入 system,避免再占一轮「读取发布表单」。"""
|
||||||
del form_snapshot # 快照由 system 注入,工具闭包不再依赖
|
del form_snapshot # 快照由 system 注入,工具闭包不再依赖
|
||||||
|
|
||||||
def set_goods_name(goodsName: str) -> str:
|
def patch_basic(**_kwargs: Any) -> str:
|
||||||
return f"已请求写入标题:{goodsName}"
|
return "已请求更新基础信息"
|
||||||
|
|
||||||
def set_goods_desc(goodsDesc: str) -> str:
|
def patch_skus(skus: list[Any]) -> str:
|
||||||
return "已请求写入商品描述"
|
return f"已请求更新 {len(skus or [])} 条 SKU"
|
||||||
|
|
||||||
|
def patch_props(props: list[Any]) -> str:
|
||||||
|
return f"已请求更新 {len(props or [])} 个类目属性"
|
||||||
|
|
||||||
def set_spec_axes(
|
def set_spec_axes(
|
||||||
axes: list[Any],
|
axes: list[Any],
|
||||||
@@ -73,16 +111,25 @@ def build_publish_client_tools(
|
|||||||
|
|
||||||
return [
|
return [
|
||||||
StructuredTool.from_function(
|
StructuredTool.from_function(
|
||||||
func=set_goods_name,
|
func=patch_basic,
|
||||||
name="ui_set_goods_name",
|
name="ui_patch_basic",
|
||||||
description="写入商品标题到发布表单。",
|
description=(
|
||||||
args_schema=SetGoodsNameArgs,
|
"局部更新标题、描述、参考价、满两件折扣、发货时限、"
|
||||||
|
"假一赔十/7天无理由/预售/二手、运费模板。未传的字段不要改。"
|
||||||
|
),
|
||||||
|
args_schema=PatchBasicArgs,
|
||||||
),
|
),
|
||||||
StructuredTool.from_function(
|
StructuredTool.from_function(
|
||||||
func=set_goods_desc,
|
func=patch_skus,
|
||||||
name="ui_set_goods_desc",
|
name="ui_patch_skus",
|
||||||
description="写入商品描述到发布表单。",
|
description="按已有商家编码局部更新单买价、拼单价、库存、重量。不增删 SKU,不改规格文案。",
|
||||||
args_schema=SetGoodsDescArgs,
|
args_schema=PatchSkusArgs,
|
||||||
|
),
|
||||||
|
StructuredTool.from_function(
|
||||||
|
func=patch_props,
|
||||||
|
name="ui_patch_props",
|
||||||
|
description="按 refPid 局部更新类目属性。枚举用快照里的 vid 或文案。",
|
||||||
|
args_schema=PatchPropsArgs,
|
||||||
),
|
),
|
||||||
StructuredTool.from_function(
|
StructuredTool.from_function(
|
||||||
func=set_spec_axes,
|
func=set_spec_axes,
|
||||||
@@ -97,5 +144,6 @@ def build_publish_client_tools(
|
|||||||
|
|
||||||
|
|
||||||
def is_client_tool_name(name: str) -> bool:
|
def is_client_tool_name(name: str) -> bool:
|
||||||
"""需前端执行的写工具(ui_set_*);读类工具不走 client_tool。"""
|
"""需前端执行的写工具;读类工具不走 client_tool。"""
|
||||||
return (name or "").startswith("ui_set_")
|
n = name or ""
|
||||||
|
return n.startswith("ui_set_") or n.startswith("ui_patch_")
|
||||||
|
|||||||
@@ -32,9 +32,13 @@ _BASE_SYSTEM = (
|
|||||||
|
|
||||||
_PUBLISH_SYSTEM = (
|
_PUBLISH_SYSTEM = (
|
||||||
"你是电商 DMB 的拼多多商品发布助手,只在当前系统草稿页改表。"
|
"你是电商 DMB 的拼多多商品发布助手,只在当前系统草稿页改表。"
|
||||||
"能写的只有三处:标题 ui_set_goods_name、描述 ui_set_goods_desc、规格轴 ui_set_spec_axes。"
|
"能写的工具:ui_patch_basic(标题、描述、参考价、满两件折扣、发货时限、"
|
||||||
"不能改店铺、类目、类目属性、图片、价格、库存、重量、参考价、折扣、运费和服务承诺;"
|
"假一赔十/7天无理由/预售/二手、运费模板)、ui_set_spec_axes(规格轴)、"
|
||||||
"用户要求这些时用一句话说明对话里改不了,不要假装已改。"
|
"ui_patch_skus(按 outSkuSn 改单买价、拼单价、库存、重量)、ui_patch_props(类目属性)。"
|
||||||
|
"每次只传要改的字段,未传的保持原样。"
|
||||||
|
"运费模板 ID 必须来自快照 costTemplates;发货时限只能用快照里的秒数;"
|
||||||
|
"类目属性枚举用 goodsProperties 里的 vid 或文案,多选英文逗号分隔。"
|
||||||
|
"不能改店铺、叶子类目、轮播图、详情图、SKU 缩略图;用户要求这些时用一句话说明对话里改不了。"
|
||||||
"【当前发布表单】是本轮开始时的页面内容。改表必须调用上述工具,不要只口头说已修改。"
|
"【当前发布表单】是本轮开始时的页面内容。改表必须调用上述工具,不要只口头说已修改。"
|
||||||
"工具返回是页面当时的写入结果:不以「写入失败」开头即已显示在编辑器里,无需用户刷新或再核对。"
|
"工具返回是页面当时的写入结果:不以「写入失败」开头即已显示在编辑器里,无需用户刷新或再核对。"
|
||||||
"以「写入失败」开头时只说明原因,不要声称已经改好。"
|
"以「写入失败」开头时只说明原因,不要声称已经改好。"
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ export type PublishSkuRowSnapshot = {
|
|||||||
skuName: string;
|
skuName: string;
|
||||||
quantity: number;
|
quantity: number;
|
||||||
weight: number;
|
weight: number;
|
||||||
|
priceYuan?: number;
|
||||||
|
multiPriceYuan?: number;
|
||||||
productName?: string;
|
productName?: string;
|
||||||
internalSkuName?: string;
|
internalSkuName?: string;
|
||||||
};
|
};
|
||||||
@@ -28,8 +30,29 @@ export type PublishFormSnapshot = {
|
|||||||
catId?: number;
|
catId?: number;
|
||||||
marketPriceYuan?: number;
|
marketPriceYuan?: number;
|
||||||
costTemplateId?: number;
|
costTemplateId?: number;
|
||||||
|
/** 当前店铺可选运费模板,写 costTemplateId 时只能用这里的 id */
|
||||||
|
costTemplates: Array<{ id: number; name: string }>;
|
||||||
shipmentLimitSecond?: number;
|
shipmentLimitSecond?: number;
|
||||||
|
/** 发货时限只允许这些秒数 */
|
||||||
|
shipmentLimitOptions: Array<{ seconds: number; label: string }>;
|
||||||
twoPiecesDiscount?: number;
|
twoPiecesDiscount?: number;
|
||||||
|
isFolt: boolean;
|
||||||
|
isPreSale: boolean;
|
||||||
|
isRefundable: boolean;
|
||||||
|
secondHand: boolean;
|
||||||
|
/** 当前可见的类目属性;枚举 options 可能截断 */
|
||||||
|
goodsProperties: Array<{
|
||||||
|
refPid: number;
|
||||||
|
name: string;
|
||||||
|
required?: boolean;
|
||||||
|
multiple?: boolean;
|
||||||
|
value?: string;
|
||||||
|
valueText?: string;
|
||||||
|
unit?: string;
|
||||||
|
units?: string[];
|
||||||
|
options?: Array<{ vid?: number; value?: string }>;
|
||||||
|
optionsTruncated?: boolean;
|
||||||
|
}>;
|
||||||
specAxes: PublishSpecAxisSnapshot[];
|
specAxes: PublishSpecAxisSnapshot[];
|
||||||
skuRows: PublishSkuRowSnapshot[];
|
skuRows: PublishSkuRowSnapshot[];
|
||||||
/** 当前类目可选规格维,便于模型选对轴名 */
|
/** 当前类目可选规格维,便于模型选对轴名 */
|
||||||
@@ -44,7 +67,7 @@ export type ClientToolResult = {
|
|||||||
/** 编辑器 defineExpose 契约 */
|
/** 编辑器 defineExpose 契约 */
|
||||||
export type PublishEditorAgentApi = {
|
export type PublishEditorAgentApi = {
|
||||||
getFormSnapshot: () => PublishFormSnapshot;
|
getFormSnapshot: () => PublishFormSnapshot;
|
||||||
/** 文本类工具会 await 逐字删写动效后再返回 */
|
/** 写入后立即返回;标题/描述的逐字动画在后台播放 */
|
||||||
applyClientTool: (
|
applyClientTool: (
|
||||||
name: string,
|
name: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
|
|||||||
@@ -187,6 +187,9 @@ const agent = usePublishAgent({
|
|||||||
syncSkuRowsFromSpecs: sku.syncSkuRowsFromSpecs,
|
syncSkuRowsFromSpecs: sku.syncSkuRowsFromSpecs,
|
||||||
applySkuAssignmentsByOutSkuSn: sku.applySkuAssignmentsByOutSkuSn,
|
applySkuAssignmentsByOutSkuSn: sku.applySkuAssignmentsByOutSkuSn,
|
||||||
revokeUploadFiles: images.revokeUploadFiles,
|
revokeUploadFiles: images.revokeUploadFiles,
|
||||||
|
logisticsOptions: category.logisticsOptions,
|
||||||
|
nonSaleProps: category.nonSaleProps,
|
||||||
|
loadCostTemplateDetail: category.loadCostTemplateDetail,
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
|
|||||||
+322
-2
@@ -1,5 +1,6 @@
|
|||||||
import type { Ref } from 'vue';
|
import { nextTick, ref, type Ref } from 'vue';
|
||||||
import { nextTick, ref } from 'vue';
|
|
||||||
|
import type { PddGoodsPublishVO } from '#/api/channel/pdd/goods';
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
ClientToolResult,
|
ClientToolResult,
|
||||||
@@ -11,6 +12,10 @@ import type {
|
|||||||
SpecCatalogItem,
|
SpecCatalogItem,
|
||||||
SkuRow,
|
SkuRow,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
import {
|
||||||
|
effectiveValueUnits,
|
||||||
|
resolveCatPropControlKind,
|
||||||
|
} from '../utils/cat-property';
|
||||||
import { typewriterReplace } from '../utils/typewriter-text';
|
import { typewriterReplace } from '../utils/typewriter-text';
|
||||||
|
|
||||||
/** 智能体正在打字的表单焦点(用于高亮输入区) */
|
/** 智能体正在打字的表单焦点(用于高亮输入区) */
|
||||||
@@ -41,6 +46,9 @@ export function usePublishAgent(options: {
|
|||||||
prevRows: SkuRow[],
|
prevRows: SkuRow[],
|
||||||
) => number;
|
) => number;
|
||||||
revokeUploadFiles: (files?: SkuRow['thumbFileList']) => void;
|
revokeUploadFiles: (files?: SkuRow['thumbFileList']) => void;
|
||||||
|
logisticsOptions: Ref<Array<{ label: string; value: number }>>;
|
||||||
|
nonSaleProps: Ref<PddGoodsPublishVO.CatProperty[]>;
|
||||||
|
loadCostTemplateDetail: (templateId?: number) => void;
|
||||||
}) {
|
}) {
|
||||||
const {
|
const {
|
||||||
form,
|
form,
|
||||||
@@ -57,6 +65,9 @@ export function usePublishAgent(options: {
|
|||||||
syncSkuRowsFromSpecs,
|
syncSkuRowsFromSpecs,
|
||||||
applySkuAssignmentsByOutSkuSn,
|
applySkuAssignmentsByOutSkuSn,
|
||||||
revokeUploadFiles,
|
revokeUploadFiles,
|
||||||
|
logisticsOptions,
|
||||||
|
nonSaleProps,
|
||||||
|
loadCostTemplateDetail,
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
/** 当前打字焦点,编辑器据此加高亮 */
|
/** 当前打字焦点,编辑器据此加高亮 */
|
||||||
@@ -133,6 +144,25 @@ export function usePublishAgent(options: {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SHIPMENT_OPTIONS = [
|
||||||
|
{ seconds: 86400, label: '24小时' },
|
||||||
|
{ seconds: 172800, label: '48小时' },
|
||||||
|
];
|
||||||
|
const PROP_OPTION_CAP = 40;
|
||||||
|
|
||||||
|
function propValueText(p: PddGoodsPublishVO.CatProperty, raw?: string) {
|
||||||
|
if (!raw) return '';
|
||||||
|
if (!p.values?.length) return raw;
|
||||||
|
return raw
|
||||||
|
.split(',')
|
||||||
|
.map((token) => {
|
||||||
|
const matched = p.values?.find((v) => String(v.vid) === token.trim());
|
||||||
|
return matched?.value || token.trim();
|
||||||
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
.join('、');
|
||||||
|
}
|
||||||
|
|
||||||
function getFormSnapshot(): PublishFormSnapshot {
|
function getFormSnapshot(): PublishFormSnapshot {
|
||||||
return {
|
return {
|
||||||
goodsName: form.goodsName,
|
goodsName: form.goodsName,
|
||||||
@@ -141,8 +171,41 @@ export function usePublishAgent(options: {
|
|||||||
catId: form.catId,
|
catId: form.catId,
|
||||||
marketPriceYuan: form.marketPriceYuan,
|
marketPriceYuan: form.marketPriceYuan,
|
||||||
costTemplateId: form.costTemplateId,
|
costTemplateId: form.costTemplateId,
|
||||||
|
costTemplates: logisticsOptions.value.map((t) => ({
|
||||||
|
id: t.value,
|
||||||
|
name: t.label,
|
||||||
|
})),
|
||||||
shipmentLimitSecond: form.shipmentLimitSecond,
|
shipmentLimitSecond: form.shipmentLimitSecond,
|
||||||
|
shipmentLimitOptions: SHIPMENT_OPTIONS,
|
||||||
twoPiecesDiscount: form.twoPiecesDiscount,
|
twoPiecesDiscount: form.twoPiecesDiscount,
|
||||||
|
isFolt: form.isFolt,
|
||||||
|
isPreSale: form.isPreSale,
|
||||||
|
isRefundable: form.isRefundable,
|
||||||
|
secondHand: form.secondHand,
|
||||||
|
goodsProperties: nonSaleProps.value
|
||||||
|
.filter((p) => p.refPid != null)
|
||||||
|
.map((p) => {
|
||||||
|
const key = String(p.refPid);
|
||||||
|
const raw = form.goodsPropValues[key];
|
||||||
|
const options = (p.values ?? []).slice(0, PROP_OPTION_CAP).map((v) => ({
|
||||||
|
vid: v.vid,
|
||||||
|
value: v.value,
|
||||||
|
}));
|
||||||
|
const units = effectiveValueUnits(p);
|
||||||
|
return {
|
||||||
|
refPid: p.refPid as number,
|
||||||
|
name: p.name || key,
|
||||||
|
required: p.required,
|
||||||
|
multiple: (p.chooseMaxNum ?? 1) > 1,
|
||||||
|
value: raw,
|
||||||
|
valueText: propValueText(p, raw),
|
||||||
|
unit: form.goodsPropUnits[key],
|
||||||
|
units: units.length ? units : undefined,
|
||||||
|
options: options.length ? options : undefined,
|
||||||
|
optionsTruncated:
|
||||||
|
(p.values?.length ?? 0) > PROP_OPTION_CAP ? true : undefined,
|
||||||
|
};
|
||||||
|
}),
|
||||||
specAxes: specAxes.value.map((a) => ({
|
specAxes: specAxes.value.map((a) => ({
|
||||||
name: a.name,
|
name: a.name,
|
||||||
catalogKey: a.catalogKey,
|
catalogKey: a.catalogKey,
|
||||||
@@ -155,6 +218,8 @@ export function usePublishAgent(options: {
|
|||||||
skuName: r.skuName,
|
skuName: r.skuName,
|
||||||
quantity: r.quantity,
|
quantity: r.quantity,
|
||||||
weight: r.weight,
|
weight: r.weight,
|
||||||
|
priceYuan: r.priceYuan,
|
||||||
|
multiPriceYuan: r.multiPriceYuan,
|
||||||
productName: r.productName,
|
productName: r.productName,
|
||||||
internalSkuName: r.internalSkuName,
|
internalSkuName: r.internalSkuName,
|
||||||
})),
|
})),
|
||||||
@@ -399,15 +464,270 @@ export function usePublishAgent(options: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function fieldPresent(args: Record<string, unknown>, key: string) {
|
||||||
|
return args[key] !== undefined && args[key] !== null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readBool(raw: unknown): boolean | undefined {
|
||||||
|
if (typeof raw === 'boolean') return raw;
|
||||||
|
if (raw === 'true' || raw === 1) return true;
|
||||||
|
if (raw === 'false' || raw === 0) return false;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function readNum(raw: unknown): number | undefined {
|
||||||
|
if (typeof raw === 'number' && Number.isFinite(raw)) return raw;
|
||||||
|
if (typeof raw === 'string' && raw.trim()) {
|
||||||
|
const n = Number(raw);
|
||||||
|
if (Number.isFinite(n)) return n;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function summarizePatch(applied: string[], errors: string[]): ClientToolResult {
|
||||||
|
if (applied.length === 0) {
|
||||||
|
return { ok: false, message: errors.join(';') || '没有要改的字段' };
|
||||||
|
}
|
||||||
|
const head = `已更新${applied.join('、')}`;
|
||||||
|
if (errors.length === 0) return { ok: true, message: head };
|
||||||
|
return { ok: true, message: `${head}。未写入:${errors.join(';')}` };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 局部更新标量字段;标题/描述动画不阻塞回执。 */
|
||||||
|
async function applyPatchBasic(
|
||||||
|
args: Record<string, unknown>,
|
||||||
|
): Promise<ClientToolResult> {
|
||||||
|
const applied: string[] = [];
|
||||||
|
const errors: string[] = [];
|
||||||
|
|
||||||
|
if (fieldPresent(args, 'goodsName')) {
|
||||||
|
const result = await applySetGoodsName(args);
|
||||||
|
if (result.ok) applied.push('标题');
|
||||||
|
else errors.push(result.message);
|
||||||
|
}
|
||||||
|
if (fieldPresent(args, 'goodsDesc') || fieldPresent(args, 'goods_desc')) {
|
||||||
|
const result = await applySetGoodsDesc(args);
|
||||||
|
if (result.ok) applied.push('描述');
|
||||||
|
else errors.push(result.message);
|
||||||
|
}
|
||||||
|
if (fieldPresent(args, 'marketPriceYuan')) {
|
||||||
|
const n = readNum(args.marketPriceYuan);
|
||||||
|
if (n == null || n < 0.01) errors.push('参考价不能小于 0.01');
|
||||||
|
else {
|
||||||
|
form.marketPriceYuan = Math.round(n * 100) / 100;
|
||||||
|
applied.push('参考价');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fieldPresent(args, 'twoPiecesDiscount')) {
|
||||||
|
const n = readNum(args.twoPiecesDiscount);
|
||||||
|
if (n == null || n < 0 || n > 10) errors.push('满两件折扣范围为 0-10');
|
||||||
|
else {
|
||||||
|
form.twoPiecesDiscount = Math.round(n * 10) / 10;
|
||||||
|
applied.push('满两件折扣');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fieldPresent(args, 'shipmentLimitSecond')) {
|
||||||
|
const n = readNum(args.shipmentLimitSecond);
|
||||||
|
if (n !== 86400 && n !== 172800) {
|
||||||
|
errors.push('发货时限只能是 86400(24小时)或 172800(48小时)');
|
||||||
|
} else {
|
||||||
|
form.shipmentLimitSecond = n;
|
||||||
|
applied.push('发货时限');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const flags: Array<[string, 'isFolt' | 'isRefundable' | 'isPreSale' | 'secondHand', string]> = [
|
||||||
|
['isFolt', 'isFolt', '假一赔十'],
|
||||||
|
['isRefundable', 'isRefundable', '7天无理由'],
|
||||||
|
['isPreSale', 'isPreSale', '预售'],
|
||||||
|
['secondHand', 'secondHand', '二手'],
|
||||||
|
];
|
||||||
|
for (const [key, field, label] of flags) {
|
||||||
|
if (!fieldPresent(args, key)) continue;
|
||||||
|
const v = readBool(args[key]);
|
||||||
|
if (v == null) {
|
||||||
|
errors.push(`${label}需要布尔值`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
form[field] = v;
|
||||||
|
applied.push(label);
|
||||||
|
}
|
||||||
|
if (fieldPresent(args, 'costTemplateId')) {
|
||||||
|
const id = readNum(args.costTemplateId);
|
||||||
|
const known = logisticsOptions.value.some((t) => t.value === id);
|
||||||
|
if (id == null || !known) errors.push('运费模板不在当前店铺可选列表');
|
||||||
|
else {
|
||||||
|
form.costTemplateId = id;
|
||||||
|
loadCostTemplateDetail(id);
|
||||||
|
applied.push('运费模板');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return summarizePatch(applied, errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
function asRecordList(raw: unknown): Array<Record<string, unknown>> {
|
||||||
|
if (!Array.isArray(raw)) return [];
|
||||||
|
return raw.filter(
|
||||||
|
(item): item is Record<string, unknown> =>
|
||||||
|
!!item && typeof item === 'object' && !Array.isArray(item),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 按商家编码改价/库存/重量,不增删行。 */
|
||||||
|
function applyPatchSkus(args: Record<string, unknown>): ClientToolResult {
|
||||||
|
const rows = asRecordList(args.skus);
|
||||||
|
if (rows.length === 0) return { ok: false, message: '没有要改的 SKU' };
|
||||||
|
const applied: string[] = [];
|
||||||
|
const errors: string[] = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
const sn = String(row.outSkuSn ?? row.out_sku_sn ?? '').trim();
|
||||||
|
const target = skuRows.value.find((r) => r.outSkuSn.trim() === sn);
|
||||||
|
if (!sn || !target) {
|
||||||
|
errors.push(`未找到编码 ${sn || '(空)'}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const patch: Partial<Pick<SkuRow, 'priceYuan' | 'multiPriceYuan' | 'quantity' | 'weight'>> = {};
|
||||||
|
let rowError = '';
|
||||||
|
const take = (
|
||||||
|
key: 'priceYuan' | 'multiPriceYuan' | 'quantity' | 'weight',
|
||||||
|
label: string,
|
||||||
|
min: number,
|
||||||
|
) => {
|
||||||
|
if (!fieldPresent(row, key) || rowError) return;
|
||||||
|
const n = readNum(row[key]);
|
||||||
|
if (n == null || n < min) {
|
||||||
|
rowError = `${sn} ${label}`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
patch[key] = key === 'priceYuan' || key === 'multiPriceYuan'
|
||||||
|
? Math.round(n * 100) / 100
|
||||||
|
: Math.round(n);
|
||||||
|
};
|
||||||
|
take('priceYuan', '单买价不能小于 0.01', 0.01);
|
||||||
|
take('multiPriceYuan', '拼单价不能小于 0.01', 0.01);
|
||||||
|
take('quantity', '库存不能为负', 0);
|
||||||
|
take('weight', '重量不能为负', 0);
|
||||||
|
if (rowError) {
|
||||||
|
errors.push(rowError);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (Object.keys(patch).length === 0) {
|
||||||
|
errors.push(`${sn} 没有要改的价格或库存`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Object.assign(target, patch);
|
||||||
|
applied.push(sn);
|
||||||
|
}
|
||||||
|
return summarizePatch(
|
||||||
|
applied.length ? [`SKU ${applied.length} 条`] : [],
|
||||||
|
errors,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function storePropValue(
|
||||||
|
p: PddGoodsPublishVO.CatProperty,
|
||||||
|
raw: string,
|
||||||
|
): { ok: true; stored: string } | { ok: false; message: string } {
|
||||||
|
const name = p.name || String(p.refPid);
|
||||||
|
const kind = resolveCatPropControlKind(p);
|
||||||
|
if (
|
||||||
|
kind === 'unsupported' ||
|
||||||
|
kind === 'material' ||
|
||||||
|
kind === 'product2' ||
|
||||||
|
kind === 'product3' ||
|
||||||
|
kind === 'range' ||
|
||||||
|
kind === 'dateRange'
|
||||||
|
) {
|
||||||
|
return { ok: false, message: `${name}暂不支持对话修改` };
|
||||||
|
}
|
||||||
|
if (kind === 'select' && p.values?.length) {
|
||||||
|
const tokens = raw
|
||||||
|
.split(/[,,]/)
|
||||||
|
.map((s) => s.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
const max = p.chooseMaxNum ?? 1;
|
||||||
|
if (tokens.length === 0) return { ok: false, message: `${name}取值为空` };
|
||||||
|
if (tokens.length > max) {
|
||||||
|
return { ok: false, message: `${name}最多选 ${max} 项` };
|
||||||
|
}
|
||||||
|
const vids: string[] = [];
|
||||||
|
for (const token of tokens) {
|
||||||
|
const matched = p.values.find(
|
||||||
|
(v) => String(v.vid) === token || v.value === token,
|
||||||
|
);
|
||||||
|
if (matched?.vid == null) {
|
||||||
|
return { ok: false, message: `${name}没有选项「${token}」` };
|
||||||
|
}
|
||||||
|
vids.push(String(matched.vid));
|
||||||
|
}
|
||||||
|
return { ok: true, stored: vids.join(',') };
|
||||||
|
}
|
||||||
|
if (kind === 'number') {
|
||||||
|
const n = Number(raw);
|
||||||
|
if (!Number.isFinite(n)) return { ok: false, message: `${name}不是数字` };
|
||||||
|
return { ok: true, stored: String(n) };
|
||||||
|
}
|
||||||
|
if (kind === 'date' && !/^\d{4}-\d{2}-\d{2}$/.test(raw)) {
|
||||||
|
return { ok: false, message: `${name}日期格式应为 YYYY-MM-DD` };
|
||||||
|
}
|
||||||
|
if (kind === 'month' && !/^\d{4}-\d{2}$/.test(raw)) {
|
||||||
|
return { ok: false, message: `${name}月份格式应为 YYYY-MM` };
|
||||||
|
}
|
||||||
|
if (kind === 'time' && !/^\d{2}:\d{2}:\d{2}$/.test(raw)) {
|
||||||
|
return { ok: false, message: `${name}时间格式应为 HH:mm:ss` };
|
||||||
|
}
|
||||||
|
return { ok: true, stored: raw };
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 按 refPid 改当前可见的类目属性。 */
|
||||||
|
function applyPatchProps(args: Record<string, unknown>): ClientToolResult {
|
||||||
|
const rows = asRecordList(args.props);
|
||||||
|
if (rows.length === 0) return { ok: false, message: '没有要改的类目属性' };
|
||||||
|
const applied: string[] = [];
|
||||||
|
const errors: string[] = [];
|
||||||
|
for (const row of rows) {
|
||||||
|
const refPid = readNum(row.refPid);
|
||||||
|
const prop = nonSaleProps.value.find((p) => p.refPid === refPid);
|
||||||
|
if (refPid == null || !prop) {
|
||||||
|
errors.push(`未找到属性 ${refPid ?? ''}`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const name = prop.name || String(refPid);
|
||||||
|
const raw = String(row.value ?? '').trim();
|
||||||
|
const stored = storePropValue(prop, raw);
|
||||||
|
if (!stored.ok) {
|
||||||
|
errors.push(stored.message);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (fieldPresent(row, 'unit')) {
|
||||||
|
const unit = String(row.unit ?? '').trim();
|
||||||
|
const units = effectiveValueUnits(prop);
|
||||||
|
if (unit && !units.includes(unit)) {
|
||||||
|
errors.push(`${name}的单位不在可选列表`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
form.goodsPropUnits[String(refPid)] = unit;
|
||||||
|
}
|
||||||
|
form.goodsPropValues[String(refPid)] = stored.stored;
|
||||||
|
applied.push(name);
|
||||||
|
}
|
||||||
|
return summarizePatch(applied, errors);
|
||||||
|
}
|
||||||
|
|
||||||
async function applyClientTool(
|
async function applyClientTool(
|
||||||
name: string,
|
name: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
): Promise<ClientToolResult> {
|
): Promise<ClientToolResult> {
|
||||||
switch (name) {
|
switch (name) {
|
||||||
|
case 'ui_patch_basic':
|
||||||
|
return applyPatchBasic(args);
|
||||||
case 'ui_set_goods_name':
|
case 'ui_set_goods_name':
|
||||||
return applySetGoodsName(args);
|
return applySetGoodsName(args);
|
||||||
case 'ui_set_goods_desc':
|
case 'ui_set_goods_desc':
|
||||||
return applySetGoodsDesc(args);
|
return applySetGoodsDesc(args);
|
||||||
|
case 'ui_patch_skus':
|
||||||
|
return applyPatchSkus(args);
|
||||||
|
case 'ui_patch_props':
|
||||||
|
return applyPatchProps(args);
|
||||||
case 'ui_set_spec_axes':
|
case 'ui_set_spec_axes':
|
||||||
return applySetSpecAxes(args);
|
return applySetSpecAxes(args);
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in New Issue
Block a user