ARCHITECTURE.md 9.71 KB

packageDynamicForm 架构与数据流

分层职责

层 目录 职责
Page pages/*DailyReport*.js 周历、只读态、loadSeq 竞态、buildFormRequestContext、engine.init、提交
Loader services/*FormLoader.js 并行 bootstrap(brand/scene/detail)→ pageContext → schema
API 门面 api/*DailyReport.js 页面/Loader 调用的稳定方法;组装 pageContext、save payload
Adapter api/*DailyReportAdapters.js mock/real 分支;sceneCache / schemaCache / brandCache
Adapter api/negotiationTodayPlanStoreAdapters.js 洽谈今日计划到店 count/list(todayPlanStore 开关)
Engine form-engine/ 显隐、values、校验、normalized save payload
Field UI components/form-field-* 按 schema type 渲染;optionsKey → fieldOptionsService
Config config/apiSwitch.js configCenter.js 联调开关、配置中心 schema 拉取与 indexed → list 转换

端到端序列(切日期加载)

sequenceDiagram
  participant Page as DailyReportPage
  participant Loader as FormLoader
  participant API as DailyReportApi
  participant Adapter as Adapters
  participant Engine as formEngine
  participant UI as dynamicForm

  Page->>Page: dayClick / lastWeek / nextWeek
  Page->>Page: loadFormData loadSeq++
  Page->>Loader: loadXxxDailyForm
  par bootstrap
    Loader->>API: fetchMerchantBrandIds
    API->>Adapter: fetchBrandAdapter
    Loader->>API: fetchMerchantScene
    API->>Adapter: fetchSceneAdapter
    Loader->>API: fetchReportDetail
    API->>Adapter: fetchDetailAdapter
  end
  Loader->>API: fetchPageContext
  Loader->>API: fetchSchema
  API->>Adapter: fetchSchemaRootAdapter
  Loader-->>Page: pageContext schema detail
  Page->>Engine: init schema pageContext
  Page->>UI: applyEngineView fixedFields groupFields values

写入与保存

sequenceDiagram
  participant UI as formField
  participant Page as DailyReportPage
  participant Engine as formEngine
  participant Assemble as assembleFormListForSaveLists
  participant Submit as buildAppDailyReportSubmitPayload
  participant Save as buildDailyReportSaveRequest

  UI->>Page: change / groupchange
  Page->>Engine: setFieldValue / setGroupFieldValue
  Page->>Page: submit or saveDraft
  Page->>Engine: validate
  Page->>Assemble: formList 含 subform.children
  Page->>Submit: formList
  Submit->>Submit: lists 再 serialize childrenValue
  Page->>Save: jsonContent fieldDataList subformDataList
  Save->>Adapter: dynamic.save or dynamic.draft.save

详见 daily-report-save-and-json.md。

洽谈今日计划客户导入

sequenceDiagram
  participant Page as negotiationDetail
  participant Popup as todayPlanCustomerPopup
  participant API as negotiationDailyReport
  participant Import as negotiationCustomerImport
  participant Engine as formEngine

  Page->>API: fetchTodayPlanStoreCount
  Page->>Popup: show merchantId advanceUserId
  Popup->>API: fetchTodayPlanStoreList inviteDate
  Popup->>Page: confirm customers
  Page->>Import: applyCustomersToEngine
  Import->>Engine: appendGroupRows per visible F3

业务规则与 UI 见 PAGES.md 洽谈节「今日计划到店客户」。

真接口约定(摘要)

能力 形态 入口
Schema 配置中心数字键 { "0": field, ... } → { list } configCenter.normalizeConfigToSchemaRoot
主营品牌 merchants.api.open.merchant.info carBrandApi → fetchBrandAdapter(按 merchantId 缓存)
详情 detail.get + jsonContent + subformDataList fetchDetailAdapter → parseJsonContentDetailValues
场景 staffBaseInfo.merchant.baseSceneCode.list(staffUuid + merchantId + sceneType) fetchSceneAdapter(按 staffUuid:merchantId:sceneType 缓存)
周历 dailyReport.weekly.list fetchWeeklyAdapter(页面独立刷新,不在 Loader 内)
字段选项 getEmployeeList 等 fieldOptionsAdapters + fieldOptionProviders

字段类型要点

选择器 mode 总览见 selector-modes.md。

select / multipleSelect

  • select:标量 string;form-picker-popup mode="single";归一化见 form-engine/select-value.js。
  • multipleSelect:数组 string[];mode="multiple";展示用 / 拼接 label。
  • 选项接口见 field-options-api.md(storeStaffList、4sStaffList 等)。

subform(group-repeater)

  • schema type: subform;行数据在 values[groupKey] 与 groupRowsCache 双写。
  • 入库 jsonContent:childrenValue + origin_children(无 children);lists 由序列化前含 children 的 formList 生成。
  • 回显:mergeAppJsonIntoSchema / getSectionRowFieldsList(兼容 childrenValue 与旧 children)→ parseJsonContentDetailValues。
  • UI 与行数上限详见 group-repeater.md;保存全链路见 daily-report-save-and-json.md。

numberInput

  • 默认 ≥0 非负整数(field-input-limits.js 输入过滤 + validator.js 兜底);schema-normalizer 补 min: 0、integer: true。
  • 金额等需小数:显式 rules.decimal: true(最多两位小数,如驻店 F1-3 延保退单金额)。

radio / 出勤显隐

  • 配置中心可能用中文 label 作 value;radio-value.js 归一化。
  • 驻店 F1-1 调休时隐藏其余字段:visibility.js。

Adapter 缓存

缓存 Key 说明
sceneCache merchantId 切日期不重复打场景接口
schemaCache moduleUrl + baseSceneCode 同场景 schema 复用
brandCache merchantId 切日期不重复打 merchant.info

首屏仍读缓存快速展示;applyEngineView 后由页面 scheduleMetadataCacheRevalidate 空闲后台 skipCacheRead 刷新三缓存,不热更新当前页 UI(下次进页/切日期用新数据)。

三页各自 *DailyReportCache.js;单测 beforeEach 需 clear*DailyReportCache()。

相关单测模块

模块 测试文件
form-engine tests/form-engine.test.mjs
adapters tests/staffDailyReportAdapters.test.mjs storeDailyReportAdapters.test.mjs
loaders tests/staffDailyFormLoader.test.mjs storeDailyFormLoader.test.mjs
fieldOptions tests/fieldOptionsAdapters.test.mjs
菜单路由 tests/dynamicFormMenus.test.mjs

完整索引:integration-test-plan.md。

子表双状态不变式(排查必读)

子表(type: subform)在引擎内维护两份状态,修「有值但校验空 / 提交丢行 / 切日期残留行」时按下列顺序核对,不要先改组件。

状态 结构 用途
values[groupKey] valueMap[] 校验、提交序列化(validate 前须与 cache 同步)
groupRowsCache[groupKey] FormField[][] 与 form-group-repeater 绑定;洽谈 App 双 key 写 cache

不变式(任一步破坏即可能出 bug)

  1. 加载:buildInitialValues → buildGroupRowsCache → applyVisibilityClearing(clearHiddenFieldValues 仅清理误挂顶层的 subform 列 key;clearHiddenSubformRowValues 清行内联动 hidden;loadSchema / init 内可能执行两次)。
  2. 子表单元格编辑:先写 groupRowsCache(setRowFieldInCache / setNegotiationDualKeyFieldInCache)→ syncValuesFromGroupCache → applyVisibilityClearing → alignGroupRowsCacheAfterHiddenClear(隐藏 subform 时删 cache key)。
  3. 校验:validate() 开头 syncSubformValuesFromCache(cache → values)。
  4. 保存:collectSaveValues / collectSubformStorageValues 对 subform 以 cache 为准覆盖 values[groupKey];lists 用 assembleFormListForSaveLists(可见),jsonContent 用 assembleFormListForJsonContent(持久化隐藏值)。
  5. 对外写入:Page 只调 engine.setGroupFieldValue / setAppGroupFieldValue / mutateSubform;勿直接改 getViewState().groupRowsCache 再写回。

开发态可在 form-engine/subform-invariants.js 调用 assertSubformCacheValuesAligned(单测与调试)。

修改字段 type 触达文件表

新增或修改某 type 的行为时,按类型勾选(实现优先查 form-engine/field-types/registry.js):

关注点 路径
策略注册 / 组件名 form-engine/field-types/registry.js、components/dynamic-form/dynamic-form.js
初始值 form-engine/values.js、field-types/* 的 normalizeInitialValue
写入归一化 field-types/* 的 normalizeInput;子表经 core.mutateSubform
校验 form-engine/validator.js、field-types/* 的 validate
提交 / storageType form-engine/field-submit-value.js、app-subform-json.js
显隐(若与 type 无关则跳过) form-engine/visibility/、visibility.js
选项下拉 api/fieldOptions*.js、docs/field-options-api.md
三页 Page 差异 pages/*DailyReport.js(仅编排;逻辑勿塞进组件)
单测 tests/form-engine.test.mjs、*-template-schema.test.mjs、新增 type 时补 registry 用例

架构演进模块(2026-06)

模块 说明
services/dailyReportBootstrap.js 三页共用 Loader 编排(Template Method)
pages/behaviors/dailyReportPage.js 共用 loadSeq / applyEngineView / ensureEngine
form-engine/field-types/ 字段类型 Strategy Registry
form-engine/visibility/specs/ 显隐 Specification
form-engine/detail-echo.js 详情回显 ACL;init 使用 initialValues,schema 保持模版
form-engine/save-policies.js 按 templateType 选择保存 payload 构建器