fix(系统设置): 仓库设置子项归一化改为按 getFieldMeta 的 item_keys 驱动
- auto_outbound_rules 字段名对齐后端:allow_same_day_outbound 改为 exclude_same_day_outbound (语义相反、默认 true),并兼容读取旧字段名 - normalizeWarehouseSubjectConfig 新增可选 schema:顶层 object / array 字段的子项按 meta item_keys 递归归一化(flag→boolean、integer→number、缺省用 default),避免后端改字段名 或新增子项时读取与提交再次出现漂移;顶层其它类型保持既有归一化,不改 flag 的 0/1 语义 - normalizeGetSettingsPayload / buildSaveSettingsRequest / normalizeSaveSettingsPayload 透传 schema; 页面先解析 meta 再解析 settings,并把字段元信息作为 schema 传入 - 顺带修复 after_sale_followup_hours 提交空对象的问题
This commit is contained in:
parent
5c3ad6ad75
commit
4b6def1d89
@ -14,11 +14,11 @@ export type SettingWarehouseAutoOutboundRules = {
|
||||
require_sync?: boolean
|
||||
require_print?: boolean
|
||||
require_pick?: boolean
|
||||
/** 是否允许当天复核的订单当天自动出库,默认 false */
|
||||
allow_same_day_outbound?: boolean
|
||||
/** 允许当天出库最后时间,格式 HH:mm,默认 06:00;复核时间早于该时间的订单不受「出库当天订单」关闭的影响 */
|
||||
/** 是否不出库当天订单(默认 true):开启后,当天放开时间前、「允许当天出库最后时间」之后复核的订单不自动出库 */
|
||||
exclude_same_day_outbound?: boolean
|
||||
/** 允许当天出库最后时间,格式 HH:mm,默认 06:00;复核时间早于该时间的订单不受「不出库当天订单」的影响 */
|
||||
same_day_outbound_deadline_time?: string
|
||||
/** 当天放开时间,格式 HH:mm,默认 22:00;未开启「出库当天订单」时,当天到达该时间后不再限制当天复核订单的自动出库 */
|
||||
/** 当天放开时间,格式 HH:mm,默认 22:00;开启「不出库当天订单」时,当天到达该时间后不再限制当天复核订单的自动出库 */
|
||||
same_day_outbound_release_time?: string
|
||||
}
|
||||
|
||||
@ -31,7 +31,7 @@ export const AUTO_OUTBOUND_RULE_FLAG_KEYS = [
|
||||
'require_sync',
|
||||
'require_print',
|
||||
'require_pick',
|
||||
'allow_same_day_outbound'
|
||||
'exclude_same_day_outbound'
|
||||
] as const
|
||||
|
||||
/** auto_outbound_rules 中的 HH:mm 时间子项(缺省/空值回退默认,非空值原样保留交由后端校验) */
|
||||
@ -49,7 +49,7 @@ export const DEFAULT_AUTO_OUTBOUND_RULES: Required<SettingWarehouseAutoOutboundR
|
||||
require_sync: true,
|
||||
require_print: true,
|
||||
require_pick: true,
|
||||
allow_same_day_outbound: false,
|
||||
exclude_same_day_outbound: true,
|
||||
same_day_outbound_deadline_time: '06:00',
|
||||
same_day_outbound_release_time: '22:00'
|
||||
}
|
||||
@ -178,9 +178,136 @@ function clonePlainObject<T extends Record<string, unknown>>(src: T): T {
|
||||
return JSON.parse(JSON.stringify(src)) as T
|
||||
}
|
||||
|
||||
/** 归一化 getSettings / saveSettings 返回的 subject_config(补全文档默认项) */
|
||||
/**
|
||||
* 字段元信息 schema(只取归一化需要的部分)。来源二选一:
|
||||
* - getFieldMeta 的 `list` / `item_keys`(原始 meta,读 `type` / `default` / `item_keys`);
|
||||
* - 页面归一化后的表单字段(读 `type` / `defaultValue` / `itemFields`)。
|
||||
* `type` 兼容 meta 原始值(flag / integer / string_array…)与页面归一化值(boolean / number…)。
|
||||
*/
|
||||
export type WarehouseSubjectFieldSchema = {
|
||||
key: string
|
||||
type?: string
|
||||
/** meta 原始默认值 */
|
||||
default?: unknown
|
||||
/** 页面归一化后的默认值(优先) */
|
||||
defaultValue?: unknown
|
||||
/** 页面归一化后的子项 */
|
||||
itemFields?: WarehouseSubjectFieldSchema[]
|
||||
/** meta 原始子项 */
|
||||
item_keys?: WarehouseSubjectFieldSchema[]
|
||||
}
|
||||
|
||||
/** 取子项 schema(兼容 itemFields / item_keys 两种来源) */
|
||||
function schemaChildren(field: WarehouseSubjectFieldSchema): WarehouseSubjectFieldSchema[] {
|
||||
const children = field.itemFields ?? field.item_keys
|
||||
return Array.isArray(children) ? children : []
|
||||
}
|
||||
|
||||
function schemaFlagValue(value: unknown, fallback: unknown): boolean {
|
||||
if (value === true || value === 1 || value === '1') return true
|
||||
if (value === false || value === 0 || value === '0') return false
|
||||
return fallback === true || fallback === 1 || fallback === '1'
|
||||
}
|
||||
|
||||
const SCHEMA_FLAG_TYPES = new Set(['flag', 'boolean', 'bool', 'switch'])
|
||||
const SCHEMA_NUMBER_TYPES = new Set(['number', 'integer', 'int', 'float', 'decimal'])
|
||||
|
||||
/** 按字段 schema 归一化单个值;返回 undefined 表示该键不写入 */
|
||||
function normalizeSchemaValue(
|
||||
value: unknown,
|
||||
field: WarehouseSubjectFieldSchema,
|
||||
present: boolean
|
||||
): unknown {
|
||||
const type = String(field.type ?? '').toLowerCase()
|
||||
const fallback = field.defaultValue !== undefined ? field.defaultValue : field.default
|
||||
const children = schemaChildren(field)
|
||||
|
||||
// flag / boolean:兼容 true|false、1|0、'1'|'0',非法或缺省回退默认
|
||||
if (SCHEMA_FLAG_TYPES.has(type)) {
|
||||
if (!present && fallback === undefined) return undefined
|
||||
return schemaFlagValue(present ? value : fallback, fallback)
|
||||
}
|
||||
|
||||
// number / integer:非法或缺省回退默认(无默认则不写入)
|
||||
if (SCHEMA_NUMBER_TYPES.has(type)) {
|
||||
const num = Number(present ? value : fallback)
|
||||
if (Number.isFinite(num)) return num
|
||||
return fallback === undefined ? undefined : fallback
|
||||
}
|
||||
|
||||
// object:只保留 schema(meta item_keys)里声明的子项,逐项递归
|
||||
if (type === 'object') {
|
||||
if (!children.length) return present ? value : fallback
|
||||
const source =
|
||||
value && typeof value === 'object' && !Array.isArray(value)
|
||||
? (value as Record<string, unknown>)
|
||||
: fallback && typeof fallback === 'object' && !Array.isArray(fallback)
|
||||
? (fallback as Record<string, unknown>)
|
||||
: {}
|
||||
return normalizeSchemaObject(source, children)
|
||||
}
|
||||
|
||||
// array:逐项按子项 schema 归一化
|
||||
if (type === 'array') {
|
||||
const list = Array.isArray(value) ? value : Array.isArray(fallback) ? fallback : []
|
||||
if (!children.length) return list
|
||||
return list.map((item) =>
|
||||
item && typeof item === 'object' && !Array.isArray(item)
|
||||
? normalizeSchemaObject(item as Record<string, unknown>, children)
|
||||
: item
|
||||
)
|
||||
}
|
||||
|
||||
// string_array:字符串数组(去空去重)
|
||||
if (type === 'string_array') {
|
||||
const list = Array.isArray(value) ? value : Array.isArray(fallback) ? fallback : []
|
||||
return list
|
||||
.filter((v): v is string => typeof v === 'string' && v.trim() !== '')
|
||||
.map((v) => v.trim())
|
||||
}
|
||||
|
||||
// string / color / select / location:有值原样保留,缺省用默认
|
||||
if (!present) return fallback
|
||||
return value
|
||||
}
|
||||
|
||||
/** 按子项 schema 递归归一化对象(只保留 schema 中声明的子项) */
|
||||
function normalizeSchemaObject(
|
||||
source: Record<string, unknown>,
|
||||
children: WarehouseSubjectFieldSchema[]
|
||||
): Record<string, unknown> {
|
||||
const out: Record<string, unknown> = {}
|
||||
for (const child of children) {
|
||||
if (!child?.key) continue
|
||||
const normalized = normalizeSchemaValue(source[child.key], child, child.key in source)
|
||||
if (normalized !== undefined) out[child.key] = normalized
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 schema 归一化顶层 object / array 字段的子项(子项集合以 meta item_keys 为准,避免前端硬编码漂移)。
|
||||
* 顶层其它类型仍走各自既有归一化,不改动 flag 的 0/1 语义。
|
||||
*/
|
||||
function applySchemaToSubjectConfig(
|
||||
config: Record<string, unknown>,
|
||||
schema?: WarehouseSubjectFieldSchema[]
|
||||
): void {
|
||||
if (!Array.isArray(schema) || schema.length === 0) return
|
||||
for (const field of schema) {
|
||||
if (!field?.key) continue
|
||||
const type = String(field.type ?? '').toLowerCase()
|
||||
if (type !== 'object' && type !== 'array') continue
|
||||
if (!schemaChildren(field).length) continue
|
||||
const normalized = normalizeSchemaValue(config[field.key], field, field.key in config)
|
||||
if (normalized !== undefined) config[field.key] = normalized
|
||||
}
|
||||
}
|
||||
|
||||
/** 归一化 getSettings / saveSettings 返回的 subject_config(补全文档默认项;传入 schema 时按 meta 归一化子项) */
|
||||
export function normalizeWarehouseSubjectConfig(
|
||||
raw: unknown
|
||||
raw: unknown,
|
||||
schema?: WarehouseSubjectFieldSchema[]
|
||||
): SettingWarehouseSubjectConfig {
|
||||
const base =
|
||||
raw && typeof raw === 'object' && !Array.isArray(raw)
|
||||
@ -197,6 +324,14 @@ export function normalizeWarehouseSubjectConfig(
|
||||
? { ...(rulesRaw as Record<string, unknown>) }
|
||||
: ({} as Record<string, unknown>)
|
||||
|
||||
// 兼容历史字段名:allow_same_day_outbound(旧,「允许当天出库」)= !exclude_same_day_outbound(新,「不出库当天订单」)
|
||||
if (!('exclude_same_day_outbound' in rules) && 'allow_same_day_outbound' in rules) {
|
||||
const legacyAllow = rules.allow_same_day_outbound
|
||||
rules.exclude_same_day_outbound = !(
|
||||
legacyAllow === true || legacyAllow === 1 || legacyAllow === '1'
|
||||
)
|
||||
}
|
||||
|
||||
// 按白名单保留 flag 与 HH:mm 时间子项:不能只按「值是否为 boolean」过滤,
|
||||
// 否则 same_day_outbound_deadline_time / same_day_outbound_release_time(字符串)
|
||||
// 会在读取与保存时被静默丢弃。
|
||||
@ -307,17 +442,38 @@ export function normalizeWarehouseSubjectConfig(
|
||||
base.receiving_pending_inbound_notice =
|
||||
notice as SettingWarehouseReceivingPendingInboundNotice
|
||||
|
||||
// 子项集合以 getFieldMeta 的 item_keys 为准(不再由前端硬编码子项 key):
|
||||
// 后端给 object / array 字段改名或加子项时,只要页面把 meta 字段(含 itemFields)传进来,
|
||||
// 读取与保存都会自动跟随,不会再出现「拉取的和提交的对不上」。
|
||||
applySchemaToSubjectConfig(base, schema)
|
||||
|
||||
// 跨字段约束:max_hours 必须大于 min_hours(schema 无法表达,放 schema 之后兜底)
|
||||
const finalNotice = base.receiving_pending_inbound_notice
|
||||
if (finalNotice && typeof finalNotice === 'object' && !Array.isArray(finalNotice)) {
|
||||
const finalObj = finalNotice as Record<string, unknown>
|
||||
const minH = Number(finalObj.min_hours)
|
||||
const maxH = Number(finalObj.max_hours)
|
||||
const safeMin = Number.isFinite(minH) ? minH : 0
|
||||
if (!Number.isFinite(maxH) || maxH <= safeMin) {
|
||||
finalObj.max_hours = Math.max(
|
||||
DEFAULT_RECEIVING_PENDING_INBOUND_NOTICE.max_hours,
|
||||
safeMin + 1
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return base as SettingWarehouseSubjectConfig
|
||||
}
|
||||
|
||||
export function normalizeGetSettingsPayload(
|
||||
raw: Record<string, unknown> | null | undefined
|
||||
raw: Record<string, unknown> | null | undefined,
|
||||
schema?: WarehouseSubjectFieldSchema[]
|
||||
): SettingWarehouseSettingsPayload {
|
||||
const payload = raw && typeof raw === 'object' ? raw : {}
|
||||
const warehouseId = Number(payload.warehouse_id)
|
||||
return {
|
||||
warehouse_id: Number.isFinite(warehouseId) ? warehouseId : 0,
|
||||
subject_config: normalizeWarehouseSubjectConfig(payload.subject_config)
|
||||
subject_config: normalizeWarehouseSubjectConfig(payload.subject_config, schema)
|
||||
}
|
||||
}
|
||||
|
||||
@ -325,9 +481,11 @@ export function normalizeGetSettingsPayload(
|
||||
export function buildSaveSettingsRequest(options: {
|
||||
subject_config: SettingWarehouseSubjectConfig | Record<string, unknown>
|
||||
cache_warehouse_id?: number | null
|
||||
/** 字段元信息(页面归一化后的表单字段即可):用于按 item_keys 归一化 object / array 子项 */
|
||||
schema?: WarehouseSubjectFieldSchema[]
|
||||
}): SettingWarehouseSaveSettingsParams {
|
||||
const body: SettingWarehouseSaveSettingsParams = {
|
||||
subject_config: normalizeWarehouseSubjectConfig(options.subject_config)
|
||||
subject_config: normalizeWarehouseSubjectConfig(options.subject_config, options.schema)
|
||||
}
|
||||
const cacheId = options.cache_warehouse_id
|
||||
if (cacheId != null && Number.isFinite(cacheId) && cacheId > 0) {
|
||||
@ -337,9 +495,10 @@ export function buildSaveSettingsRequest(options: {
|
||||
}
|
||||
|
||||
export function normalizeSaveSettingsPayload(
|
||||
raw: Record<string, unknown> | null | undefined
|
||||
raw: Record<string, unknown> | null | undefined,
|
||||
schema?: WarehouseSubjectFieldSchema[]
|
||||
): SettingWarehouseSaveSettingsPayload {
|
||||
const base = normalizeGetSettingsPayload(raw)
|
||||
const base = normalizeGetSettingsPayload(raw, schema)
|
||||
const cleared = raw?.cache_cleared_warehouse_id
|
||||
const cacheCleared =
|
||||
cleared === null || cleared === undefined
|
||||
|
||||
@ -321,9 +321,9 @@ const FALLBACK_FIELDS: NormalizedField[] = [
|
||||
{ key: 'require_sync', label: '要求已同步', type: 'boolean', description: 'sync_status=fin,默认开启' },
|
||||
{ key: 'require_print', label: '要求已打印', type: 'boolean', description: 'print_status=fin,默认开启' },
|
||||
{ key: 'require_pick', label: '要求已拣货', type: 'boolean', description: 'pick_status=fin,默认开启' },
|
||||
{ key: 'allow_same_day_outbound', label: '出库当天订单', type: 'boolean', description: '允许当天复核的订单当天自动出库,默认关闭' },
|
||||
{ key: 'same_day_outbound_deadline_time', label: '允许当天出库最后时间', type: 'string', maxLength: 5, defaultValue: '06:00', description: '格式 HH:mm,默认 06:00;复核时间早于该时间的订单不受「出库当天订单」关闭的影响' },
|
||||
{ key: 'same_day_outbound_release_time', label: '当天放开时间', type: 'string', maxLength: 5, defaultValue: '22:00', description: '格式 HH:mm,默认 22:00;未开启「出库当天订单」时,当天到达该时间后不再限制当天复核订单的自动出库' }
|
||||
{ key: 'exclude_same_day_outbound', label: '不出库当天订单', type: 'boolean', defaultValue: true, description: '开启后,在当天放开时间前、「允许当天出库最后时间」之后复核的订单不自动出库,到达当天放开时间后恢复;默认开启' },
|
||||
{ key: 'same_day_outbound_deadline_time', label: '允许当天出库最后时间', type: 'string', maxLength: 5, defaultValue: '06:00', description: '格式 HH:mm,默认 06:00;复核时间早于该时间的订单不受「不出库当天订单」的影响' },
|
||||
{ key: 'same_day_outbound_release_time', label: '当天放开时间', type: 'string', maxLength: 5, defaultValue: '22:00', description: '格式 HH:mm,默认 22:00;开启「不出库当天订单」时,当天到达该时间后不再限制当天复核订单的自动出库' }
|
||||
]
|
||||
},
|
||||
{
|
||||
@ -601,7 +601,11 @@ function onLocationFieldChange(key: string, value: number | string | null) {
|
||||
}
|
||||
|
||||
function applySettingsPayload(settingsRes: SettingWarehouseSettingsPayload | Record<string, unknown>) {
|
||||
const normalized = normalizeGetSettingsPayload(settingsRes as Record<string, unknown>)
|
||||
// schema 传 meta 表单字段:object / array 子项按 item_keys 归一化(后端改名/加字段自动跟随)
|
||||
const normalized = normalizeGetSettingsPayload(
|
||||
settingsRes as Record<string, unknown>,
|
||||
displayFields.value
|
||||
)
|
||||
const wid = normalized.warehouse_id
|
||||
warehouseId.value = typeof wid === 'number' && wid > 0 ? wid : null
|
||||
const cfg = cloneSubjectConfig(normalized.subject_config)
|
||||
@ -636,13 +640,17 @@ async function loadAll() {
|
||||
if (!settingsRes || !okCode(settingsRes.code)) {
|
||||
throw new Error(settingsRes?.message || '获取仓库设置失败(getSettings)')
|
||||
}
|
||||
const settingsPayload = normalizeGetSettingsPayload(unwrapSettingWarehousePayload(settingsRes as Record<string, unknown>))
|
||||
applySettingsPayload(settingsPayload)
|
||||
// 先解析字段元信息(schema),再解析配置:配置里的 object / array 子项按 meta 归一化
|
||||
const metaPayload = unwrapSettingWarehousePayload(metaRes as Record<string, unknown>)
|
||||
const metaList = metaRes && okCode(metaRes.code) ? metaPayload.list : null
|
||||
const parsed = normalizeFieldMetaList(metaList)
|
||||
normalizedFields.value = parsed
|
||||
useMetaForm.value = parsed.length > 0
|
||||
const settingsPayload = normalizeGetSettingsPayload(
|
||||
unwrapSettingWarehousePayload(settingsRes as Record<string, unknown>),
|
||||
parsed
|
||||
)
|
||||
applySettingsPayload(settingsPayload)
|
||||
ensureFieldDefaults(subjectConfig, displayFields.value)
|
||||
if (metaRes && !okCode(metaRes.code)) {
|
||||
ElMessage.warning(metaRes.message || '字段元信息获取失败,已使用默认表单字段')
|
||||
@ -660,13 +668,20 @@ async function handleSave() {
|
||||
const payloadConfig = { ...subjectConfig }
|
||||
saving.value = true
|
||||
try {
|
||||
const body = buildSaveSettingsRequest({ subject_config: payloadConfig, cache_warehouse_id: cacheWarehouseId.value })
|
||||
const body = buildSaveSettingsRequest({
|
||||
subject_config: payloadConfig,
|
||||
cache_warehouse_id: cacheWarehouseId.value,
|
||||
schema: displayFields.value
|
||||
})
|
||||
const res = await settingWarehouseSaveSettings(body)
|
||||
if (!res || !okCode(res.code)) {
|
||||
throw new Error(res?.message || '保存失败')
|
||||
}
|
||||
ElMessage.success(res.message || '保存成功')
|
||||
const savePayload = normalizeSaveSettingsPayload(unwrapSettingWarehousePayload(res as Record<string, unknown>))
|
||||
const savePayload = normalizeSaveSettingsPayload(
|
||||
unwrapSettingWarehousePayload(res as Record<string, unknown>),
|
||||
displayFields.value
|
||||
)
|
||||
applySettingsPayload(savePayload)
|
||||
const cleared = savePayload.cache_cleared_warehouse_id
|
||||
if (cleared != null) ElMessage.info(`已清理仓库缓存:${cleared}`)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user