低代码平台设计与可视化开发实践
低代码平台概述
低代码平台(Low-Code Platform)通过可视化配置的方式,让非技术人员也能快速构建业务应用。本文介绍企业信息化系统中低代码平台的设计方案,涵盖表单配置、工作流引擎、报表设计器等核心组件的实现。
平台架构设计
| 层次 | 功能模块 | 技术选型 |
|---|---|---|
| 表现层 | 可视化设计器、表单渲染 | Vue3 / React |
| 业务层 | 表单引擎、工作流引擎 | BPMN.js |
| 数据层 | 元数据管理、数据存储 | PostgreSQL |
表单配置引擎
通过 JSON Schema 定义表单结构:
// 表单Schema定义
const formSchema = {
type: 'object',
properties: {
// 文本输入
customerName: {
type: 'string',
title: '客户名称',
component: 'Input',
required: true,
props: {
maxLength: 100,
placeholder: '请输入客户名称'
},
rules: [
{ required: true, message: '请输入客户名称' }
]
},
// 数字输入
orderAmount: {
type: 'number',
title: '订单金额',
component: 'NumberInput',
required: true,
props: {
min: 0,
precision: 2,
prefix: '¥'
}
},
// 下拉选择
paymentType: {
type: 'string',
title: '付款方式',
component: 'Select',
props: {
options: [
{ label: '现金', value: 'cash' },
{ label: '银行转账', value: 'transfer' },
{ label: '微信支付', value: 'wechat' },
{ label: '支付宝', value: 'alipay' }
]
}
},
// 日期选择
deliveryDate: {
type: 'string',
title: '交货日期',
component: 'DatePicker',
props: {
type: 'date',
format: 'YYYY-MM-DD'
}
},
// 关联选择(从表)
relatedProducts: {
type: 'array',
title: '关联产品',
component: 'TableSelect',
props: {
columns: [
{ key: 'name', label: '产品名称' },
{ key: 'spec', label: '规格' },
{ key: 'quantity', label: '数量' }
],
api: '/api/products'
}
},
// 富文本
description: {
type: 'string',
title: '备注说明',
component: 'RichText',
props: {
height: 200
}
}
}
};
// 表单渲染器
class FormRenderer {
constructor(schema) {
this.schema = schema;
this.components = this.loadComponents();
}
loadComponents() {
return {
Input: () => import('./components/Input.vue'),
NumberInput: () => import('./components/NumberInput.vue'),
Select: () => import('./components/Select.vue'),
DatePicker: () => import('./components/DatePicker.vue'),
TableSelect: () => import('./components/TableSelect.vue'),
RichText: () => import('./components/RichText.vue')
};
}
// 渲染表单
render() {
const fields = Object.entries(this.schema.properties).map(([key, config]) => {
const Component = this.components[config.component];
return {
key,
component: Component,
props: config.props,
rules: config.rules
};
});
return this.createForm(fields);
}
// 获取表单项
getFieldConfig(fieldName) {
return this.schema.properties[fieldName];
}
// 验证数据
validate(values) {
const errors = {};
const properties = this.schema.properties;
for (const [key, config] of Object.entries(properties)) {
// 必填验证
if (config.required && !values[key]) {
errors[key] = '此字段为必填项';
continue;
}
// 自定义验证规则
if (config.rules) {
for (const rule of config.rules) {
if (rule.validator) {
const result = rule.validator(values[key], values);
if (result !== true) {
errors[key] = result;
}
}
}
}
}
return {
valid: Object.keys(errors).length === 0,
errors
};
}
}
工作流引擎设计
使用 BPMN.js 实现可视化流程设计:
// 工作流节点配置
const workflowNodes = [
{
id: 'start',
type: 'startEvent',
name: '流程开始',
config: { formId: 'purchase_request' }
},
{
id: 'department_approval',
type: 'userTask',
name: '部门审批',
assignee: '${ initiator }',
candidateUsers: ['manager'],
config: {
formId: 'approval_form',
allowDelegate: true,
timeout: 172800 // 48小时
}
},
{
id: 'finance_approval',
type: 'userTask',
name: '财务审批',
assignee: 'finance_team',
config: {
formId: 'finance_approval_form',
condition: '${ orderAmount > 10000 }'
}
},
{
id: 'purchase_execute',
type: 'serviceTask',
name: '执行采购',
config: {
type: 'http',
url: '/api/purchase/execute',
method: 'POST'
}
},
{
id: 'end',
type: 'endEvent',
name: '流程结束'
}
];
// 工作流网关配置
const gateways = [
{
id: 'amount_check',
type: 'exclusiveGateway',
name: '金额判断',
conditions: [
{ expression: '${ orderAmount <= 10000 }', target: 'purchase_execute' },
{ expression: '${ orderAmount > 10000 }', target: 'finance_approval' }
]
}
];
// 流程实例
class WorkflowInstance {
constructor(processDefId) {
this.id = this.generateId();
this.processDefId = processDefId;
this.status = 'running';
this.currentNode = 'start';
this.variables = {};
this.tasks = [];
this.history = [];
}
// 启动流程
async start(initiator, variables) {
this.initiator = initiator;
this.startTime = new Date();
this.variables = { ...variables, initiator };
// 执行开始节点
await this.executeNode('start');
// 流转到下一个节点
await this.transitionTo('department_approval');
return this;
}
// 执行节点
async executeNode(nodeId) {
const node = workflowNodes.find(n => n.id === nodeId);
this.history.push({
nodeId,
nodeName: node.name,
executedAt: new Date()
});
if (node.type === 'serviceTask') {
// 执行服务任务
await this.executeServiceTask(node.config);
}
}
// 任务处理
async completeTask(taskId, variables = {}) {
const task = this.tasks.find(t => t.id === taskId);
if (!task) throw new Error('Task not found');
// 更新流程变量
Object.assign(this.variables, variables);
// 执行任务
await this.executeNode(task.nodeId);
// 查找下一节点
const nextNodeId = this.findNextNode(task.nodeId);
if (nextNodeId) {
await this.transitionTo(nextNodeId);
} else {
this.complete();
}
}
// 流程流转
async transitionTo(targetNodeId) {
const node = workflowNodes.find(n => n.id === targetNodeId);
if (node.type === 'userTask') {
// 创建任务
const task = {
id: this.generateId(),
nodeId: targetNodeId,
name: node.name,
assignee: this.resolveExpression(node.assignee),
status: 'pending',
createdAt: new Date()
};
this.tasks.push(task);
this.currentNode = targetNodeId;
} else if (node.type === 'serviceTask') {
await this.executeNode(targetNodeId);
const nextNodeId = this.findNextNode(targetNodeId);
if (nextNodeId) {
await this.transitionTo(nextNodeId);
}
} else if (node.type === 'endEvent') {
this.complete();
}
}
// 流程完成
complete() {
this.status = 'completed';
this.endTime = new Date();
}
}
可视化报表设计器
拖拽式报表设计器实现:
// 报表组件配置
const reportComponents = [
{
type: 'table',
icon: 'table',
label: '数据表格',
defaultProps: {
stripe: true,
border: true,
showSequence: true,
pagination: true,
pageSize: 10
},
configPanel: [
{ prop: 'dataSource', label: '数据源', type: 'select', options: [] },
{ prop: 'columns', label: '列配置', type: 'columnEditor' },
{ prop: 'showSummary', label: '显示合计', type: 'switch' }
]
},
{
type: 'chart',
icon: 'bar-chart',
label: '图表',
subTypes: ['bar', 'line', 'pie', 'radar'],
configPanel: [
{ prop: 'chartType', label: '图表类型', type: 'select' },
{ prop: 'dataSource', label: '数据源', type: 'select' },
{ prop: 'xAxis', label: 'X轴字段', type: 'fieldSelect' },
{ prop: 'yAxis', label: 'Y轴字段', type: 'fieldSelect' }
]
},
{
type: 'text',
icon: 'font-size',
label: '文本',
defaultProps: {
content: '文本内容',
fontSize: 14,
color: '#333333'
}
},
{
type: 'image',
icon: 'picture',
label: '图片',
defaultProps: {
src: '',
fit: 'contain'
}
},
{
type: 'parameter',
icon: 'filter',
label: '查询参数',
defaultProps: {
fields: []
}
}
];
// 报表渲染
class ReportRenderer {
constructor(config) {
this.components = config.components || [];
this.dataSources = config.dataSources || {};
}
// 渲染报表
async render(container) {
for (const component of this.components) {
const element = await this.createComponent(component);
container.appendChild(element);
}
}
// 创建组件
async createComponent(config) {
switch (config.type) {
case 'table':
return this.renderTable(config);
case 'chart':
return this.renderChart(config);
case 'text':
return this.renderText(config);
default:
return document.createElement('div');
}
}
// 渲染表格
async renderTable(config) {
// 获取数据
const data = await this.fetchData(config.dataSource);
// 构建表格
const table = document.createElement('table');
table.className = 'report-table';
// 表头
const thead = document.createElement('thead');
const headerRow = document.createElement('tr');
config.columns.forEach(col => {
const th = document.createElement('th');
th.textContent = col.label;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
// 表体
const tbody = document.createElement('tbody');
data.forEach(row => {
const tr = document.createElement('tr');
config.columns.forEach(col => {
const td = document.createElement('td');
td.textContent = this.getValue(row, col.key);
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
return table;
}
}
总结
低代码平台是企业信息化的重要支撑工具,核心价值包括:
- 快速开发:可视化配置大幅降低开发成本
- 灵活应变:业务变化时可快速调整
- 降本增效:减少对专业开发人员的依赖
- 统一规范:标准化组件保证应用质量
通过模块化设计,可以构建功能完善、扩展性强的低代码平台。