From 9d9036060187fedec8b92e10fb6cde561c63e4b3 Mon Sep 17 00:00:00 2001 From: MagicalKudzu Date: Sun, 15 Mar 2026 16:37:58 +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 --- .../controller/system/SysUserController.java | 2 +- .../generator/controller/GenController.java | 9 +- .../cool/kudzu/generator/domain/GenTable.java | 2 +- .../service/GenTableServiceImpl.java | 3 +- .../generator/service/IGenTableService.java | 3 +- .../kudzu/generator/util/VelocityUtils.java | 24 +- generator/src/main/resources/vm/ts/api.ts.vm | 50 ++ .../src/main/resources/vm/ts/index.ts.vm | 9 + generator/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 ++++++++++++++++++ 11 files changed, 1213 insertions(+), 10 deletions(-) create mode 100644 generator/src/main/resources/vm/ts/api.ts.vm create mode 100644 generator/src/main/resources/vm/ts/index.ts.vm create mode 100644 generator/src/main/resources/vm/ts/type.ts.vm create mode 100644 generator/src/main/resources/vm/vue/v3ts/index-tree.vue.vm create mode 100644 generator/src/main/resources/vm/vue/v3ts/index.vue.vm diff --git a/admin/src/main/java/cool/kudzu/web/controller/system/SysUserController.java b/admin/src/main/java/cool/kudzu/web/controller/system/SysUserController.java index ec5f386..dcee895 100644 --- a/admin/src/main/java/cool/kudzu/web/controller/system/SysUserController.java +++ b/admin/src/main/java/cool/kudzu/web/controller/system/SysUserController.java @@ -36,7 +36,7 @@ import java.util.stream.Collectors; /** * 用户信息 * - * @author ruoyi + * @author kudzu */ @RestController @RequestMapping("/system/user") diff --git a/generator/src/main/java/cool/kudzu/generator/controller/GenController.java b/generator/src/main/java/cool/kudzu/generator/controller/GenController.java index 0e626e4..54bc6c4 100644 --- a/generator/src/main/java/cool/kudzu/generator/controller/GenController.java +++ b/generator/src/main/java/cool/kudzu/generator/controller/GenController.java @@ -29,6 +29,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 java.io.IOException; @@ -108,11 +109,11 @@ public class GenController extends BaseController { @PreAuthorize("@ss.hasPermi('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, SecurityUtils.getUsername()); + genTableService.importGenTable(tableList, tplWebType, SecurityUtils.getUsername()); return success(); } @@ -122,7 +123,7 @@ public class GenController extends BaseController { @PreAuthorize("@ss.hasRole('admin')") @Log(title = "创建表", businessType = BusinessType.OTHER) @PostMapping("/createTable") - public AjaxResult createTableSave(String sql) { + public AjaxResult createTableSave(@RequestParam("sql") String sql, @RequestParam("tplWebType") String tplWebType) { try { SqlUtil.filterKeyword(sql); List sqlStatements = SQLUtils.parseStatements(sql, DbType.mysql); @@ -137,7 +138,7 @@ public class GenController extends BaseController { } List tableList = genTableService.selectDbTableListByNames(tableNames.toArray(new String[tableNames.size()])); String operName = SecurityUtils.getUsername(); - genTableService.importGenTable(tableList, operName); + genTableService.importGenTable(tableList, tplWebType, operName); return AjaxResult.success(); } catch (Exception e) { logger.error(e.getMessage(), e); diff --git a/generator/src/main/java/cool/kudzu/generator/domain/GenTable.java b/generator/src/main/java/cool/kudzu/generator/domain/GenTable.java index 715e7c3..929ac7e 100644 --- a/generator/src/main/java/cool/kudzu/generator/domain/GenTable.java +++ b/generator/src/main/java/cool/kudzu/generator/domain/GenTable.java @@ -56,7 +56,7 @@ public class GenTable extends BaseEntity { private String tplCategory; /** - * 前端类型(element-ui 模版 element-plus 模版) + * 前端类型(element-ui 模版 element-plus 模版 element-plus-typescript模版) */ private String tplWebType; diff --git a/generator/src/main/java/cool/kudzu/generator/service/GenTableServiceImpl.java b/generator/src/main/java/cool/kudzu/generator/service/GenTableServiceImpl.java index 456b0dd..246eae4 100644 --- a/generator/src/main/java/cool/kudzu/generator/service/GenTableServiceImpl.java +++ b/generator/src/main/java/cool/kudzu/generator/service/GenTableServiceImpl.java @@ -173,10 +173,11 @@ public class GenTableServiceImpl implements IGenTableService { */ @Override @Transactional - public void importGenTable(List tableList, String operName) { + public void importGenTable(List tableList, String tplWebType, String operName) { try { 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/generator/src/main/java/cool/kudzu/generator/service/IGenTableService.java b/generator/src/main/java/cool/kudzu/generator/service/IGenTableService.java index 6334aaf..3f1c69a 100644 --- a/generator/src/main/java/cool/kudzu/generator/service/IGenTableService.java +++ b/generator/src/main/java/cool/kudzu/generator/service/IGenTableService.java @@ -78,9 +78,10 @@ public interface IGenTableService { * 导入表结构 * * @param tableList 导入表列表 + * @param tplWebType 前端类型 * @param operName 操作人员 */ - void importGenTable(List tableList, String operName); + void importGenTable(List tableList, String tplWebType, String operName); /** * 预览代码 diff --git a/generator/src/main/java/cool/kudzu/generator/util/VelocityUtils.java b/generator/src/main/java/cool/kudzu/generator/util/VelocityUtils.java index c73b258..27b51e0 100644 --- a/generator/src/main/java/cool/kudzu/generator/util/VelocityUtils.java +++ b/generator/src/main/java/cool/kudzu/generator/util/VelocityUtils.java @@ -35,6 +35,17 @@ 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"; + + /** * 设置模板变量信息 * @@ -128,8 +139,12 @@ 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"); @@ -139,7 +154,6 @@ 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"); if (GenConstants.TPL_CRUD.equals(tplCategory)) { templates.add(useWebType + "/index.vue.vm"); } else if (GenConstants.TPL_TREE.equals(tplCategory)) { @@ -189,6 +203,12 @@ public class VelocityUtils { fileName = businessName + "Menu.sql"; } else if (template.contains("api.js.vm")) { 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); } else if (template.contains("index-tree.vue.vm")) { diff --git a/generator/src/main/resources/vm/ts/api.ts.vm b/generator/src/main/resources/vm/ts/api.ts.vm new file mode 100644 index 0000000..da4a8dc --- /dev/null +++ b/generator/src/main/resources/vm/ts/api.ts.vm @@ -0,0 +1,50 @@ +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/generator/src/main/resources/vm/ts/index.ts.vm b/generator/src/main/resources/vm/ts/index.ts.vm new file mode 100644 index 0000000..e51ef15 --- /dev/null +++ b/generator/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/generator/src/main/resources/vm/ts/type.ts.vm b/generator/src/main/resources/vm/ts/type.ts.vm new file mode 100644 index 0000000..45b0017 --- /dev/null +++ b/generator/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 \ No newline at end of file diff --git a/generator/src/main/resources/vm/vue/v3ts/index-tree.vue.vm b/generator/src/main/resources/vm/vue/v3ts/index-tree.vue.vm new file mode 100644 index 0000000..8f7af57 --- /dev/null +++ b/generator/src/main/resources/vm/vue/v3ts/index-tree.vue.vm @@ -0,0 +1,476 @@ + + + \ No newline at end of file diff --git a/generator/src/main/resources/vm/vue/v3ts/index.vue.vm b/generator/src/main/resources/vm/vue/v3ts/index.vue.vm new file mode 100644 index 0000000..ad23b18 --- /dev/null +++ b/generator/src/main/resources/vm/vue/v3ts/index.vue.vm @@ -0,0 +1,594 @@ + + + \ No newline at end of file