feat(stock): 商品支持序列号规则配置与扫码校验
- 新增 sn_code_rules 规则编辑组件,支持前缀/后缀匹配与 full/strip 取值 - 开启序列号管理后必须配置规则,并提供扫码内容本地校验预览(不随表单提交) - 新增/编辑保存时归一化规则并校验空值、长度、重复及条数上限 - getInfo 回显与保存请求补充 sn_code_rules 类型 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
33e0b4c459
commit
aeb13757ea
@ -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
|
||||
}
|
||||
|
||||
|
||||
256
src/views/stock/stock/component/SnCodeRulesEditor.vue
Normal file
256
src/views/stock/stock/component/SnCodeRulesEditor.vue
Normal file
@ -0,0 +1,256 @@
|
||||
<template>
|
||||
<div class="sn-code-rules">
|
||||
<div class="sn-code-rules__toolbar">
|
||||
<span class="sn-code-rules__count">
|
||||
共 {{ rules.length }} / {{ SN_CODE_RULE_MAX_COUNT }} 条规则
|
||||
</span>
|
||||
<el-button
|
||||
size="small"
|
||||
type="primary"
|
||||
plain
|
||||
:disabled="disabled || rules.length >= SN_CODE_RULE_MAX_COUNT"
|
||||
@click="addRule"
|
||||
>
|
||||
<el-icon><Plus /></el-icon>
|
||||
添加规则
|
||||
</el-button>
|
||||
</div>
|
||||
|
||||
<el-table
|
||||
v-if="rules.length"
|
||||
:data="rules"
|
||||
size="small"
|
||||
border
|
||||
class="sn-code-rules__table"
|
||||
empty-text="暂无序列号规则"
|
||||
>
|
||||
<el-table-column type="index" label="#" width="48" align="center" />
|
||||
|
||||
<el-table-column label="匹配位置" width="130">
|
||||
<template #default="{ row }">
|
||||
<el-select v-model="row.match_type" size="small">
|
||||
<el-option label="前缀" value="prefix" />
|
||||
<el-option label="后缀" value="suffix" />
|
||||
</el-select>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="匹配内容" min-width="220">
|
||||
<template #default="{ row }">
|
||||
<el-input
|
||||
v-model="row.match_value"
|
||||
size="small"
|
||||
:maxlength="SN_CODE_RULE_VALUE_MAX_LENGTH"
|
||||
show-word-limit
|
||||
clearable
|
||||
placeholder="1~64 个字符"
|
||||
@blur="handleValueBlur(row)"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="SN 码取值" width="160">
|
||||
<template #default="{ row }">
|
||||
<el-select v-model="row.keep_mode" size="small">
|
||||
<el-option label="完整扫码内容" value="full" />
|
||||
<el-option label="去除前后缀" value="strip" />
|
||||
</el-select>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column label="操作" width="72" align="center">
|
||||
<template #default="{ $index }">
|
||||
<el-button type="danger" link size="small" :disabled="disabled" @click="removeRule($index)">
|
||||
删除
|
||||
</el-button>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
|
||||
<p v-else class="sn-code-rules__empty">
|
||||
尚未添加序列号规则,开启序列号管理后至少需要配置一条规则
|
||||
</p>
|
||||
|
||||
<div class="sn-code-rules__preview">
|
||||
<div class="sn-code-rules__preview-head">
|
||||
<span class="sn-code-rules__preview-title">规则校验</span>
|
||||
<span class="sn-code-rules__hint">测试内容仅用于本地校验,不会提交</span>
|
||||
</div>
|
||||
<el-input
|
||||
v-model="sampleText"
|
||||
size="small"
|
||||
clearable
|
||||
placeholder="粘贴或扫描一段条码内容,自动按上面的规则提取 SN 码"
|
||||
/>
|
||||
<div class="sn-code-rules__preview-result">
|
||||
<span v-if="!sampleText.trim()" class="sn-code-rules__hint">
|
||||
输入扫码内容后,将按规则顺序匹配并展示提取出的 SN 码
|
||||
</span>
|
||||
<template v-else-if="preview">
|
||||
<el-tag type="success" size="small">
|
||||
命中第 {{ preview.index + 1 }} 条规则({{
|
||||
preview.rule.match_type === 'prefix' ? '前缀' : '后缀'
|
||||
}}:{{ preview.rule.match_value }})
|
||||
</el-tag>
|
||||
<span class="sn-code-rules__sn">
|
||||
SN 码:<strong>{{ preview.sn || '(空)' }}</strong>
|
||||
</span>
|
||||
</template>
|
||||
<el-tag v-else type="danger" size="small">未匹配到任何规则</el-tag>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, ref, watch } from 'vue'
|
||||
import { Plus } from '@element-plus/icons-vue'
|
||||
import {
|
||||
SN_CODE_RULE_MAX_COUNT,
|
||||
SN_CODE_RULE_VALUE_MAX_LENGTH,
|
||||
createSnCodeRule,
|
||||
matchSnCode,
|
||||
normalizeSnCodeRules,
|
||||
type SnCodeRule
|
||||
} from '../sn-code-rules'
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
modelValue?: SnCodeRule[]
|
||||
disabled?: boolean
|
||||
}>(),
|
||||
{
|
||||
modelValue: () => [],
|
||||
disabled: false
|
||||
}
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:modelValue', value: SnCodeRule[]): void
|
||||
}>()
|
||||
|
||||
const rules = ref<SnCodeRule[]>([])
|
||||
const sampleText = ref('')
|
||||
let syncing = false
|
||||
|
||||
// 外部变更(接口回显 / 重置)时同步到本地;本地已提交过的值不重复覆盖,避免输入焦点丢失
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(value) => {
|
||||
const normalized = normalizeSnCodeRules(value)
|
||||
if (JSON.stringify(normalized) === JSON.stringify(rules.value)) return
|
||||
syncing = true
|
||||
rules.value = normalized
|
||||
void nextTick(() => {
|
||||
syncing = false
|
||||
})
|
||||
},
|
||||
{ immediate: true, deep: true }
|
||||
)
|
||||
|
||||
watch(
|
||||
rules,
|
||||
() => {
|
||||
if (syncing) return
|
||||
emit(
|
||||
'update:modelValue',
|
||||
rules.value.map((rule) => ({ ...rule }))
|
||||
)
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
|
||||
const preview = computed(() => matchSnCode(rules.value, sampleText.value))
|
||||
|
||||
function addRule() {
|
||||
if (props.disabled || rules.value.length >= SN_CODE_RULE_MAX_COUNT) return
|
||||
rules.value.push(createSnCodeRule())
|
||||
}
|
||||
|
||||
function removeRule(index: number) {
|
||||
if (props.disabled) return
|
||||
rules.value.splice(index, 1)
|
||||
}
|
||||
|
||||
function handleValueBlur(row: SnCodeRule) {
|
||||
const trimmed = String(row.match_value ?? '').trim()
|
||||
if (trimmed !== row.match_value) {
|
||||
row.match_value = trimmed
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.sn-code-rules {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.sn-code-rules__toolbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.sn-code-rules__count,
|
||||
.sn-code-rules__hint {
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.sn-code-rules__table {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.sn-code-rules__empty {
|
||||
margin: 0 0 8px;
|
||||
padding: 12px;
|
||||
color: var(--el-text-color-secondary);
|
||||
font-size: 12px;
|
||||
text-align: center;
|
||||
background: var(--el-fill-color-lighter);
|
||||
border: 1px dashed var(--el-border-color);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.sn-code-rules__preview {
|
||||
margin-top: 12px;
|
||||
padding: 10px 12px;
|
||||
background: var(--el-fill-color-lighter);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.sn-code-rules__preview-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.sn-code-rules__preview-title {
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.sn-code-rules__preview-result {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
min-height: 24px;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.sn-code-rules__sn {
|
||||
font-size: 12px;
|
||||
color: var(--el-text-color-regular);
|
||||
}
|
||||
|
||||
.sn-code-rules__sn strong {
|
||||
color: var(--el-color-primary);
|
||||
font-size: 13px;
|
||||
word-break: break-all;
|
||||
}
|
||||
</style>
|
||||
@ -193,6 +193,17 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 序列号规则:开启序列号管理后必须配置,用于从扫码内容中提取 SN 码 -->
|
||||
<template v-if="formData.is_sn_mgmt === 'Y'">
|
||||
<div class="task-panel__sub-head">
|
||||
<span class="task-panel__sub-title">序列号规则</span>
|
||||
<span class="task-panel__sub-meta">按规则从扫码内容中提取 SN 码;测试内容仅本地校验,不会提交</span>
|
||||
</div>
|
||||
<div class="task-panel__table-wrap">
|
||||
<SnCodeRulesEditor v-model="formData.sn_code_rules" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- 尺寸信息 -->
|
||||
<div class="task-panel__sub-head">
|
||||
<span class="task-panel__sub-title">尺寸信息</span>
|
||||
@ -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 {
|
||||
|
||||
@ -228,6 +228,17 @@
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- 序列号规则:开启序列号管理后必须配置,用于从扫码内容中提取 SN 码 -->
|
||||
<section v-if="formData.is_sn_mgmt === 'Y'" class="info-block">
|
||||
<div class="task-panel__sub-head">
|
||||
<span class="task-panel__sub-title">序列号规则</span>
|
||||
<span class="task-panel__sub-meta">按规则从扫码内容中提取 SN 码;测试内容仅本地校验,不会提交</span>
|
||||
</div>
|
||||
<div class="task-panel__table-wrap">
|
||||
<SnCodeRulesEditor v-model="formData.sn_code_rules" />
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<!-- 尺寸与箱规 -->
|
||||
<section class="info-block">
|
||||
<div class="task-panel__sub-head">
|
||||
@ -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
|
||||
}
|
||||
|
||||
134
src/views/stock/stock/sn-code-rules.ts
Normal file
134
src/views/stock/stock/sn-code-rules.ts
Normal file
@ -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<string, unknown>, 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<string, unknown>
|
||||
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<string, number>()
|
||||
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
|
||||
}))
|
||||
}
|
||||
@ -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,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user