代码生成器 对接'加载所有模板'接口

This commit is contained in:
cuijiawang 2025-09-25 09:29:07 +08:00
parent a99ba1dda7
commit dbf9cf1c3b
3 changed files with 16 additions and 58 deletions

View File

@ -27,8 +27,9 @@ export interface CodegenRequest {
// 模板信息
export interface TemplateInfo {
id: string;
name: string;
key: string;
description: string;
}
// 模板组
@ -45,9 +46,7 @@ export interface CodegenResponse {
// 获取所有模板
export function getAllTemplatesApi() {
return http.request<ResponseData<{ templates: TemplateGroup[] }>>("post", "/template/all", {
data: { id: 1234 }
});
return http.request<ResponseData<TemplateGroup[]>>("get", "/codegen/template/all");
}
// 生成代码

View File

@ -47,8 +47,8 @@ const isEmpty = computed(() => props.templates.length === 0);
<div class="template-buttons">
<el-button
v-for="template in group.templates"
:key="template.key"
:type="currentSelect === template.key ? 'primary' : 'default'"
:key="template.id"
:type="currentSelect === template.name ? 'primary' : 'default'"
size="default"
class="template-button"
@click="handleTemplateClick(template)"

View File

@ -1,11 +1,12 @@
import { ref, reactive, computed } from "vue";
import { ElMessage } from "element-plus";
import {
getAllTemplatesApi,
generateCodeApi,
CodegenOptions,
import {
getAllTemplatesApi,
generateCodeApi,
CodegenOptions,
TemplateGroup,
CodegenRequest
TemplateInfo,
CodegenRequest
} from "@/api/system/codegen";
export function useCodegen() {
@ -91,50 +92,8 @@ export function useCodegen() {
async function loadAllTemplates() {
try {
loading.value = true;
// 临时使用假数据
templates.value = [
{
group: "MyBatis-Plus",
templates: [
{ name: "Entity", key: "plusentity" },
{ name: "Controller", key: "pluscontroller" },
{ name: "Service", key: "plusservice" },
{ name: "Mapper", key: "plusmapper" }
]
},
{
group: "MyBatis",
templates: [
{ name: "Entity", key: "model" },
{ name: "Controller", key: "controller" },
{ name: "Service", key: "service" },
{ name: "ServiceImpl", key: "service_impl" },
{ name: "Mapper", key: "mapper" },
{ name: "Mapper.xml", key: "mybatis" }
]
},
{
group: "JPA",
templates: [
{ name: "Entity", key: "entity" },
{ name: "Controller", key: "jpacontroller" },
{ name: "Repository", key: "repository" }
]
},
{
group: "UI模板",
templates: [
{ name: "Element UI", key: "element-ui" },
{ name: "Bootstrap UI", key: "bootstrap-ui" },
{ name: "LayUI List", key: "layui-list" },
{ name: "LayUI Edit", key: "layui-edit" }
]
}
];
// 后续替换为真实API调用
// const { data } = await getAllTemplatesApi();
// templates.value = data.templates;
const { data } = await getAllTemplatesApi();
templates.value = data;
} catch (error) {
console.error("加载模板失败:", error);
ElMessage.error("加载模板失败");
@ -144,10 +103,10 @@ export function useCodegen() {
}
// 设置输出模板
function setOutputModel(template: { name: string; key: string }) {
currentSelect.value = template.key;
function setOutputModel(template: TemplateInfo) {
currentSelect.value = template.name;
if (outputStr.value.length > 30) {
outputStr.value = outputJson.value[template.key] || "";
outputStr.value = outputJson.value[template.name] || "";
}
}