From 86cb7555ebfd6c3e03aeeaf310df7523abcff8ce Mon Sep 17 00:00:00 2001
From: RuoYi <yzz_ivy@163.com>
Date: Thu, 02 Apr 2026 11:08:38 +0800
Subject: [PATCH] 代码生成支持表单布局选项

---
 ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/util/VelocityUtils.java |  119 +++++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 107 insertions(+), 12 deletions(-)

diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/util/VelocityUtils.java b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/util/VelocityUtils.java
index 0267345..132af1c 100644
--- a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/util/VelocityUtils.java
+++ b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/util/VelocityUtils.java
@@ -3,8 +3,10 @@
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import org.apache.velocity.VelocityContext;
-import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
 import com.ruoyi.common.core.constant.GenConstants;
 import com.ruoyi.common.core.utils.DateUtils;
 import com.ruoyi.common.core.utils.StringUtils;
@@ -26,6 +28,12 @@
 
     /** 默认上级菜单,系统工具 */
     private static final String DEFAULT_PARENT_MENU_ID = "3";
+
+    /** Vue3 Element Plus 模版 */
+    private static final String ELEMENT_PLUS = "element-plus";
+
+    /** Vue3 Element Plus TypeScript 模版 */
+    private static final String ELEMENT_PLUS_TYPESSRIPT = "element-plus-typescript";
 
     /**
      * 设置模板变量信息
@@ -52,12 +60,14 @@
         velocityContext.put("basePackage", getPackagePrefix(packageName));
         velocityContext.put("packageName", packageName);
         velocityContext.put("author", genTable.getFunctionAuthor());
+        velocityContext.put("colSpan", getColSpan(genTable.getFormColNum()));
         velocityContext.put("datetime", DateUtils.getDate());
         velocityContext.put("pkColumn", genTable.getPkColumn());
         velocityContext.put("importList", getImportList(genTable));
         velocityContext.put("permissionPrefix", getPermissionPrefix(moduleName, businessName));
         velocityContext.put("columns", genTable.getColumns());
         velocityContext.put("table", genTable);
+        velocityContext.put("dicts", getDicts(genTable));
         setMenuVelocityContext(velocityContext, genTable);
         if (GenConstants.TPL_TREE.equals(tplCategory))
         {
@@ -73,7 +83,7 @@
     public static void setMenuVelocityContext(VelocityContext context, GenTable genTable)
     {
         String options = genTable.getOptions();
-        JSONObject paramsObj = JSONObject.parseObject(options);
+        JSONObject paramsObj = JSON.parseObject(options);
         String parentMenuId = getParentMenuId(paramsObj);
         context.put("parentMenuId", parentMenuId);
     }
@@ -81,7 +91,7 @@
     public static void setTreeVelocityContext(VelocityContext context, GenTable genTable)
     {
         String options = genTable.getOptions();
-        JSONObject paramsObj = JSONObject.parseObject(options);
+        JSONObject paramsObj = JSON.parseObject(options);
         String treeCode = getTreecode(paramsObj);
         String treeParentCode = getTreeParentCode(paramsObj);
         String treeName = getTreeName(paramsObj);
@@ -120,11 +130,23 @@
 
     /**
      * 获取模板信息
-     *
+     * @param tplCategory 生成的模板
+     * @param tplWebType 前端类型
      * @return 模板列表
      */
-    public static List<String> getTemplateList(String tplCategory)
+    public static List<String> getTemplateList(String tplCategory, String tplWebType)
     {
+        String useWebType = "vm/vue";
+        String apiTemplate = "vm/js/api.js.vm";
+        if (StringUtils.equals(ELEMENT_PLUS, tplWebType))
+        {
+            useWebType = "vm/vue/v3";
+        }
+        else if (StringUtils.equals(ELEMENT_PLUS_TYPESSRIPT, tplWebType))
+        {
+            useWebType = "vm/vue/v3ts";
+            apiTemplate = "vm/ts/api.ts.vm";
+        }
         List<String> templates = new ArrayList<String>();
         templates.add("vm/java/domain.java.vm");
         templates.add("vm/java/mapper.java.vm");
@@ -133,18 +155,23 @@
         templates.add("vm/java/controller.java.vm");
         templates.add("vm/xml/mapper.xml.vm");
         templates.add("vm/sql/sql.vm");
-        templates.add("vm/js/api.js.vm");
+        templates.add(apiTemplate);
+        if (StringUtils.equals(ELEMENT_PLUS_TYPESSRIPT, tplWebType))
+        {
+            templates.add("vm/ts/type.ts.vm");
+            templates.add("vm/ts/index.ts.vm");
+        }
         if (GenConstants.TPL_CRUD.equals(tplCategory))
         {
-            templates.add("vm/vue/index.vue.vm");
+            templates.add(useWebType + "/index.vue.vm");
         }
         else if (GenConstants.TPL_TREE.equals(tplCategory))
         {
-            templates.add("vm/vue/index-tree.vue.vm");
+            templates.add(useWebType + "/index-tree.vue.vm");
         }
         else if (GenConstants.TPL_SUB.equals(tplCategory))
         {
-            templates.add("vm/vue/index.vue.vm");
+            templates.add(useWebType + "/index.vue.vm");
             templates.add("vm/java/sub-domain.java.vm");
         }
         return templates;
@@ -206,6 +233,18 @@
         {
             fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
         }
+        else if (template.contains("api.ts.vm"))
+        {
+            fileName = StringUtils.format("{}/api/{}/{}.ts", vuePath, moduleName, businessName);
+        }
+        else if (template.contains("type.ts.vm"))
+        {
+            fileName = StringUtils.format("{}/types/api/{}/{}.ts", vuePath, moduleName, businessName);
+        }
+        else if (template.contains("index.ts.vm"))
+        {
+            fileName = StringUtils.format("{}/types/api/index-bak.ts", vuePath);
+        }
         else if (template.contains("index.vue.vm"))
         {
             fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
@@ -226,8 +265,7 @@
     public static String getPackagePrefix(String packageName)
     {
         int lastIndex = packageName.lastIndexOf(".");
-        String basePackage = StringUtils.substring(packageName, 0, lastIndex);
-        return basePackage;
+        return StringUtils.substring(packageName, 0, lastIndex);
     }
 
     /**
@@ -258,6 +296,44 @@
             }
         }
         return importList;
+    }
+
+    /**
+     * 根据列类型获取字典组
+     * 
+     * @param genTable 业务表对象
+     * @return 返回字典组
+     */
+    public static String getDicts(GenTable genTable)
+    {
+        List<GenTableColumn> columns = genTable.getColumns();
+        Set<String> dicts = new HashSet<String>();
+        addDicts(dicts, columns);
+        if (StringUtils.isNotNull(genTable.getSubTable()))
+        {
+            List<GenTableColumn> subColumns = genTable.getSubTable().getColumns();
+            addDicts(dicts, subColumns);
+        }
+        return StringUtils.join(dicts, ", ");
+    }
+
+    /**
+     * 添加字典列表
+     * 
+     * @param dicts 字典列表
+     * @param columns 列集合
+     */
+    public static void addDicts(Set<String> dicts, List<GenTableColumn> columns)
+    {
+        for (GenTableColumn column : columns)
+        {
+            if (!column.isSuperColumn() && StringUtils.isNotEmpty(column.getDictType()) && StringUtils.equalsAny(
+                    column.getHtmlType(),
+                    new String[] { GenConstants.HTML_SELECT, GenConstants.HTML_RADIO, GenConstants.HTML_CHECKBOX }))
+            {
+                dicts.add("'" + column.getDictType() + "'");
+            }
+        }
     }
 
     /**
@@ -342,7 +418,7 @@
     public static int getExpandColumn(GenTable genTable)
     {
         String options = genTable.getOptions();
-        JSONObject paramsObj = JSONObject.parseObject(options);
+        JSONObject paramsObj = JSON.parseObject(options);
         String treeName = paramsObj.getString(GenConstants.TREE_NAME);
         int num = 0;
         for (GenTableColumn column : genTable.getColumns())
@@ -359,4 +435,23 @@
         }
         return num;
     }
+
+    /**
+     * 获取表单 el-col span
+     * 
+     * @param formColNum 表单布局方式(1单列 2双列 3三列)
+     * @return span 数值字符串
+     */
+    public static String getColSpan(int formColNum)
+    {
+        if (formColNum == 2)
+        {
+            return "12";
+        }
+        else if (formColNum == 3)
+        {
+            return "8";
+        }
+        return "24";
+    }
 }

--
Gitblit v1.9.3