117 lines
3.5 KiB
TypeScript
117 lines
3.5 KiB
TypeScript
import type { QcItemParam } from '@/api/qc-item-param'
|
||
|
||
/** 解析参数 options 字段(JSON 数组字符串) */
|
||
export function parseQcParamOptions(options?: string | null): string[] {
|
||
if (!options) return []
|
||
try {
|
||
const arr = JSON.parse(options)
|
||
return Array.isArray(arr)
|
||
? arr.map((x) => String(x ?? '').trim()).filter(Boolean)
|
||
: []
|
||
} catch {
|
||
return []
|
||
}
|
||
}
|
||
|
||
/** 逗号分隔文本 → 选项列表(支持中英文逗号) */
|
||
export function parseQcParamOptionText(text?: string | null): string[] {
|
||
if (!text?.trim()) return []
|
||
const seen = new Set<string>()
|
||
const list: string[] = []
|
||
for (const part of text.split(/[,,]/)) {
|
||
const val = part.trim()
|
||
if (!val || seen.has(val)) continue
|
||
seen.add(val)
|
||
list.push(val)
|
||
}
|
||
return list
|
||
}
|
||
|
||
/** 选项列表 → 逗号分隔展示文本(默认项排在最前) */
|
||
export function formatQcParamOptionText(options?: string | null, defaultValue?: string | null): string {
|
||
const list = parseQcParamOptions(options)
|
||
if (!list.length) return ''
|
||
const dv = (defaultValue || '').trim()
|
||
if (dv && list.includes(dv) && list[0] !== dv) {
|
||
return [dv, ...list.filter((x) => x !== dv)].join(',')
|
||
}
|
||
return list.join(',')
|
||
}
|
||
|
||
let paramRowKeySeed = 0
|
||
|
||
/** 为参数行分配稳定 key(表格展开态、列表渲染) */
|
||
export function assignQcItemParamRowKeys(params: QcItemParam[]): QcItemParam[] {
|
||
return (params || []).map((p) => ({
|
||
...p,
|
||
_rowKey: p._rowKey || p.id || `param-${Date.now()}-${++paramRowKeySeed}`
|
||
}))
|
||
}
|
||
|
||
/** 表单加载:补充单选选项编辑态 */
|
||
export function hydrateQcItemParamsForForm(params: QcItemParam[]): QcItemParam[] {
|
||
return assignQcItemParamRowKeys(
|
||
(params || []).map((p) => {
|
||
if (p.paramType !== 'select') return { ...p }
|
||
return {
|
||
...p,
|
||
_optionText: formatQcParamOptionText(p.options, p.defaultValue)
|
||
}
|
||
})
|
||
)
|
||
}
|
||
|
||
/** 类型切换时清理或初始化单选字段 */
|
||
export function onQcItemParamTypeChange(param: QcItemParam) {
|
||
if (param.paramType === 'select') {
|
||
if (param._optionText === undefined) {
|
||
param._optionText = ''
|
||
}
|
||
return
|
||
}
|
||
param.options = undefined
|
||
param.defaultValue = undefined
|
||
param._optionText = undefined
|
||
}
|
||
|
||
/** 提交前序列化并校验 */
|
||
export function serializeQcItemParamsForSave(params: QcItemParam[]): QcItemParam[] {
|
||
return (params || []).map((p, idx) => {
|
||
const { _optionText, _rowKey, ...rest } = p
|
||
const base: QcItemParam = { ...rest, orderNum: p.orderNum ?? idx }
|
||
if (p.paramType !== 'select') {
|
||
base.options = undefined
|
||
base.defaultValue = undefined
|
||
return base
|
||
}
|
||
const list =
|
||
_optionText !== undefined
|
||
? parseQcParamOptionText(_optionText)
|
||
: parseQcParamOptions(p.options)
|
||
return {
|
||
...base,
|
||
options: list.length ? JSON.stringify(list) : undefined,
|
||
defaultValue: list[0] || undefined
|
||
}
|
||
})
|
||
}
|
||
|
||
/** @returns 错误文案;通过时返回 null */
|
||
export function validateQcItemParamsBeforeSave(params: QcItemParam[]): string | null {
|
||
const serialized = serializeQcItemParamsForSave(params)
|
||
for (let i = 0; i < serialized.length; i++) {
|
||
const p = serialized[i]
|
||
const label = p.paramName?.trim() || `第 ${i + 1} 个参数`
|
||
if (p.paramType === 'select') {
|
||
const opts = parseQcParamOptions(p.options)
|
||
if (!opts.length) {
|
||
return `「${label}」为单选类型,请至少配置一个选项`
|
||
}
|
||
if (new Set(opts).size !== opts.length) {
|
||
return `「${label}」的选项不能重复`
|
||
}
|
||
}
|
||
}
|
||
return null
|
||
}
|