package com.hzabjt.common; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; /** * @author wlr * @date 2023/5/31 16:36 */ public class FormilyGenerator { /** * 生成 Formily 表单的方法 * * @param interfaceDef 接口说明,格式为 {"属性": "标题", ...} * @return 对应的 Formily 表单 JSON 字符串 */ public static String generateFormilyForm(String interfaceDef) { // 解析接口说明为 JSON 对象 JSONObject def = JSONObject.parseObject(interfaceDef); // 构造表单布局 JSONObject form = new JSONObject(); form.put("labelCol", 6); form.put("wrapperCol", 12); // 构造表单数据结构 JSONObject schema = new JSONObject(); schema.put("type", "object"); JSONObject properties = new JSONObject(); int index = 0; // 遍历接口说明中的每个属性,生成 Formily 对应的结构 for (String key : def.keySet()) { JSONObject prop = new JSONObject(); prop.put("type", "string"); prop.put("title", def.getString(key)); prop.put("x-decorator", "FormItem"); prop.put("x-component", "Input"); prop.put("x-validator", new JSONArray()); prop.put("x-component-props", new JSONObject()); prop.put("x-decorator-props", new JSONObject()); prop.put("name", key); prop.put("x-designable-id", key + index); prop.put("x-index", index); properties.put(key,prop); index++; } schema.put("properties", properties); schema.put("x-designable-id", "bi41f6oenwv"); // 构造最终的表单对象 JSONObject formilyForm = new JSONObject(); formilyForm.put("form", form); formilyForm.put("schema", schema); // 返回表单 JSON 字符串 return formilyForm.toJSONString(); } public static void main(String[] args) { String interfaceDef = "{'name': '姓名', 'phone': '电话','address': '地址','age': '年龄'}"; String formilyForm = generateFormilyForm(interfaceDef); System.out.println(formilyForm); } }
基于描述,java生成formily表单
最新推荐文章于 2025-03-09 10:03:16 发布