Commit e2bf6d76 by 张立顺

fix 保存逻辑修复

1 parent db284078
...@@ -18,7 +18,7 @@ export const appId = "wx570d9331f9ace0bd"; ...@@ -18,7 +18,7 @@ export const appId = "wx570d9331f9ace0bd";
* 4预发布 * 4预发布
* 5cdptest * 5cdptest
*/ */
let index = 1; let index = 2;
const accountInfo = wx.getAccountInfoSync(); const accountInfo = wx.getAccountInfoSync();
const systemInfo = wx.getSystemInfoSync(); const systemInfo = wx.getSystemInfoSync();
const envName = accountInfo.miniProgram.envVersion; const envName = accountInfo.miniProgram.envVersion;
......
...@@ -34,7 +34,9 @@ sequenceDiagram ...@@ -34,7 +34,9 @@ sequenceDiagram
| 3 | 持久化 list → **childrenValue** 入库 jsonContent | `serializeFormListForAppJsonStorage(formListForJson)` | | 3 | 持久化 list → **childrenValue** 入库 jsonContent | `serializeFormListForAppJsonStorage(formListForJson)` |
| 4 | 请求体 | [`buildDailyReportSaveRequest`](../form-engine/save-request.js) | | 4 | 请求体 | [`buildDailyReportSaveRequest`](../form-engine/save-request.js) |
**显隐与持久化**:模版级隐藏(调休、品牌/场景过滤等)**仅隐藏 UI**,不清 `values`;`jsonContent` 写入仍有值的隐藏块供再次编辑回显。`fieldDataList` / `subformDataList` / 校验仍仅**当前可见**字段。subform 行内 `childrenShowRules` 联动切换仍清 hidden cell(如洽谈「是否成交」否)。 **显隐与持久化**:模版级隐藏(调休、品牌/场景过滤等)**仅隐藏 UI**;调休等规则隐藏块仍可在 `jsonContent` 中持久化供再次编辑。`fieldDataList` / `subformDataList` / 校验仍仅**当前可见**字段。subform 行内 `childrenShowRules` 联动切换仍清 hidden cell(如洽谈「是否成交」否)。
**静态 group 入库**:`jsonContent` / `requestJson` 中 `type: 'group'` 的 **`children` 与 UI 一致**,仅包含当前 `baseSceneCode`、品牌、`sceneType` 下可见的子字段(`getVisibleGroupChildren`),**精品保存不含延保子项、延保保存不含精品子项**,避免 App 按 `jsonContent` 回显时跨场景字段泄漏。跨场景不可见子字段的 `values` 在显隐刷新时清理(对齐 App `itemShow` 隐藏清值)。
**HTTP 传输**:`fieldDataList` / `subformDataList` 不再作为顶层字段;由 [`save-request.js`](../form-engine/save-request.js) 组装 **`requestJson`**(`JSON.stringify({ fieldDataList, subformDataList?, carBrandList? })`,与 `jsonContent` 同级字符串)。空 `subformDataList` 在 `requestJson` 对象内**不传**;`carBrandList` 在 **`dynamic.save` 与 `dynamic.draft.save`** 的 `requestJson` 中均写入(`pageContext.carBrandList` 非空时)。`buildDailyReportSaveRequest` 返回值中的 lists 仍为数组,供 mock 回显。 **HTTP 传输**:`fieldDataList` / `subformDataList` 不再作为顶层字段;由 [`save-request.js`](../form-engine/save-request.js) 组装 **`requestJson`**(`JSON.stringify({ fieldDataList, subformDataList?, carBrandList? })`,与 `jsonContent` 同级字符串)。空 `subformDataList` 在 `requestJson` 对象内**不传**;`carBrandList` 在 **`dynamic.save` 与 `dynamic.draft.save`** 的 `requestJson` 中均写入(`pageContext.carBrandList` 非空时)。`buildDailyReportSaveRequest` 返回值中的 lists 仍为数组,供 mock 回显。
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* buildDynamicSavePayload:lists 仅可见;jsonContent 含持久化隐藏值 * buildDynamicSavePayload:lists 仅可见;jsonContent 含持久化隐藏值
* parseJsonContentDetailValues:仅从 jsonContent 解析 values;radio/select/multipleSelect 归一化 * parseJsonContentDetailValues:仅从 jsonContent 解析 values;radio/select/multipleSelect 归一化
*/ */
import { getVisibleList, getVisibleLeafFields } from './visibility.js' import { getVisibleList, getVisibleLeafFields, getVisibleGroupChildren } from './visibility.js'
import { normalizeRadioValuesInMap } from './radio-value.js' import { normalizeRadioValuesInMap } from './radio-value.js'
import { normalizeSelectValuesInMap, coerceSelectValue } from './select-value.js' import { normalizeSelectValuesInMap, coerceSelectValue } from './select-value.js'
import { normalizeMultipleSelectValuesInMap, coerceMultipleSelectValue } from './multiple-select-value.js' import { normalizeMultipleSelectValuesInMap, coerceMultipleSelectValue } from './multiple-select-value.js'
...@@ -55,9 +55,13 @@ function cloneJson(data) { ...@@ -55,9 +55,13 @@ function cloneJson(data) {
return JSON.parse(JSON.stringify(data)) return JSON.parse(JSON.stringify(data))
} }
function applyStaticGroupValues(field = {}, values = {}) { function applyStaticGroupValues(field = {}, values = {}, ctx = {}, schemaList = []) {
const next = { ...field } const next = { ...field }
const children = getStaticGroupChildren(field).map(child => { const childrenSource =
ctx && schemaList?.length
? getVisibleGroupChildren(field, ctx, values, schemaList)
: getStaticGroupChildren(field)
const children = childrenSource.map(child => {
const nextChild = { ...child } const nextChild = { ...child }
if (values[child.key] !== undefined) { if (values[child.key] !== undefined) {
nextChild.value = values[child.key] nextChild.value = values[child.key]
...@@ -67,7 +71,7 @@ function applyStaticGroupValues(field = {}, values = {}) { ...@@ -67,7 +71,7 @@ function applyStaticGroupValues(field = {}, values = {}) {
return { ...next, children } return { ...next, children }
} }
function applyValuesToFormList(list = [], values = {}) { function applyValuesToFormList(list = [], values = {}, ctx = {}, schemaList = []) {
return (list || []).map(field => { return (list || []).map(field => {
if (field.type === 'subform') { if (field.type === 'subform') {
const nextGroup = { ...field } const nextGroup = { ...field }
...@@ -81,7 +85,7 @@ function applyValuesToFormList(list = [], values = {}) { ...@@ -81,7 +85,7 @@ function applyValuesToFormList(list = [], values = {}) {
return nextGroup return nextGroup
} }
if (field.type === 'group') { if (field.type === 'group') {
return applyStaticGroupValues(field, values) return applyStaticGroupValues(field, values, ctx, schemaList)
} }
const nextField = { ...field } const nextField = { ...field }
if (values[field.key] !== undefined) { if (values[field.key] !== undefined) {
...@@ -98,9 +102,9 @@ export function mergeValuesIntoSchema(schemaRoot = {}, values = {}) { ...@@ -98,9 +102,9 @@ export function mergeValuesIntoSchema(schemaRoot = {}, values = {}) {
return nextSchema return nextSchema
} }
/** 构建带当前值的可见 formList(用于 jsonContent 提交) */ /** 构建带当前值的可见 formList(用于 jsonContent 提交);group children 与 UI 一致按场景/品牌过滤 */
export function buildVisibleFormList(visibleList = [], values = {}) { export function buildVisibleFormList(visibleList = [], values = {}, ctx = {}, schemaList = []) {
return applyValuesToFormList(cloneJson(visibleList), values) return applyValuesToFormList(cloneJson(visibleList), values, ctx, schemaList)
} }
/** 保存前收集 engine 最新 values(subform 以 groupRowsCache 为准与 UI 对齐) */ /** 保存前收集 engine 最新 values(subform 以 groupRowsCache 为准与 UI 对齐) */
...@@ -141,9 +145,13 @@ function mergeStorageValues(saveValues = {}, engine, visibleList = []) { ...@@ -141,9 +145,13 @@ function mergeStorageValues(saveValues = {}, engine, visibleList = []) {
function assembleFormListForSaveListsWithValues(engine, visibleList = [], saveValues = {}) { function assembleFormListForSaveListsWithValues(engine, visibleList = [], saveValues = {}) {
const cache = engine.getGroupRowsCache?.() || {} const cache = engine.getGroupRowsCache?.() || {}
const ctx = engine.getPageContext?.() || {}
const schemaList = engine.getSchemaList?.() || []
const topLevel = buildVisibleFormList( const topLevel = buildVisibleFormList(
visibleList.filter(field => field.type !== 'subform'), visibleList.filter(field => field.type !== 'subform'),
saveValues saveValues,
ctx,
schemaList
) )
const subformSections = buildAppJsonContent(visibleList, cache) const subformSections = buildAppJsonContent(visibleList, cache)
return [...topLevel, ...subformSections] return [...topLevel, ...subformSections]
......
/** 表单 values 读写与隐藏字段清理 — @see packageDynamicForm/docs/ARCHITECTURE.md */ /** 表单 values 读写与隐藏字段清理 — @see packageDynamicForm/docs/ARCHITECTURE.md */
import { getVisibleList, resolveEffectiveRowFields, getVisibleGroupChildFields } from './visibility.js' import {
getVisibleList,
resolveEffectiveRowFields,
getVisibleGroupChildFields
} from './visibility.js'
import { import {
listSubforms, listSubforms,
listStaticGroups,
getStaticGroupChildren, getStaticGroupChildren,
getSubformRowTemplate, getSubformRowTemplate,
getSubformRowTemplateForAdd getSubformRowTemplateForAdd
} from './field-types.js' } from './field-types.js'
import { failsStructuralPersistVisibility } from './json-content-persist.js'
import { getGroupRows } from './group-values.js' import { getGroupRows } from './group-values.js'
import { normalizeFieldValueByType } from './field-types/registry.js' import { normalizeFieldValueByType } from './field-types/registry.js'
import { normalizeSelectValuesInMap } from './select-value.js' import { normalizeSelectValuesInMap } from './select-value.js'
...@@ -101,7 +107,40 @@ export function setFieldValue(values, key, value) { ...@@ -101,7 +107,40 @@ export function setFieldValue(values, key, value) {
} }
} }
/** 清除误挂在顶层的 subform 列 key;模版级隐藏字段保留 values 供 jsonContent 回显 */ /** 清除静态 group / 顶层字段中因场景、品牌不匹配的 values(对齐 App 跨场景不入库) */
export function clearHiddenStaticGroupChildValues(list, ctx, values) {
const visibilityOptions = { schemaList: list }
const nextValues = { ...values }
let changed = false
const clearIfStructuralMismatch = field => {
if (!field?.key) {
return
}
if (!failsStructuralPersistVisibility(field, ctx, nextValues, visibilityOptions)) {
return
}
if (nextValues[field.key] !== undefined) {
delete nextValues[field.key]
changed = true
}
}
;(list || []).forEach(field => {
if (field.type === 'group' || field.type === 'subform' || field.type === 'divider') {
return
}
clearIfStructuralMismatch(field)
})
listStaticGroups(list).forEach(group => {
getStaticGroupChildren(group).forEach(clearIfStructuralMismatch)
})
return changed ? nextValues : values
}
/** 清除误挂在顶层的 subform 列 key;跨场景(baseSceneCode/品牌)不匹配字段一并清理 */
export function clearHiddenFieldValues(list, ctx, values) { export function clearHiddenFieldValues(list, ctx, values) {
const subformChildKeys = new Set() const subformChildKeys = new Set()
listSubforms(list).forEach(subform => { listSubforms(list).forEach(subform => {
...@@ -116,7 +155,7 @@ export function clearHiddenFieldValues(list, ctx, values) { ...@@ -116,7 +155,7 @@ export function clearHiddenFieldValues(list, ctx, values) {
delete nextValues[key] delete nextValues[key]
} }
}) })
return nextValues return clearHiddenStaticGroupChildValues(list, ctx, nextValues)
} }
/** 清除 subform 行内不可见 cell 的值(场景 spec + childrenShowRules) */ /** 清除 subform 行内不可见 cell 的值(场景 spec + childrenShowRules) */
......
...@@ -999,6 +999,8 @@ describe('submit adapter', async () => { ...@@ -999,6 +999,8 @@ describe('submit adapter', async () => {
const wenjie = jsonContent.find(item => item.key === 'F1-5') const wenjie = jsonContent.find(item => item.key === 'F1-5')
const wenjieField = wenjie.children.find(item => item.key === 'F1-5-1') const wenjieField = wenjie.children.find(item => item.key === 'F1-5-1')
assert.equal(wenjieField.value, 1) assert.equal(wenjieField.value, 1)
const wenjieChildKeys = wenjie.children.map(item => item.key).sort()
assert.deepEqual(wenjieChildKeys, ['F1-5-1', 'F1-5-2', 'F1-5-3'])
}) })
it('IT-API-004b: buildDynamicSavePayload off-duty jsonContent persists hidden brand values', () => { it('IT-API-004b: buildDynamicSavePayload off-duty jsonContent persists hidden brand values', () => {
......
...@@ -76,6 +76,14 @@ describe('hidden field clear on linkage toggle', async () => { ...@@ -76,6 +76,14 @@ describe('hidden field clear on linkage toggle', async () => {
assert.ok(!state.visibleList.some(item => item.key === 'F1-5')) assert.ok(!state.visibleList.some(item => item.key === 'F1-5'))
}) })
it('IT-HIDDEN-CLEAR-004: warranty ctx clears boutique group child values on init', () => {
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, warrantyCtx)
const state = engine.getViewState()
assert.equal(state.values['F1-5-1'], undefined)
})
it('IT-HIDDEN-CLEAR-003: save jsonContent excludes hidden subform cells after toggle', () => { it('IT-HIDDEN-CLEAR-003: save jsonContent excludes hidden subform cells after toggle', () => {
const engine = createFormEngine(negotiationSchema) const engine = createFormEngine(negotiationSchema)
engine.init(negotiationSchema, warrantyCtx) engine.init(negotiationSchema, warrantyCtx)
......
...@@ -24,6 +24,17 @@ describe('jsonContentPersist', async () => { ...@@ -24,6 +24,17 @@ describe('jsonContentPersist', async () => {
merchantId: '100', merchantId: '100',
sceneList: [{ baseSceneCode: 100101 }] sceneList: [{ baseSceneCode: 100101 }]
} }
const warrantyCtx = {
baseSceneCode: 110201,
brandIds: [3454],
merchantId: '100',
sceneList: [{ baseSceneCode: 110201 }]
}
function assertGroupChildrenKeys(group, expectedKeys, label) {
const childKeys = (group?.children || []).map(item => item.key).sort()
assert.deepEqual(childKeys, [...expectedKeys].sort(), label)
}
it('IT-STAFF-SAVE-SCENE-001: boutique save excludes warranty-only fields and template-0 cross-scene groups', () => { it('IT-STAFF-SAVE-SCENE-001: boutique save excludes warranty-only fields and template-0 cross-scene groups', () => {
const initialValues = mergeTemplateDefaultsIntoInitialValues( const initialValues = mergeTemplateDefaultsIntoInitialValues(
...@@ -49,6 +60,54 @@ describe('jsonContentPersist', async () => { ...@@ -49,6 +60,54 @@ describe('jsonContentPersist', async () => {
} }
}) })
it('IT-STAFF-SAVE-SCENE-002: boutique save group children exclude warranty sub-fields', () => {
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, boutiqueCtx)
engine.setFieldValue('F1-1', '在岗')
engine.setFieldValue('F1-5-1', 1)
engine.setFieldValue('F1-5-2', 2)
engine.setFieldValue('F1-5-3', 3)
const payload = buildDynamicSavePayload({
reportTemplateId: 1001,
engine,
reportDate: '2026-06-15',
normalizedFieldDataList: true
})
const f15 = JSON.parse(payload.jsonContent).find(item => item.key === 'F1-5')
assert.ok(f15)
assertGroupChildrenKeys(
f15,
['F1-5-1', 'F1-5-2', 'F1-5-3'],
'精品保存不应写入延保子字段'
)
})
it('IT-STAFF-SAVE-SCENE-003: warranty save group children exclude boutique sub-fields', () => {
const engine = createFormEngine(staffSchema)
engine.init(staffSchema, warrantyCtx)
engine.setFieldValue('F1-1', '在岗')
engine.setFieldValue('F1-5-4', 4)
engine.setFieldValue('F1-5-5', 5)
engine.setFieldValue('F1-5-6', 6)
const payload = buildDynamicSavePayload({
reportTemplateId: 1001,
engine,
reportDate: '2026-06-15',
normalizedFieldDataList: true
})
const f15 = JSON.parse(payload.jsonContent).find(item => item.key === 'F1-5')
assert.ok(f15)
assertGroupChildrenKeys(
f15,
['F1-5-4', 'F1-5-5', 'F1-5-6'],
'延保保存不应写入精品子字段'
)
const jsonKeys = JSON.parse(payload.jsonContent).map(item => item.key)
assert.ok(jsonKeys.includes('F1-2'), '延保顶层字段应入库')
})
it('IT-ECHO-SCENE-001: wrong-scene group in jsonContent does not expand children via forceEchoBlock', () => { it('IT-ECHO-SCENE-001: wrong-scene group in jsonContent does not expand children via forceEchoBlock', () => {
const warrantyGroup = staffSchema.list.find( const warrantyGroup = staffSchema.list.find(
item => item =>
...@@ -99,5 +158,10 @@ describe('jsonContentPersist', async () => { ...@@ -99,5 +158,10 @@ describe('jsonContentPersist', async () => {
const jsonContent = JSON.parse(payload.jsonContent) const jsonContent = JSON.parse(payload.jsonContent)
const f15 = jsonContent.find(item => item.key === 'F1-5') const f15 = jsonContent.find(item => item.key === 'F1-5')
assert.ok(f15, '调休时应持久化已填品牌 group') assert.ok(f15, '调休时应持久化已填品牌 group')
assertGroupChildrenKeys(
f15,
['F1-5-1', 'F1-5-2', 'F1-5-3'],
'调休持久化 group 仍只含精品子字段'
)
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!