From aeb13757ea6e9e4df6564d8f16a8b6d2a7dd0e72 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Fri, 13 Nov 2026 21:23:03 +0800 Subject: [PATCH] =?UTF-8?q?feat(stock):=20=E5=95=86=E5=93=81=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=BA=8F=E5=88=97=E5=8F=B7=E8=A7=84=E5=88=99=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E4=B8=8E=E6=89=AB=E7=A0=81=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 sn_code_rules 规则编辑组件,支持前缀/后缀匹配与 full/strip 取值 - 开启序列号管理后必须配置规则,并提供扫码内容本地校验预览(不随表单提交) - 新增/编辑保存时归一化规则并校验空值、长度、重复及条数上限 - getInfo 回显与保存请求补充 sn_code_rules 类型 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/api/stock/stock.ts | 16 ++ .../stock/component/SnCodeRulesEditor.vue | 256 ++++++++++++++++++ src/views/stock/stock/component/add.vue | 30 ++ src/views/stock/stock/component/info.vue | 32 ++- src/views/stock/stock/sn-code-rules.ts | 134 +++++++++ src/views/stock/stock/stock-config.ts | 3 + 6 files changed, 470 insertions(+), 1 deletion(-) create mode 100644 src/views/stock/stock/component/SnCodeRulesEditor.vue create mode 100644 src/views/stock/stock/sn-code-rules.ts diff --git a/src/api/stock/stock.ts b/src/api/stock/stock.ts index 19fa5cb6..61a58016 100644 --- a/src/api/stock/stock.ts +++ b/src/api/stock/stock.ts @@ -103,10 +103,22 @@ export function getProhibitedList() { } as any) } +/** 序列号规则:扫码时按规则从扫码内容中提取 SN 码 */ +export type SnCodeRule = { + /** prefix=匹配前缀,suffix=匹配后缀 */ + match_type: 'prefix' | 'suffix' + /** 要匹配的前缀/后缀字符串,1~64 字符 */ + match_value: string + /** full=整个扫码内容作为 SN 码,strip=去掉匹配到的前后缀后作为 SN 码 */ + keep_mode: 'full' | 'strip' +} + /** stock.warehouse.getInfo 响应 data */ export type StockWarehouseInfoResponse = { /** 是否需要复核:Y=需要复核,N=不需要复核 */ need_recheck?: string + /** 序列号规则(接口统一输出数组) */ + sn_code_rules?: SnCodeRule[] [key: string]: any } @@ -128,6 +140,8 @@ export function getInfo(id: any) { export type SaveWarehouseStockParams = { /** 是否需要复核:Y=需要复核,N=不需要复核(非 Y/N 值会报错) */ need_recheck?: string + /** 序列号规则:整体覆盖保存,传 [] 清空,不传保持原值 */ + sn_code_rules?: SnCodeRule[] [key: string]: any } @@ -318,6 +332,8 @@ export function checkItemCodeUnique(data: CheckItemCodeUniqueParams) { export type CreateWarehouseStockParams = { /** 是否需要复核:Y=需要复核,N=不需要复核;缺省默认 Y(需要复核) */ need_recheck?: string + /** 序列号规则:整体覆盖保存,不传时创建为 [] */ + sn_code_rules?: SnCodeRule[] [key: string]: any } diff --git a/src/views/stock/stock/component/SnCodeRulesEditor.vue b/src/views/stock/stock/component/SnCodeRulesEditor.vue new file mode 100644 index 00000000..faac612c --- /dev/null +++ b/src/views/stock/stock/component/SnCodeRulesEditor.vue @@ -0,0 +1,256 @@ + + + + + diff --git a/src/views/stock/stock/component/add.vue b/src/views/stock/stock/component/add.vue index cb5c82dc..67a0b0c7 100644 --- a/src/views/stock/stock/component/add.vue +++ b/src/views/stock/stock/component/add.vue @@ -193,6 +193,17 @@ + + +
尺寸信息 @@ -575,6 +586,8 @@ import { Check, Refresh, Plus } from '@element-plus/icons-vue' import * as stockApi from '@/api/stock/stock' import { useUserStore } from '@/stores/modules/user' import { stockDefaultFormData, stockFormRules } from '../stock-config' +import { toSnCodeRulePayload, validateSnCodeRules } from '../sn-code-rules' +import SnCodeRulesEditor from './SnCodeRulesEditor.vue' const userStore = useUserStore() @@ -650,6 +663,7 @@ const resetForm = () => { formRef.value.resetFields() } formData.managementFlags = [] + formData.sn_code_rules = [] formData.enable_box_spec = false handleBoxSpecToggle(false) ElMessage.info('表单已重置') @@ -807,14 +821,30 @@ const prepareSaveData = () => { data.box_height = 0.00 data.box_volume = 0.00 } + + // 序列号规则仅在开启序列号管理时提交;关闭时传 [] 清空 + data.sn_code_rules = data.is_sn_mgmt === 'Y' ? toSnCodeRulePayload(formData.sn_code_rules) : [] + return data } +/** 开启序列号管理时必须配置规则,返回首条错误提示 */ +const validateSnCodeRulesForSave = () => { + if (formData.is_sn_mgmt !== 'Y') return '' + if (!formData.sn_code_rules.length) return '开启序列号管理后,请至少添加一条序列号规则' + return validateSnCodeRules(formData.sn_code_rules) +} + const handleSave = async () => { if (!formRef.value) return await formRef.value.validate(async (valid: boolean) => { if (valid) { + const snRuleError = validateSnCodeRulesForSave() + if (snRuleError) { + ElMessage.warning(snRuleError) + return + } loading.value = true emit('saving-change', true) try { diff --git a/src/views/stock/stock/component/info.vue b/src/views/stock/stock/component/info.vue index f60f87c9..b70872c9 100644 --- a/src/views/stock/stock/component/info.vue +++ b/src/views/stock/stock/component/info.vue @@ -228,6 +228,17 @@
+ +
+
+ 序列号规则 + 按规则从扫码内容中提取 SN 码;测试内容仅本地校验,不会提交 +
+
+ +
+
+
@@ -551,6 +562,8 @@ import { QuestionFilled } from '@element-plus/icons-vue' import * as stockApi from '@/api/stock/stock' import { useUserStore } from '@/stores/modules/user' import { stockDefaultFormData, stockFormRules } from '../stock-config' +import { normalizeSnCodeRules, toSnCodeRulePayload, validateSnCodeRules } from '../sn-code-rules' +import SnCodeRulesEditor from './SnCodeRulesEditor.vue' const userStore = useUserStore() const hasPermission = (key: string) => userStore.checkPermission(key) @@ -785,6 +798,8 @@ function normalizeStockInfoFormNumbers() { formData.packing_requirements = formData.packing_requirements ?? '' const mergeRaw = String(formData.merge_requirements ?? '').trim().toUpperCase() formData.merge_requirements = mergeRaw === 'N' ? 'N' : 'Y' + // 序列号规则:接口可能返回数组或 JSON 字符串,统一为数组 + formData.sn_code_rules = normalizeSnCodeRules(formData.sn_code_rules) formData.tag_price = toFiniteNumber(formData.tag_price, 0) formData.retail_price = toFiniteNumber(formData.retail_price, 0) formData.gross_weight = toFiniteNumber(formData.gross_weight, 0) @@ -925,10 +940,20 @@ function prepareSaveData() { data.box_height = 0.00 data.box_volume = 0.00 } - + + // 序列号规则仅在开启序列号管理时提交;关闭时传 [] 清空 + data.sn_code_rules = data.is_sn_mgmt === 'Y' ? toSnCodeRulePayload(formData.sn_code_rules) : [] + return data } +/** 开启序列号管理时必须配置规则,返回首条错误提示 */ +function validateSnCodeRulesForSave() { + if (formData.is_sn_mgmt !== 'Y') return '' + if (!formData.sn_code_rules.length) return '开启序列号管理后,请至少添加一条序列号规则' + return validateSnCodeRules(formData.sn_code_rules) +} + function validateShelfLifeForSave() { if (formData.is_shelf_life_mgmt !== 'Y') { return true @@ -959,6 +984,11 @@ async function handleSave() { try { await formRef.value.validate() + const snRuleError = validateSnCodeRulesForSave() + if (snRuleError) { + ElMessage.warning(snRuleError) + return + } if (!validateShelfLifeForSave()) { return } diff --git a/src/views/stock/stock/sn-code-rules.ts b/src/views/stock/stock/sn-code-rules.ts new file mode 100644 index 00000000..0a8fc5d7 --- /dev/null +++ b/src/views/stock/stock/sn-code-rules.ts @@ -0,0 +1,134 @@ +import type { SnCodeRule } from '@/api/stock/stock' + +export type { SnCodeRule } from '@/api/stock/stock' + +/** 序列号规则最大条数(与后端校验一致) */ +export const SN_CODE_RULE_MAX_COUNT = 20 +/** 匹配内容最大长度(与后端校验一致) */ +export const SN_CODE_RULE_VALUE_MAX_LENGTH = 64 + +const MATCH_TYPES: SnCodeRule['match_type'][] = ['prefix', 'suffix'] +const KEEP_MODES: SnCodeRule['keep_mode'][] = ['full', 'strip'] + +/** 新建一条默认规则:匹配前缀 + 去掉前缀后作为 SN 码 */ +export function createSnCodeRule(): SnCodeRule { + return { match_type: 'prefix', match_value: '', keep_mode: 'strip' } +} + +function readField(source: Record, keys: string[]): unknown { + for (const key of keys) { + if (source[key] !== undefined && source[key] !== null) { + return source[key] + } + } + return undefined +} + +/** + * 把接口返回值(数组或 JSON 字符串)统一成合法的规则数组。 + * 兼容 match_type/matchType/type、match_value/matchValue/value、keep_mode/keepMode/mode。 + */ +export function normalizeSnCodeRules(input: unknown): SnCodeRule[] { + if (input === null || input === undefined || input === '') return [] + let raw: unknown = input + if (typeof raw === 'string') { + try { + raw = JSON.parse(raw) + } catch { + return [] + } + } + if (!Array.isArray(raw)) return [] + + const rules: SnCodeRule[] = [] + for (const item of raw) { + if (!item || typeof item !== 'object') continue + const source = item as Record + const matchType = String(readField(source, ['match_type', 'matchType', 'type']) ?? '').trim().toLowerCase() + const keepMode = String(readField(source, ['keep_mode', 'keepMode', 'mode']) ?? '').trim().toLowerCase() + const matchValue = String(readField(source, ['match_value', 'matchValue', 'value']) ?? '') + if (!MATCH_TYPES.includes(matchType as SnCodeRule['match_type'])) continue + if (!KEEP_MODES.includes(keepMode as SnCodeRule['keep_mode'])) continue + rules.push({ + match_type: matchType as SnCodeRule['match_type'], + match_value: matchValue, + keep_mode: keepMode as SnCodeRule['keep_mode'] + }) + } + return rules +} + +/** 校验规则,返回第一条错误提示;全部合法时返回空字符串 */ +export function validateSnCodeRules(rules: SnCodeRule[]): string { + if (rules.length > SN_CODE_RULE_MAX_COUNT) { + return `序列号规则最多 ${SN_CODE_RULE_MAX_COUNT} 条` + } + + const matchedIndex = new Map() + for (let index = 0; index < rules.length; index++) { + const rule = rules[index] + if (!rule) continue + const order = index + 1 + const value = String(rule.match_value ?? '').trim() + if (value === '') { + return `第 ${order} 条序列号规则的匹配内容不能为空` + } + if (value.length > SN_CODE_RULE_VALUE_MAX_LENGTH) { + return `第 ${order} 条序列号规则的匹配内容不能超过 ${SN_CODE_RULE_VALUE_MAX_LENGTH} 个字符` + } + if (!MATCH_TYPES.includes(rule.match_type)) { + return `第 ${order} 条序列号规则的匹配位置不合法` + } + if (!KEEP_MODES.includes(rule.keep_mode)) { + return `第 ${order} 条序列号规则的 SN 码取值方式不合法` + } + const key = `${rule.match_type}\u0000${value}` + const duplicated = matchedIndex.get(key) + if (duplicated !== undefined) { + return `第 ${order} 条序列号规则与第 ${duplicated + 1} 条重复` + } + matchedIndex.set(key, index) + } + return '' +} + +export type SnCodeMatchResult = { + /** 命中的规则在规则列表中的下标 */ + index: number + rule: SnCodeRule + /** 按规则提取出的 SN 码(keep_mode=strip 时可能为空) */ + sn: string +} + +/** 按规则顺序匹配扫码内容,返回首个命中的规则与提取出的 SN 码 */ +export function matchSnCode(rules: SnCodeRule[], code: string): SnCodeMatchResult | null { + const content = String(code ?? '') + if (content.trim() === '') return null + + for (let index = 0; index < rules.length; index++) { + const rule = rules[index] + if (!rule) continue + const value = String(rule.match_value ?? '').trim() + if (!value) continue + const matched = rule.match_type === 'suffix' ? content.endsWith(value) : content.startsWith(value) + if (!matched) continue + + let sn = content + if (rule.keep_mode === 'strip') { + sn = rule.match_type === 'suffix' + ? content.slice(0, content.length - value.length) + : content.slice(value.length) + } + return { index, rule, sn } + } + return null +} + +/** 生成提交给后端的规则数据(去掉测试内容,匹配内容去首尾空格) */ +export function toSnCodeRulePayload(rules: SnCodeRule[]): SnCodeRule[] { + return rules.map((rule) => ({ + match_type: rule.match_type, + match_value: String(rule.match_value ?? '').trim(), + keep_mode: rule.keep_mode + })) +} diff --git a/src/views/stock/stock/stock-config.ts b/src/views/stock/stock/stock-config.ts index 68ad52ae..b6d71180 100644 --- a/src/views/stock/stock/stock-config.ts +++ b/src/views/stock/stock/stock-config.ts @@ -1,6 +1,7 @@ import { scanWorkflowField } from '@/utils/scanFields' import type { ToolbarButtonConfig } from '@/components/ToolbarButtonGroup/types' import { normalizeBarcodeList } from '@/utils/barcode' +import type { SnCodeRule } from '@/api/stock/stock' /** ScanBox 筛选字段分组(商品模块专用,保留供其他场景复用) */ export const STOCK_SCAN_FIELD_GROUPS = { @@ -569,6 +570,8 @@ export const stockDefaultFormData = { prohibited_id: undefined as number | undefined, brand_name: '', managementFlags: [] as string[], + // 序列号规则:开启序列号管理后用于从扫码内容中提取 SN 码 + sn_code_rules: [] as SnCodeRule[], // 箱规信息 enable_box_spec: false, quantity_per_box: 0,