diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/enums/codegen/CodegenFrontTypeEnum.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/enums/codegen/CodegenFrontTypeEnum.java new file mode 100644 index 000000000..5dfc61e26 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/enums/codegen/CodegenFrontTypeEnum.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.infra.enums.codegen; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +/** + * 代码生成的前端类型枚举 + * + * @author 芋道源码 + */ +@AllArgsConstructor +@Getter +public enum CodegenFrontTypeEnum { + + VUE2(10), // Vue2 Element UI 标准模版 + VUE3(20), // Vue3 Element Plus 标准模版 + VUE3_SCHEMA(21), // Vue3 Element Plus Schema 模版 + VUE3_VXE(22), // Vue3 VXE 模版 + ; + + /** + * 类型 + */ + private final Integer type; + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/codegen/config/CodegenProperties.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/codegen/config/CodegenProperties.java index a8649dc23..d9daabb7f 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/codegen/config/CodegenProperties.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/codegen/config/CodegenProperties.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.infra.framework.codegen.config; +import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.validation.annotation.Validated; @@ -25,4 +26,12 @@ public class CodegenProperties { @NotEmpty(message = "数据库不能为空") private Collection dbSchemas; + /** + * 代码生成的前端类型 + * + * 枚举 {@link CodegenFrontTypeEnum#getType()} + */ + @NotNull(message = "代码生成的前端类型不能为空") + private Integer frontType; + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java index 85f70f8c7..20fd7e67d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java @@ -23,9 +23,12 @@ import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum; import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenColumnDO; import cn.iocoder.yudao.module.infra.dal.dataobject.codegen.CodegenTableDO; +import cn.iocoder.yudao.module.infra.enums.codegen.CodegenFrontTypeEnum; import cn.iocoder.yudao.module.infra.enums.codegen.CodegenSceneEnum; import cn.iocoder.yudao.module.infra.framework.codegen.config.CodegenProperties; +import com.google.common.collect.ImmutableTable; import com.google.common.collect.Maps; +import com.google.common.collect.Table; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -50,11 +53,12 @@ import static cn.hutool.core.text.CharSequenceUtil.*; public class CodegenEngine { /** - * 模板配置 + * 后端的模板配置 + * * key:模板在 resources 的地址 * value:生成的路径 */ - private static final Map TEMPLATES = MapUtil.builder(new LinkedHashMap<>()) // 有序 + private static final Map SERVER_TEMPLATES = MapUtil.builder(new LinkedHashMap<>()) // 有序 // Java module-biz Main .put(javaTemplatePath("controller/vo/baseVO"), javaModuleImplVOFilePath("BaseVO")) .put(javaTemplatePath("controller/vo/createReqVO"), javaModuleImplVOFilePath("CreateReqVO")) @@ -80,23 +84,33 @@ public class CodegenEngine { javaModuleImplTestFilePath("service/${table.businessName}/${table.className}ServiceImplTest")) // Java module-api Main .put(javaTemplatePath("enums/errorcode"), javaModuleApiMainFilePath("enums/ErrorCodeConstants_手动操作")) - // Vue2 - .put(vueTemplatePath("views/index.vue"), - vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue")) - .put(vueTemplatePath("api/api.js"), - vueFilePath("api/${table.moduleName}/${classNameVar}.js")) - // Vue3 - .put(vue3TemplatePath("views/index.vue"), - vue3FilePath("views/${table.moduleName}/${classNameVar}/index.vue")) - .put(vue3TemplatePath("views/data.ts"), - vue3FilePath("views/${table.moduleName}/${classNameVar}/${classNameVar}.data.ts")) - .put(vue3TemplatePath("api/api.ts"), - vue3FilePath("api/${table.moduleName}/${classNameVar}/index.ts")) // SQL .put("codegen/sql/sql.vm", "sql/sql.sql") .put("codegen/sql/h2.vm", "sql/h2.sql") .build(); + /** + * 后端的配置模版 + * + * key1:UI 模版的类型 {@link CodegenFrontTypeEnum#getType()} + * key2:模板在 resources 的地址 + * value:生成的路径 + */ + private static final Table FRONT_TEMPLATES = ImmutableTable.builder() + // Vue2 + .put(CodegenFrontTypeEnum.VUE2.getType(), vueTemplatePath("views/index.vue"), + vueFilePath("views/${table.moduleName}/${classNameVar}/index.vue")) + .put(CodegenFrontTypeEnum.VUE2.getType(), vueTemplatePath("api/api.js"), + vueFilePath("api/${table.moduleName}/${classNameVar}.js")) + // Vue3 + .put(CodegenFrontTypeEnum.VUE3.getType(), vue3TemplatePath("views/index.vue"), + vue3FilePath("views/${table.moduleName}/${classNameVar}/index.vue")) + .put(CodegenFrontTypeEnum.VUE3.getType(), vue3TemplatePath("views/data.ts"), + vue3FilePath("views/${table.moduleName}/${classNameVar}/${classNameVar}.data.ts")) + .put(CodegenFrontTypeEnum.VUE3.getType(), vue3TemplatePath("api/api.ts"), + vue3FilePath("api/${table.moduleName}/${classNameVar}/index.ts")) + .build(); + @Resource private CodegenProperties codegenProperties; @@ -165,8 +179,9 @@ public class CodegenEngine { bindingMap.put("permissionPrefix", table.getModuleName() + ":" + simpleClassNameStrikeCase); // 执行生成 - final Map result = Maps.newLinkedHashMapWithExpectedSize(TEMPLATES.size()); // 有序 - TEMPLATES.forEach((vmPath, filePath) -> { + Map templates = getTemplates(); + Map result = Maps.newLinkedHashMapWithExpectedSize(templates.size()); // 有序 + templates.forEach((vmPath, filePath) -> { filePath = formatFilePath(filePath, bindingMap); String content = templateEngine.getTemplate(vmPath).render(bindingMap); result.put(filePath, content); @@ -174,6 +189,13 @@ public class CodegenEngine { return result; } + private Map getTemplates() { + Map templates = new LinkedHashMap<>(); + templates.putAll(SERVER_TEMPLATES); + templates.putAll(FRONT_TEMPLATES.row(codegenProperties.getFrontType())); + return templates; + } + private String formatFilePath(String filePath, Map bindingMap) { filePath = StrUtil.replace(filePath, "${basePackage}", getStr(bindingMap, "basePackage").replaceAll("\\.", "/")); diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index 6b2a08a01..6dfe1d86c 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -125,6 +125,7 @@ yudao: codegen: base-package: ${yudao.info.base-package} db-schemas: ${spring.datasource.dynamic.datasource.master.name} + front-type: 20 error-code: # 错误码相关配置项 constants-class-list: - cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants