diff --git a/components/UploadModal.tsx b/components/UploadModal.tsx index 5978efbb..a67c6c8a 100644 --- a/components/UploadModal.tsx +++ b/components/UploadModal.tsx @@ -69,7 +69,7 @@ import { validateImageFiles, } from '@/components/upload/drag-drop-utils' import { getDingtalkApprovalNoError } from '@/lib/dingtalk-approval-no' -import { isPlausibleSpuProductModel } from '@/lib/product-model' +import { isPlausibleSpuProductModel, sanitizeSpuProductModelInput } from '@/lib/product-model' interface UploadModalProps { onClose: () => void @@ -2077,7 +2077,7 @@ export default function UploadModal({ onClose, onSuccess }: UploadModalProps) { newFiles[index].productModel = value newFiles[index].productModelSource = 'manual' // 用户手动输入 } else { - processedValue = value.replace(/[^a-zA-Z0-9\-_.]/g, '').toUpperCase() + processedValue = sanitizeSpuProductModelInput(value) newFiles[index].productModel = processedValue newFiles[index].productModelSource = 'manual' // 用户手动输入,最高优先级 @@ -2366,9 +2366,10 @@ export default function UploadModal({ onClose, onSuccess }: UploadModalProps) { disabled={interactionLocked} onUpdateProductModel={(index, value) => { const newFiles = [...batchVideoFiles] - newFiles[index].productModel = value + const normalizedValue = sanitizeSpuProductModelInput(value) + newFiles[index].productModel = normalizedValue // SPU 自动识别品类 - const result = parseSPU(value) + const result = parseSPU(normalizedValue) if (result.categoryMatched && result.categoryName) { const catExists = productCategories.some(c => c.name === result.categoryName) if (catExists) { diff --git a/components/upload/UploadForm.tsx b/components/upload/UploadForm.tsx index a5bef0d2..f36cba22 100644 --- a/components/upload/UploadForm.tsx +++ b/components/upload/UploadForm.tsx @@ -9,7 +9,7 @@ import { useState, useEffect, useRef } from 'react' import { requiresCategoryAndScene, requiresProductCategoryName, requiresRenderer, shouldAutoFillGeneral } from '@/lib/material-validation' import { parseSPU, type SPUParseResult } from '@/lib/spu-parser' import { isVideoMaterialType } from '@/lib/video-material-types' -import { isPlausibleSpuProductModel } from '@/lib/product-model' +import { isPlausibleSpuProductModel, sanitizeSpuProductModelInput } from '@/lib/product-model' /** * 分类数据项 @@ -198,7 +198,7 @@ export default function UploadForm({ if (materialType === '材质') { onProductModelChange(e.target.value) } else { - const value = e.target.value.replace(/[^a-zA-Z0-9\-_.]/g, '').toUpperCase() + const value = sanitizeSpuProductModelInput(e.target.value) onProductModelChange(value) } }} diff --git a/components/upload/upload-utils.test.ts b/components/upload/upload-utils.test.ts index d58b76db..cf7b49f7 100644 --- a/components/upload/upload-utils.test.ts +++ b/components/upload/upload-utils.test.ts @@ -27,8 +27,10 @@ describe('upload-utils video types', () => { ['624-456.mp4', ''], ['GBPM-SN0204-展示.mp4', 'GBPM-SN0204'], ['DF-A3DZ0008.mp4', 'DF-A3DZ0008'], - ['626DNZ0385-1.mp4', 'DNZ0385'], + ['626DNZ0385-1.mp4', '626DNZ0385-1'], ['XY-2024-001.mp4', 'XY-2024-001'], + ['GBGJ-DNZ0366+GBGJ-SJ0251.mp4', 'GBGJ-DNZ0366+GBGJ-SJ0251'], + ['GBPM-SN0204K12.mp4', 'GBPM-SN0204K12'], ])('从 %s 只提取可确认的 SPU', (fileName, expected) => { expect(extractSpuFromFileName(fileName)).toBe(expected) }) diff --git a/lib/__tests__/product-model.test.ts b/lib/__tests__/product-model.test.ts index 5784100b..bb4c576e 100644 --- a/lib/__tests__/product-model.test.ts +++ b/lib/__tests__/product-model.test.ts @@ -2,6 +2,7 @@ import { extractPlausibleSpuFromFileName, isPlausibleSpuProductModel, normalizeSpuProductModel, + sanitizeSpuProductModelInput, } from '@/lib/product-model' describe('product-model', () => { @@ -19,7 +20,18 @@ describe('product-model', () => { expect(normalizeSpuProductModel(' df-a3dz0008 ')).toBe('DF-A3DZ0008') }) - test('无法从文件名确认 SPU 时保持空值', () => { - expect(extractPlausibleSpuFromFileName('抖音素材-624-456.mp4')).toBe('') + test('输入清理保留组合 SPU 的合法加号', () => { + expect(sanitizeSpuProductModelInput(' gbgj-dnz0366+gbgj-sj0251 ')) + .toBe('GBGJ-DNZ0366+GBGJ-SJ0251') + }) + + test.each([ + ['抖音素材-624-456.mp4', ''], + ['GBGJ-DNZ0366+GBGJ-SJ0251.mp4', 'GBGJ-DNZ0366+GBGJ-SJ0251'], + ['GBPM-SN0204K12.mp4', 'GBPM-SN0204K12'], + ['626DNZ0385-1.mp4', '626DNZ0385-1'], + ['产品-GBPM-SN0204-展示.mp4', 'GBPM-SN0204'], + ])('从文件名 %s 安全识别完整 SPU', (fileName, expected) => { + expect(extractPlausibleSpuFromFileName(fileName)).toBe(expected) }) }) diff --git a/lib/product-model.ts b/lib/product-model.ts index fab520d1..989e1cc3 100644 --- a/lib/product-model.ts +++ b/lib/product-model.ts @@ -7,6 +7,14 @@ export function normalizeSpuProductModel(value: unknown): string { return typeof value === 'string' ? value.trim().toUpperCase() : '' } +/** + * 输入框只保留与后端校验一致的字符,避免合法的组合 SPU(例如 A+B) + * 在浏览器里被静默改写。 + */ +export function sanitizeSpuProductModelInput(value: unknown): string { + return normalizeSpuProductModel(value).replace(/[^A-Z0-9+_.-]/g, '') +} + /** * 这里只拒绝可以明确判定不是 SPU 的值,不假设所有业务线都使用同一套编码。 * 现有合法型号包含 DF-A3DZ0008、GBPM-SN0204、626DNZ0385-1 等多种格式; @@ -27,10 +35,18 @@ export function isPlausibleSpuProductModel(value: unknown): boolean { * 交给上传人填写,不能再把中文被剔除后的数字尾号冒充为 SPU。 */ export function extractPlausibleSpuFromFileName(filename: string): string { - const nameWithoutExtension = filename.replace(/\.[^/.]+$/, '').toUpperCase() - const candidates = nameWithoutExtension.match( - /(?:[A-Z]{2,6}-)?(?:[A-Z][A-Z0-9]{1,10}\d{3,5}|\d{4}-\d{3,})/g, - ) || [] + const nameWithoutExtension = normalizeSpuProductModel(filename.replace(/\.[^/.]+$/, '')) + + // 文件名本身就是合法 SPU 时必须完整保留,不能截掉 +组合、K12 后缀或数字前缀。 + if (isPlausibleSpuProductModel(nameWithoutExtension)) { + return nameWithoutExtension + } + + // 中文说明包裹 SPU 时,按不受支持的字符切段,再移除段首尾分隔符。 + const candidates = nameWithoutExtension + .split(/[^A-Z0-9+_.-]+/) + .map(candidate => candidate.replace(/^[+_.-]+|[+_.-]+$/g, '')) + .filter(Boolean) return candidates.find(isPlausibleSpuProductModel) || '' }