From b0ff5a01a132d7badf935f60b6aa14fa00dce5b0 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Thu, 12 Mar 2026 14:13:11 +0800 Subject: [PATCH] =?UTF-8?q?TypeScript=E5=89=8D=E7=AB=AF=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=94=9F=E6=88=90=E6=A8=A1=E6=9D=BF=E5=90=8C=E6=AD=A5=E5=88=B0?= =?UTF-8?q?=E6=9C=80=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/gen/controller/GenController.java | 5 +- .../java/com/ruoyi/gen/domain/GenTable.java | 4 +- .../gen/service/GenTableServiceImpl.java | 3 +- .../ruoyi/gen/service/IGenTableService.java | 3 +- .../com/ruoyi/gen/util/VelocityUtils.java | 33 +- .../main/resources/vm/java/controller.java.vm | 1 - .../src/main/resources/vm/ts/api.ts.vm | 51 ++ .../src/main/resources/vm/ts/index.ts.vm | 9 + .../src/main/resources/vm/ts/type.ts.vm | 51 ++ .../resources/vm/vue/v3ts/index-tree.vue.vm | 476 ++++++++++++++ .../main/resources/vm/vue/v3ts/index.vue.vm | 594 ++++++++++++++++++ ruoyi-ui/src/views/tool/gen/genInfoForm.vue | 1 + ruoyi-ui/src/views/tool/gen/importTable.vue | 2 +- ruoyi-ui/src/views/tool/gen/index.vue | 1 + 14 files changed, 1225 insertions(+), 9 deletions(-) create mode 100644 ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/api.ts.vm create mode 100644 ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/index.ts.vm create mode 100644 ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/type.ts.vm create mode 100644 ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index-tree.vue.vm create mode 100644 ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index.vue.vm diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java index 13b5ad40e..0bad3c813 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java +++ b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/controller/GenController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.core.text.Convert; import com.ruoyi.common.core.web.controller.BaseController; @@ -104,12 +105,12 @@ public class GenController extends BaseController @RequiresPermissions("tool:gen:import") @Log(title = "代码生成", businessType = BusinessType.IMPORT) @PostMapping("/importTable") - public AjaxResult importTableSave(String tables) + public AjaxResult importTableSave(@RequestParam("tables") String tables, @RequestParam("tplWebType") String tplWebType) { String[] tableNames = Convert.toStrArray(tables); // 查询表信息 List tableList = genTableService.selectDbTableListByNames(tableNames); - genTableService.importGenTable(tableList); + genTableService.importGenTable(tableList, tplWebType); return success(); } diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/domain/GenTable.java b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/domain/GenTable.java index eadb1315e..5f42880b2 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/domain/GenTable.java +++ b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/domain/GenTable.java @@ -41,7 +41,7 @@ public class GenTable extends BaseEntity /** 使用的模板(crud单表操作 tree树表操作 sub主子表操作) */ private String tplCategory; - /** 前端类型(element-ui模版 element-plus模版) */ + /** 前端类型(element-ui模版 element-plus模版 element-plus-typescript模版) */ private String tplWebType; /** 生成包路径 */ @@ -267,6 +267,7 @@ public class GenTable extends BaseEntity { this.subTable = subTable; } + public List getColumns() { return columns; @@ -346,6 +347,7 @@ public class GenTable extends BaseEntity { return tplCategory != null && StringUtils.equals(GenConstants.TPL_SUB, tplCategory); } + public boolean isTree() { return isTree(this.tplCategory); diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java index 6031be171..f6be881d9 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java +++ b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/GenTableServiceImpl.java @@ -157,7 +157,7 @@ public class GenTableServiceImpl implements IGenTableService */ @Override @Transactional(rollbackFor = Exception.class) - public void importGenTable(List tableList) + public void importGenTable(List tableList, String tplWebType) { String operName = SecurityUtils.getUsername(); try @@ -165,6 +165,7 @@ public class GenTableServiceImpl implements IGenTableService for (GenTable table : tableList) { String tableName = table.getTableName(); + table.setTplWebType(tplWebType); GenUtils.initTable(table, operName); int row = genTableMapper.insertGenTable(table); if (row > 0) diff --git a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java index 3d6e0ff69..3237b11c9 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java +++ b/ruoyi-modules/ruoyi-gen/src/main/java/com/ruoyi/gen/service/IGenTableService.java @@ -70,8 +70,9 @@ public interface IGenTableService * 导入表结构 * * @param tableList 导入表列表 + * @param tplWebType 前端类型 */ - public void importGenTable(List tableList); + public void importGenTable(List tableList, String tplWebType); /** * 预览代码 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 2c8dcb1ca..574122757 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 @@ -29,6 +29,12 @@ public class VelocityUtils /** 默认上级菜单,系统工具 */ 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"; + /** * 设置模板变量信息 * @@ -130,10 +136,16 @@ public class VelocityUtils public static List getTemplateList(String tplCategory, String tplWebType) { String useWebType = "vm/vue"; - if ("element-plus".equals(tplWebType)) + 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 templates = new ArrayList(); templates.add("vm/java/domain.java.vm"); templates.add("vm/java/mapper.java.vm"); @@ -142,7 +154,12 @@ public class VelocityUtils 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(useWebType + "/index.vue.vm"); @@ -215,6 +232,18 @@ public class VelocityUtils { 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); diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm index b706290e5..db2ce452f 100644 --- a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/java/controller.java.vm @@ -1,7 +1,6 @@ package ${packageName}.controller; import java.util.List; -import java.io.IOException; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/api.ts.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/api.ts.vm new file mode 100644 index 000000000..8ed5122bb --- /dev/null +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/api.ts.vm @@ -0,0 +1,51 @@ +import request from '@/utils/request' +import type { AjaxResult, TableDataInfo, ${BusinessName}QueryParams, ${ClassName} } from '@/types' + +// 查询${functionName}列表 +#if($table.tree) +export function list${BusinessName}(query?: ${BusinessName}QueryParams): Promise> { +#else +export function list${BusinessName}(query: ${BusinessName}QueryParams): Promise> { +#end + return request({ + url: '/${moduleName}/${businessName}/list', + method: 'get', + params: query + }) +} + +// 查询${functionName}详细 +export function get${BusinessName}(${pkColumn.javaField}: number): Promise> { + return request({ + url: '/${moduleName}/${businessName}/' + ${pkColumn.javaField}, + method: 'get' + }) +} + +// 新增${functionName} +export function add${BusinessName}(data: ${ClassName}): Promise { + return request({ + url: '/${moduleName}/${businessName}', + method: 'post', + data: data + }) +} + +// 修改${functionName} +export function update${BusinessName}(data: ${ClassName}): Promise { + return request({ + url: '/${moduleName}/${businessName}', + method: 'put', + data: data + }) +} + +// 删除${functionName} +export function del${BusinessName}(${pkColumn.javaField}: number | number[]): Promise { + return request({ + url: '/${moduleName}/${businessName}/' + ${pkColumn.javaField}, + method: 'delete' + }) +} + + diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/index.ts.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/index.ts.vm new file mode 100644 index 000000000..e51ef151e --- /dev/null +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/index.ts.vm @@ -0,0 +1,9 @@ +/** + * API 类型统一导出 + */ +.... + +// 防止覆盖,需手动追加下面代码到index.ts文件中,追加好后此文件可删除 + +// ${moduleName} 模块 +export * from "./${moduleName}/${businessName}"; \ No newline at end of file diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/type.ts.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/type.ts.vm new file mode 100644 index 000000000..fff1d4cab --- /dev/null +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/ts/type.ts.vm @@ -0,0 +1,51 @@ +import type { PageDomain, BaseEntity } from "../common"; + +/** ${functionName}配置分页查询参数 */ +export interface ${BusinessName}QueryParams extends PageDomain { +#foreach($column in $columns) +#if($column.query) +#set($type = "string") +#if($column.javaType == "Long" || $column.javaType == "Integer") + #set($type = "number") +#elseif($column.javaType == "Boolean") + #set($type = "boolean") +#end + /** ${column.columnComment} */ + ${column.javaField}?: ${type}; +#end +#end +} + +/** ${functionName}配置信息 */ +export interface ${ClassName} extends BaseEntity { +#foreach($column in $columns) +#set($type = "string") +#if($column.javaType == "Long" || $column.javaType == "Integer") + #set($type = "number") +#elseif($column.javaType == "Boolean") + #set($type = "boolean") +#end + /** ${column.columnComment} */ + ${column.javaField}?: ${type}; +#end +#if($table.sub) + /** $table.subTable.functionName信息 */ + ${subclassName}List?: ${subClassName}[]; +#end +} +#if($table.sub) + +/** ${subTable.functionName}配置信息 */ +export interface ${subClassName} extends BaseEntity { +#foreach ($column in $subTable.columns) +#set($type = "string") +#if($column.javaType == "Long" || $column.javaType == "Integer") + #set($type = "number") +#elseif($column.javaType == "Boolean") + #set($type = "boolean") +#end + /** ${column.columnComment} */ + ${column.javaField}?: ${type}; +#end +} +#end diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index-tree.vue.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index-tree.vue.vm new file mode 100644 index 000000000..f1bc586f9 --- /dev/null +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index-tree.vue.vm @@ -0,0 +1,476 @@ + + + diff --git a/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index.vue.vm b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index.vue.vm new file mode 100644 index 000000000..d0fa278aa --- /dev/null +++ b/ruoyi-modules/ruoyi-gen/src/main/resources/vm/vue/v3ts/index.vue.vm @@ -0,0 +1,594 @@ + + + diff --git a/ruoyi-ui/src/views/tool/gen/genInfoForm.vue b/ruoyi-ui/src/views/tool/gen/genInfoForm.vue index dde314f6d..e90b41072 100644 --- a/ruoyi-ui/src/views/tool/gen/genInfoForm.vue +++ b/ruoyi-ui/src/views/tool/gen/genInfoForm.vue @@ -17,6 +17,7 @@ + diff --git a/ruoyi-ui/src/views/tool/gen/importTable.vue b/ruoyi-ui/src/views/tool/gen/importTable.vue index 2fd0a945e..89ff6eef5 100644 --- a/ruoyi-ui/src/views/tool/gen/importTable.vue +++ b/ruoyi-ui/src/views/tool/gen/importTable.vue @@ -107,7 +107,7 @@ export default { this.$modal.msgError("请选择要导入的表") return } - importTable({ tables: tableNames }).then(res => { + importTable({ tables: tableNames, tplWebType: 'element-ui' }).then(res => { this.$modal.msgSuccess(res.msg) if (res.code === 200) { this.visible = false diff --git a/ruoyi-ui/src/views/tool/gen/index.vue b/ruoyi-ui/src/views/tool/gen/index.vue index d4dfa9b4c..e8fe44752 100644 --- a/ruoyi-ui/src/views/tool/gen/index.vue +++ b/ruoyi-ui/src/views/tool/gen/index.vue @@ -167,6 +167,7 @@ hljs.registerLanguage("xml", require("highlight.js/lib/languages/xml")) hljs.registerLanguage("html", require("highlight.js/lib/languages/xml")) hljs.registerLanguage("vue", require("highlight.js/lib/languages/xml")) hljs.registerLanguage("javascript", require("highlight.js/lib/languages/javascript")) +hljs.registerLanguage("typescript", require("highlight.js/lib/languages/typescript")) hljs.registerLanguage("sql", require("highlight.js/lib/languages/sql")) export default {