feat(订单复核): 支持扫码SN序列号规则校验

- scanOrder 返回的 stock.sn_code_rules 归一化后写入商品项,随 goodsList 传入 ReviewDetail
- 扫码/手动录入 SN 时按 prefix/suffix 匹配:keep_mode=strip 截取剩余部分作为 SN,full 保留整段
- 不符合序列号规则的内容不录入 SN 列表,Toast 并语音播报“非SN码”
- 未配置规则时保持原有整段作为 SN 的行为

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-13 23:44:34 +08:00
parent 9f914e149b
commit 5a86f3ea9d
2 changed files with 75 additions and 2 deletions

View File

@ -527,14 +527,21 @@ import voice from '@/core/utils/voice';
this.addSn(code, this.snPopupIndex >= 0 ? this.snPopupIndex : this.currentGoodsIndex);
},
addSn(code, index) {
const raw = this.normalizeBarcode(code);
if (!raw) return false;
const codeValue = this.normalizeBarcode(code);
if (!codeValue) return false;
const idx = (typeof index === 'number' && index >= 0 && index < this.goodsList.length)
? index
: (this.snPopupIndex >= 0 ? this.snPopupIndex : this.currentGoodsIndex);
if (idx < 0 || idx >= this.goodsList.length) return false;
const item = this.goodsList[idx];
if (!this.isSnItem(item)) return false;
// 按商品序列号规则解析扫码内容:不符合规则的不录入 SN 列表,只提示非SN码
const raw = this.resolveSnCode(codeValue, item);
if (!raw) {
uni.showToast({ title: '非SN码', icon: 'none' });
voice.speak('非SN码');
return true;
}
const planQty = Number(item.planQty || 0);
const list = (Array.isArray(item.snList) ? item.snList : []).map(v => String(v ?? '').trim()).filter(Boolean);
if (list.includes(raw)) {
@ -582,6 +589,42 @@ import voice from '@/core/utils/voice';
normalizeBarcode(value) {
return String(value || '').replace(/\s+/g, '').toUpperCase();
},
// 读取商品序列号规则(scanOrder 返回 stock.sn_code_rules)
snCodeRulesOf(item) {
return Array.isArray(item?.sn_code_rules)
? item.sn_code_rules.filter(rule => rule && typeof rule === 'object')
: [];
},
/**
* 按序列号规则解析扫码内容
* match_type: prefix/suffix 匹配前缀/后缀;keep_mode: full 整段作为 SN 码、strip 去掉匹配到的前后缀
* @returns {string} 解析后的 SN 码;不符合任何规则时返回空字符串(视为非SN码)
*/
resolveSnCode(code, item) {
const normalized = this.normalizeBarcode(code);
if (!normalized) return '';
const rules = this.snCodeRulesOf(item);
// 未配置规则:保持原有逻辑,整段扫码内容作为 SN 码
if (!rules.length) return normalized;
for (let i = 0; i < rules.length; i += 1) {
const rule = rules[i];
const matchType = String(rule?.match_type ?? rule?.matchType ?? '').trim().toLowerCase();
const keepMode = String(rule?.keep_mode ?? rule?.keepMode ?? '').trim().toLowerCase();
const matchValue = this.normalizeBarcode(rule?.match_value ?? rule?.matchValue ?? '');
if (!matchValue) continue;
let rest = '';
if (matchType === 'prefix' && normalized.startsWith(matchValue)) {
rest = normalized.slice(matchValue.length);
} else if (matchType === 'suffix' && normalized.endsWith(matchValue)) {
rest = normalized.slice(0, normalized.length - matchValue.length);
} else {
continue;
}
if (keepMode !== 'strip') return normalized;
return this.normalizeBarcode(rest);
}
return '';
},
// 提取商品条码列表(优先取 barcodes 数组,兜底拆分 barcode 字符串),仅用于展示,不影响匹配
barcodeListOf(item) {
const arr = Array.isArray(item?.barcodes) ? item.barcodes : [];

View File

@ -2609,6 +2609,27 @@ import ReviewDetail from './order-review-comps/ReviewDetail.vue';
}
return [];
},
// 归一化序列号规则(scanOrder 返回的 stock.sn_code_rules)
// match_type: prefix/suffix 匹配前缀/后缀;keep_mode: full 保留全部内容、strip 去掉匹配到的前后缀
normalizeSnCodeRules(rules) {
let list = rules;
if (typeof list === 'string') {
try {
list = JSON.parse(list);
} catch (e) {
list = [];
}
}
if (!Array.isArray(list)) return [];
return list
.filter(rule => rule && typeof rule === 'object')
.map(rule => ({
match_type: String(rule?.match_type ?? rule?.matchType ?? '').trim().toLowerCase(),
match_value: String(rule?.match_value ?? rule?.matchValue ?? '').trim(),
keep_mode: String(rule?.keep_mode ?? rule?.keepMode ?? '').trim().toLowerCase()
}))
.filter(rule => !!rule.match_value);
},
async fetchGroupReviewedDetail(waveId) {
if (!waveId) {
return;
@ -2852,6 +2873,14 @@ import ReviewDetail from './order-review-comps/ReviewDetail.vue';
const barcodeSource = stockItem?.barcodes || rawItem?.barcodes || rawItem?.barcode || snapshot?.barcodes || snapshot?.barcode || [];
const stockBarcodes = this.normalizeBarcodeValues(barcodeSource);
const boxNo = String(stockItem?.box_no ?? stockItem?.boxNo ?? rawItem?.box_no ?? rawItem?.boxNo ?? '').trim();
// 序列号规则:用于扫码时校验/截取 SN 码(未配置时整段扫码内容作为 SN 码)
let snCodeRules = [];
const snCodeRuleSources = [stockItem, rawItem, snapshot];
for (let i = 0; i < snCodeRuleSources.length; i += 1) {
const source = snCodeRuleSources[i];
snCodeRules = this.normalizeSnCodeRules(source?.sn_code_rules ?? source?.snCodeRules);
if (snCodeRules.length) break;
}
return {
id: String(rawItem?.id || stockItem?.stock_id || `item-${index + 1}`),
@ -2864,6 +2893,7 @@ import ReviewDetail from './order-review-comps/ReviewDetail.vue';
need_sn: needSn,
stock_id: stockItem?.stock_id ?? rawItem?.stock_id ?? '',
snList: [],
sn_code_rules: snCodeRules,
packageRequirement: String(stockItem?.recheck_requirements || rawItem?.recheck_requirements || stockItem?.packing_requirements || rawItem?.packing_requirements || rawItem?.package_requirement || rawItem?.packageRequirement || rawItem?.packing_requirement || '').trim() || '--',
skuCode: String(stockItem?.item_code || rawItem?.item_code || snapshot?.item_code || ''),
skuName: stockName || String(snapshot?.item_name || rawItem?.item_name || '--'),