fix: 拼多多商品标题按汉字2/ASCII1合计60字符限制

This commit is contained in:
2026-10-20 11:39:13 +08:00
parent cf08b393b7
commit 9bbb01fdee
8 changed files with 93 additions and 7 deletions
@@ -50,6 +50,11 @@ import { usePublishDraft } from './composables/use-publish-draft';
import { usePublishImages } from './composables/use-publish-images';
import { usePublishSku } from './composables/use-publish-sku';
import { usePublishSpecs } from './composables/use-publish-specs';
import {
PDD_GOODS_NAME_MAX_UNITS,
pddTitleCharUnits,
truncatePddTitle,
} from './utils/pdd-title-length';
defineOptions({ name: 'PddPublishEditor' });
@@ -363,6 +368,15 @@ function onSpecAxesListRef(el: HTMLElement | null) {
function onSkuTableWrapRef(el: HTMLElement | null) {
sku.skuTableWrapRef.value = el;
}
/** 标题:汉字算 2、ASCII 算 1,上限 60 */
function onGoodsNameChange(value: string) {
form.goodsName = truncatePddTitle(value);
}
function goodsNameCountFormatter(info: { value: string }) {
return `${pddTitleCharUnits(info.value)} / ${PDD_GOODS_NAME_MAX_UNITS}`;
}
</script>
<template>
@@ -461,7 +475,12 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
:class="{ 'agent-typing-field': agentTypingField === 'goodsName' }"
>
<Form.Item label="商品标题" required>
<Input v-model:value="form.goodsName" :maxlength="60" show-count />
<Input
:value="form.goodsName"
:show-count="{ formatter: goodsNameCountFormatter }"
placeholder="最多约 30 个汉字(60 字符)"
@update:value="onGoodsNameChange"
/>
</Form.Item>
</div>
<div
@@ -16,6 +16,7 @@ import {
effectiveValueUnits,
resolveCatPropControlKind,
} from '../utils/cat-property';
import { truncatePddTitle } from '../utils/pdd-title-length';
import { typewriterReplace } from '../utils/typewriter-text';
/** 智能体正在打字的表单焦点(用于高亮输入区) */
@@ -235,8 +236,8 @@ export function usePublishAgent(options: {
): Promise<ClientToolResult> {
const name = pickArgString(args, 'goodsName', 'goods_name');
if (!name) return { ok: false, message: '标题为空' };
// 与输入框 maxlength 对齐,避免受控 Input 截断后看似「没写上」
const next = name.slice(0, 60);
// 与拼多多标题计字对齐:汉字 2、ASCII 1,上限 60
const next = truncatePddTitle(name);
if (form.goodsName === next) {
return { ok: true, message: `标题未变:${next}` };
}
@@ -33,6 +33,11 @@ import {
seedDefaultPropUnits,
validateCatPropValue,
} from '../utils/cat-property';
import {
PDD_GOODS_NAME_MAX_UNITS,
pddTitleCharUnits,
truncatePddTitle,
} from '../utils/pdd-title-length';
type EmitDraft = {
(e: 'draft-saved', draftId: number): void;
@@ -163,6 +168,10 @@ export function usePublishDraft(options: {
message.warning('请填写商品标题');
return false;
}
if (pddTitleCharUnits(form.goodsName) > PDD_GOODS_NAME_MAX_UNITS) {
message.warning('商品标题超长(汉字约 30 个 / 60 字符)');
return false;
}
if (!form.catId) {
message.warning('请选择叶子类目');
return false;
@@ -401,10 +410,11 @@ export function usePublishDraft(options: {
loadSkuCandidates(),
]);
form.goodsName =
form.goodsName = truncatePddTitle(
(typeof es.goodsName === 'string' && es.goodsName) ||
draft.title ||
'';
draft.title ||
'',
);
if (typeof es.goodsDesc === 'string') form.goodsDesc = es.goodsDesc;
const catId =
@@ -0,0 +1,36 @@
/**
* 拼多多商品标题字数:汉字/非 ASCII 算 2,ASCII 算 1,上限 60(约 30 汉字)。
*/
export const PDD_GOODS_NAME_MAX_UNITS = 60;
/** 单个码点占用的平台字符单位 */
function charUnit(ch: string): number {
const cp = ch.codePointAt(0) ?? 0;
return cp <= 0xff ? 1 : 2;
}
/** 标题已占用的字符单位 */
export function pddTitleCharUnits(text: string): number {
let n = 0;
for (const ch of text ?? '') {
n += charUnit(ch);
}
return n;
}
/** 按单位上限截断,保证不超过 maxUnits */
export function truncatePddTitle(
text: string,
maxUnits: number = PDD_GOODS_NAME_MAX_UNITS,
): string {
let n = 0;
let out = '';
for (const ch of text ?? '') {
const w = charUnit(ch);
if (n + w > maxUnits) break;
out += ch;
n += w;
}
return out;
}