diff --git a/src/api/setting.warehouse/settingwarehouse.ts b/src/api/setting.warehouse/settingwarehouse.ts index 96272caf..27a5b258 100644 --- a/src/api/setting.warehouse/settingwarehouse.ts +++ b/src/api/setting.warehouse/settingwarehouse.ts @@ -211,12 +211,31 @@ function schemaFlagValue(value: unknown, fallback: unknown): boolean { const SCHEMA_FLAG_TYPES = new Set(['flag', 'boolean', 'bool', 'switch']) const SCHEMA_NUMBER_TYPES = new Set(['number', 'integer', 'int', 'float', 'decimal']) +/** 嵌套 object(如售后跟进时间)里代表「该类型默认值」的子键 */ +const SCHEMA_DEFAULT_SUB_KEY = '_default' +/** 嵌套 object 叶子所在层级:缺省视作「未覆盖」,不注入 meta 默认值(留空 = 跟随服务端默认) */ +const NESTED_LEAF_DEPTH = 3 -/** 按字段 schema 归一化单个值;返回 undefined 表示该键不写入 */ +function isPlainObjectValue(value: unknown): value is Record { + return !!value && typeof value === 'object' && !Array.isArray(value) +} + +/** 是否为「扁平标量」配置值(数字或非空字符串) */ +function isScalarConfigValue(value: unknown): boolean { + if (typeof value === 'number') return Number.isFinite(value) + if (typeof value === 'string') return value.trim() !== '' + return false +} + +/** + * 按字段 schema 归一化单个值;返回 undefined 表示该键不写入。 + * depth 为当前字段在配置树中的层级(顶层字段 depth=1),用于区分「一级 object」与「嵌套 object」。 + */ function normalizeSchemaValue( value: unknown, field: WarehouseSubjectFieldSchema, - present: boolean + present: boolean, + depth = 1 ): unknown { const type = String(field.type ?? '').toLowerCase() const fallback = field.defaultValue !== undefined ? field.defaultValue : field.default @@ -224,12 +243,14 @@ function normalizeSchemaValue( // flag / boolean:兼容 true|false、1|0、'1'|'0',非法或缺省回退默认 if (SCHEMA_FLAG_TYPES.has(type)) { - if (!present && fallback === undefined) return undefined + if (!present && (fallback === undefined || depth >= NESTED_LEAF_DEPTH)) return undefined return schemaFlagValue(present ? value : fallback, fallback) } // number / integer:非法或缺省回退默认(无默认则不写入) if (SCHEMA_NUMBER_TYPES.has(type)) { + // 嵌套 object 的叶子(如售后跟进时间的各子类型)缺省时视为「未覆盖」:不写入、不注入默认值 + if (!present && depth >= NESTED_LEAF_DEPTH) return undefined const num = Number(present ? value : fallback) if (Number.isFinite(num)) return num return fallback === undefined ? undefined : fallback @@ -238,13 +259,22 @@ function normalizeSchemaValue( // 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) - : fallback && typeof fallback === 'object' && !Array.isArray(fallback) - ? (fallback as Record) - : {} - return normalizeSchemaObject(source, children) + // 后端可能用「扁平标量」表示该类型下所有子类型同值(等价于只设置 _default), + // 例如 after_sale_followup_hours.order = "24";必须优先识别,否则会被内层默认值覆盖。 + if (present && isScalarConfigValue(value)) { + const defaultChild = children.find((child) => child.key === SCHEMA_DEFAULT_SUB_KEY) + if (!defaultChild) return value + const normalized = normalizeSchemaValue(value, defaultChild, true, depth + 1) + return normalized === undefined ? value : { [SCHEMA_DEFAULT_SUB_KEY]: normalized } + } + const source = isPlainObjectValue(value) ? value : isPlainObjectValue(fallback) ? fallback : null + // 嵌套 object(depth>=2)没有任何实际值时视为「未覆盖」:既不写入,也不注入内层默认值, + // 交给服务端按 meta 默认处理(页面据此显示为留空)。 + if (!source) return depth >= 2 ? undefined : normalizeSchemaObject({}, children, depth + 1) + const normalized = normalizeSchemaObject(source, children, depth + 1) + // 嵌套 object 归一化后为空(子项均未覆盖)→ 不写入该键 + if (depth >= 2 && Object.keys(normalized).length === 0) return undefined + return normalized } // array:逐项按子项 schema 归一化 @@ -252,9 +282,7 @@ function normalizeSchemaValue( 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, children) - : item + isPlainObjectValue(item) ? normalizeSchemaObject(item, children, depth + 1) : item ) } @@ -267,19 +295,20 @@ function normalizeSchemaValue( } // string / color / select / location:有值原样保留,缺省用默认 - if (!present) return fallback + if (!present) return depth >= NESTED_LEAF_DEPTH ? undefined : fallback return value } -/** 按子项 schema 递归归一化对象(只保留 schema 中声明的子项) */ +/** 按子项 schema 递归归一化对象(只保留 schema 中声明的子项);depth 为子项所在层级 */ function normalizeSchemaObject( source: Record, - children: WarehouseSubjectFieldSchema[] + children: WarehouseSubjectFieldSchema[], + depth = 1 ): Record { const out: Record = {} for (const child of children) { if (!child?.key) continue - const normalized = normalizeSchemaValue(source[child.key], child, child.key in source) + const normalized = normalizeSchemaValue(source[child.key], child, child.key in source, depth) if (normalized !== undefined) out[child.key] = normalized } return out diff --git a/src/views/system/setting/basicManagement/newview/components/ObjectFieldEditor.vue b/src/views/system/setting/basicManagement/newview/components/ObjectFieldEditor.vue new file mode 100644 index 00000000..f3644581 --- /dev/null +++ b/src/views/system/setting/basicManagement/newview/components/ObjectFieldEditor.vue @@ -0,0 +1,197 @@ + + + + + + diff --git a/src/views/system/setting/basicManagement/newview/pages/WarehouseSettingsForm.vue b/src/views/system/setting/basicManagement/newview/pages/WarehouseSettingsForm.vue index e9e54b24..3d1befa6 100644 --- a/src/views/system/setting/basicManagement/newview/pages/WarehouseSettingsForm.vue +++ b/src/views/system/setting/basicManagement/newview/pages/WarehouseSettingsForm.vue @@ -7,7 +7,7 @@ viewType='form' 渲染替换默认的列表结构。 -->