V1.0.0
Skip to content

JSON 消息格式 ​

A2UI 通过 JSON 消息驱动页面渲染。本文档汇总所有 JSON 配置格式,方便编写和查阅。


完整示例 ​

下面是一个包含所有特性的 JSON 消息示例,涵盖创建 Surface、组件树、数据绑定、事件绑定、初始数据加载等:

json
[
  {
    "version": "v0.9",
    "createSurface": {
      "surfaceId": "main",
      "catalogId": "default",
      "eventInputParams": [{ "userId": "user_001", "role": "admin" }]
    }
  },
  {
    "version": "v0.9",
    "updateDataModel": {
      "surfaceId": "main",
      "path": "/clickCount",
      "value": 0
    }
  },
  {
    "version": "v0.9",
    "updateComponents": {
      "surfaceId": "main",
      "components": [
        {
          "id": "row_main",
          "component": "WRow",
          "gutter": 16,
          "children": ["col_left", "col_right"]
        },
        {
          "id": "col_left",
          "component": "WCol",
          "span": 12,
          "children": ["input_name"]
        },
        {
          "id": "col_right",
          "component": "WCol",
          "span": 12,
          "children": ["btn_submit"]
        },
        {
          "id": "input_name",
          "component": "WInput",
          "placeholder": "请输入姓名",
          "modelValue": { "path": "/userName" },
          "action": {
            "event": {
              "name": "onInput",
              "type": "input"
            }
          }
        },
        {
          "id": "btn_submit",
          "component": "WButton",
          "type": "primary",
          "action": {
            "event": {
              "name": "submit",
              "desc": "提交表单",
              "api": {
                "url": "/api/submit",
                "constParams": [{ "name": "source", "value": "web" }]
              },
              "context": [
                { "key": "userName", "value": { "path": "/userName" } },
                { "key": "clickCount", "value": { "path": "/clickCount" } }
              ]
            }
          },
          "child": "btn_text"
        },
        {
          "id": "btn_text",
          "component": "Text",
          "text": "提交"
        }
      ]
    }
  }
]

以下分类型详细说明各字段格式。


快速开始 ​

如果你是第一次编写 A2UI JSON,从最简单的页面开始:

最小示例(一个按钮):

json
[
  {
    "version": "v0.9",
    "createSurface": { "surfaceId": "main", "catalogId": "default" }
  },
  {
    "version": "v0.9",
    "updateComponents": {
      "surfaceId": "main",
      "components": [
        {
          "id": "btn_hello",
          "component": "WButton",
          "type": "primary",
          "child": "btn_text"
        },
        {
          "id": "btn_text",
          "component": "Text",
          "text": "门诊工作站"
        }
      ]
    }
  }
]

三步理解:

  1. createSurface - 创建一个 UI 容器(必须先创建才能渲染)
  2. updateComponents - 定义组件树(按钮、文本、布局等)
  3. 组件通过 child / children 引用子组件

详细说明继续往下看 ↓


消息类型 ​

A2UI v0.9 支持四种消息类型:

消息类型作用示例场景
createSurface创建一个 UI 容器初始化页面、打开新窗口
updateComponents注册/更新组件树渲染页面内容、动态更新组件
updateDataModel更新数据模型设置表单默认值、更新列表数据
deleteSurface删除 Surface关闭窗口、清理页面

createSurface ​

创建一个新的 Surface(UI 容器),每个 Surface 有独立的组件树和数据模型。

json
{
  "version": "v0.9",
  "createSurface": {
    "surfaceId": "main",
    "catalogId": "default",
    "eventInputParams": [{ "userId": "user_001", "role": "admin" }]
  }
}
字段类型必填说明
surfaceIdstring是Surface 唯一标识
catalogIdstring否组件目录 ID,默认 default
eventInputParamsarray否业务参数,会在所有事件中原样返回
themeobject否主题配置

updateComponents ​

注册或更新组件树,是渲染 UI 的核心消息。

json
{
  "version": "v0.9",
  "updateComponents": {
    "surfaceId": "main",
    "components": [
      {
        "id": "row1",
        "component": "WRow",
        "gutter": 16,
        "children": ["col1", "col2"]
      },
      {
        "id": "col1",
        "component": "WCol",
        "span": 12,
        "children": ["btn_submit"]
      },
      {
        "id": "btn_submit",
        "component": "WButton",
        "type": "primary",
        "action": {
          "event": { "name": "submit", "desc": "提交表单" }
        },
        "child": "btn_text"
      },
      {
        "id": "btn_text",
        "component": "Text",
        "text": "提交"
      }
    ]
  }
}

组件通用字段 ​

每个组件对象都包含以下通用字段:

字段类型必填说明
idstring是组件唯一标识
componentstring是组件类型,如 WButton、WInput
weightnumber否渲染权重(用于排序)
其他属性any否组件特定属性,如 type、disabled

组件引用关系 ​

组件通过 child 和 children 引用子组件:

字段类型说明
childstring单个子组件的 ID
childrenstring[]多个子组件的 ID 数组
json
// 单子组件
{ "id": "btn", "component": "WButton", "child": "btn_text" }

// 多子组件
{ "id": "row", "component": "WRow", "children": ["col1", "col2"] }

updateDataModel ​

更新数据模型中指定路径的值。

json
{
  "version": "v0.9",
  "updateDataModel": {
    "surfaceId": "main",
    "path": "/formData/name",
    "value": "张三"
  }
}
字段类型必填说明
surfaceIdstring是Surface ID
pathstring是数据路径,以 / 开头
valueany是要设置的值

deleteSurface ​

删除指定的 Surface。

json
{
  "version": "v0.9",
  "deleteSurface": {
    "surfaceId": "main"
  }
}

数据绑定格式 ​

组件属性可以通过数据绑定从 dataModel 获取值,支持多种格式:

DataBindingValue 对象 ​

json
{
  "id": "input_name",
  "component": "WInput",
  "modelValue": { "path": "/formData/name" }
}
字段类型说明
pathstring数据模型路径,如 /formData/name
literalStringstring字符串字面量
literalNumbernumber数字字面量
literalBooleanboolean布尔字面量
literalArrayarray数组字面量(范围值、级联路径等)
literalObjectobject对象字面量(如级联 props 配置)
literalany通用字面量(字符串/数字/布尔)

各组件具体支持哪些字段,见 path 数据绑定速查。

字符串路径 ​

部分属性支持直接使用字符串路径(以 / 开头):

json
{
  "id": "text_name",
  "component": "WText",
  "text": "/formData/name"
}

@ 前缀 ​

@ 前缀表示字段名字面量,常用于表格列绑定:

json
{
  "id": "col_name",
  "component": "WTableColumn",
  "prop": "@patientName",
  "label": "患者姓名"
}

action 配置 ​

action 配置在组件的 action.event 字段中,定义用户交互行为。

基本结构 ​

json
{
  "id": "btn_submit",
  "component": "WButton",
  "action": {
    "event": {
      "name": "submit",
      "desc": "提交表单",
      "api": { "url": "/api/submit" },
      "actions": [],
      "constParams": []
    }
  }
}

action.event 字段 ​

字段类型必填说明
namestring是事件名称,用于回调中识别
typestring否事件类型:click、change、input 等
descstring否动作描述,用于 AI 识别意图
apiobject否API 配置,触发时自动调用后端
actionsLocalAction[]否本地操作规则,触发时直接在前端执行
constParamsArray<{ name, value }>否固定参数,每次请求都会携带

api 配置 ​

配置后,事件触发时自动调用后端 API:

json
{
  "action": {
    "event": {
      "name": "submitOrder",
      "desc": "提交处方",
      "api": {
        "url": "/api/order/submit",
        "method": "POST",
        "headers": { "Authorization": "Bearer xxx" },
        "constParams": [{ "name": "orderType", "value": "urgent" }]
      }
    }
  }
}
字段类型必填说明
urlstring是后端接口地址
methodstring否请求方法,默认 'POST'
headersRecord<string, string>否自定义请求头,默认 { 'Content-Type': 'application/json' },会与默认值合并
constParamsArray<{ name, value }>否固定参数,每次请求都会携带

actions 本地操作 ​

本地操作规则,触发时直接在前端修改数据模型或组件属性,无需调用 API:

json
{
  "action": {
    "event": {
      "name": "lockForm",
      "desc": "锁定表单",
      "actions": [
        {
          "type": "updateProp",
          "componentId": "input_name",
          "prop": "disabled",
          "value": true
        },
        {
          "type": "updateProp",
          "componentId": "input_phone",
          "prop": "disabled",
          "value": true
        },
        { "type": "updateData", "path": "/status", "value": "已锁定" }
      ]
    }
  }
}

LocalAction 类型:

字段类型必填说明
type'updateData' | 'updateProp' | 'updatePangoEditor'是操作类型
surfaceIdstring否目标 Surface ID,默认为触发事件的 surface
componentIdstring否*目标组件 ID(updateProp 时必填,updatePangoEditor 时为编辑器 domId)
pathstring否*数据模型路径(updateData 时必填)
propstring否*属性名(updateProp 时必填)
valueany是设置的值(updatePangoEditor 时传 'reset')

三种操作类型:

类型说明必填字段
updateData更新数据模型中指定路径的值path, value
updateProp更新组件的属性(如 disabled、visible)componentId, prop, value
updatePangoEditor重置 PangoEditor 编辑器(用初始数据重新渲染)value: 'reset',componentId 可选(不传重置所有)

actions 与 api 并存

actions 和 api 可以同时配置。触发时执行顺序:

  1. 先执行本地操作(actions,同步立即生效)
  2. 再调用 API(api,异步)
  3. 同时派发 window 事件 + onEvent

loadEventHandler ​

配置在 processMessages2 的 json 参数中,用于页面加载时自动从后端获取初始数据。

json
{
  "surfaceId": "main",
  "components": [...],
  "loadEventHandler": {
    "url": "/api/loadData",
    "method": "POST",
    "headers": { "Authorization": "Bearer xxx" },
    "desc": "加载患者列表",
    "constParams": [
      { "name": "departmentId", "value": "1001" }
    ]
  }
}
字段类型必填说明
urlstring是后端接口地址
methodstring否请求方法,默认 'POST'
headersRecord<string, string>否自定义请求头,默认 { 'Content-Type': 'application/json' },会与默认值合并
descstring否动作描述,用于回调识别
constParamsArray<{ name, value }>否固定参数

请求格式:

渲染器自动发送请求(默认 POST,可通过 method 配置):

json
{
  "sessionCtx": { "userId": "user_001" },
  "businessParams": { "orderId": "ORD-001" },
  "constParams": { "departmentId": "1001" }
}

数据映射规则:

API 返回的数据会根据组件的 dataSource 路径自动映射到 dataModel:

  1. 按路径名匹配:组件 dataSource 为 /patientList 时,从返回数据中取 patientList 键的值
  2. 单 dataSource 兼容:只有一个 dataSource 且未匹配时,尝试取常见列表字段(list / rows / items / records / data)
  3. 响应格式兼容:支持 { code, data: {...} } 和直接返回数据两种格式

processMessages2 json 结构 ​

processMessages2 接受 4 个参数:sessionCtx、businessParams、json、callback。其中 json 是简化的模板结构,callback 是 API 调用后的回调函数:

typescript
processor.processMessages2(sessionCtx, businessParams, json, callback);

json 结构:

json
{
  "surfaceId": "main",
  "components": [
    { "id": "row1", "component": "WRow", "children": [...] },
    { "id": "input_name", "component": "WInput", "modelValue": { "path": "/name" } },
    { "id": "btn_submit", "component": "WButton", "action": {...} }
  ],
  "data": {
    "name": "张三",
    "phone": "13800138000"
  },
  "loadEventHandler": {
    "url": "/api/loadData",
    "constParams": [...]
  }
}

callback:API 调用后的回调函数(processMessages2 的第 4 个参数)

typescript
// 两种触发场景:
//   - loadEventHandler 加载完成后触发(actionType: 'load')
//   - 按钮 action.event.api 调用后触发(actionType: 'event')
(actionInfo: {
  actionType: 'load' | 'event',  // 触发来源
  actionName: string,             // 'load' 时为空,'event' 时为事件名称
  actionDesc: string              // 动作描述
}, interfaceInfo: any) => void    // 后端 API 返回的 JSON(异常时为 { error: true, message: string })

参数汇总:

字段类型必填说明
surfaceIdstring否Surface ID,默认 @default
componentsarray是组件列表,格式同 updateComponents
dataobject否初始数据,渲染前写入 dataModel
loadEventHandlerobject否初始数据加载配置
callbackfunction是API 调用后的回调函数(loadEventHandler 加载完成或按钮 API 调用后触发)

callback 的详细说明和示例见 调用后端 API - callback 回调


组件事件类型映射 ​

不同组件触发事件时的 type 值:

组件触发时机type 值
WButton点击按钮click
WLink点击链接click
WBreadcrumb点击节点click
WDropdown点击菜单项command
WAlert关闭提示close
WScrollbar滚动scroll
WScrollbar到达边缘end-reached
WInput输入内容input
WAutocomplete输入内容input
WAutocomplete选中建议change
WMention输入内容input
WMention选中提及select
WInputTag标签变化change
WInputNumber数值变化change
WPagination分页变化change
WSelect选择变化change
WSelectV2选择变化change
WTableSelect选择变化change
WTransfer选项移动change
WSwitch状态变化change
WRadioGroup选择变化change
WRate评分变化change
WCheckboxGroup勾选变化change
WSegmented切换变化change
WSlider滑块变化change
WTreeSelect选择变化change
WCascader选择变化change
WColorPicker选择变化change
WDatePicker选择变化change
WTimePicker选择变化change
WTimeSelect选择变化change
WiCheckboxGroup勾选变化change
WiRadioGroup选择变化change
WiListTableSelect选择变化change
WiListTableSelect添加项add
WiListTableSelect删除项remove
WSplitter拖拽结束resize-end
WSplitter面板折叠collapse
WTabs切换标签tab-click
WTabs关闭标签tab-remove

完整示例 ​

一个包含数据绑定、action、loadEventHandler 的完整示例:

json
{
  "surfaceId": "patient_list",
  "components": [
    {
      "id": "row_toolbar",
      "component": "WRow",
      "gutter": 16,
      "children": ["col_search", "col_add"]
    },
    {
      "id": "col_search",
      "component": "WCol",
      "span": 18,
      "children": ["input_search"]
    },
    {
      "id": "input_search",
      "component": "WInput",
      "placeholder": "搜索患者",
      "modelValue": { "path": "/searchKeyword" }
    },
    {
      "id": "col_add",
      "component": "WCol",
      "span": 6,
      "children": ["btn_add"]
    },
    {
      "id": "btn_add",
      "component": "WButton",
      "type": "primary",
      "action": {
        "event": {
          "name": "addPatient",
          "desc": "新增患者",
          "actions": [
            { "type": "updateData", "path": "/dialogVisible", "value": true }
          ]
        }
      },
      "child": "btn_add_text"
    },
    {
      "id": "btn_add_text",
      "component": "Text",
      "text": "新增"
    },
    {
      "id": "table_patients",
      "component": "WTable",
      "dataSource": "/patientList",
      "children": ["col_name", "col_age", "col_action"]
    },
    {
      "id": "col_name",
      "component": "WTableColumn",
      "prop": "@patientName",
      "label": "姓名"
    },
    {
      "id": "col_age",
      "component": "WTableColumn",
      "prop": "@age",
      "label": "年龄"
    },
    {
      "id": "col_action",
      "component": "WTableColumn",
      "label": "操作",
      "children": ["btn_edit"]
    },
    {
      "id": "btn_edit",
      "component": "WButton",
      "type": "text",
      "action": {
        "event": {
          "name": "editPatient",
          "desc": "编辑患者",
          "api": {
            "url": "/api/patient/detail",
            "constParams": [
              { "name": "patientId", "value": { "path": "/selectedPatientId" } }
            ]
          }
        }
      },
      "child": "btn_edit_text"
    },
    {
      "id": "btn_edit_text",
      "component": "Text",
      "text": "编辑"
    }
  ],
  "data": {
    "searchKeyword": "",
    "dialogVisible": false
  },
  "loadEventHandler": {
    "url": "/api/patient/list",
    "desc": "加载患者列表"
  }
}

下一步 ​