Skip to content

Commit

Permalink
feat: autoFunc可以ai添加代码。
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelmaxQm committed Oct 24, 2024
1 parent f191601 commit bb8210b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion server/model/system/request/sys_auto_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type AutoFunc struct {
IsPreview bool `json:"isPreview"` // 是否预览
IsAi bool `json:"isAi"` // 是否AI
ApiFunc string `json:"apiFunc"` // API方法
ServeFunc string `json:"serveFunc"` // 服务方法
ServerFunc string `json:"serverFunc"` // 服务方法
JsFunc string `json:"jsFunc"` // JS方法
}

Expand Down
9 changes: 9 additions & 0 deletions server/service/system/auto_code_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,19 @@ func (s *autoCodeTemplate) addTemplateToFile(t string, info request.AutoFunc) er

switch t {
case "api.go":
if info.IsAi && info.ApiFunc != "" {
getTemplateStr = info.ApiFunc
}
target = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "api", "v1", info.Package, info.HumpPackageName+".go")
case "server.go":
if info.IsAi && info.ServerFunc != "" {
getTemplateStr = info.ServerFunc
}
target = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "service", info.Package, info.HumpPackageName+".go")
case "api.js":
if info.IsAi && info.JsFunc != "" {
getTemplateStr = info.JsFunc
}
target = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Web, "api", info.Package, info.PackageName+".js")
}
if info.IsPlugin {
Expand Down
51 changes: 46 additions & 5 deletions web/src/view/systemTools/autoCodeAdmin/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
v-model="funcFlag"
size="60%"
:show-close="false"
:close-on-click-modal="false"
>
<template #header>
<div class="flex justify-between items-center">
Expand All @@ -148,20 +149,22 @@
<el-button
type="primary"
@click="runFunc"
:loading="aiLoading"
>
生成
</el-button>
<el-button
type="primary"
@click="closeFunc"
:loading="aiLoading"
>
取消
</el-button>
</div>
</div>
</template>
<div class="">
<el-form label-position="top" :model="autoFunc" label-width="80px">
<el-form v-loading="aiLoading" label-position="top" :model="autoFunc" label-width="80px">
<el-form-item label="包名:">
<el-input v-model="autoFunc.package" placeholder="请输入包名" disabled />
</el-form-item>
Expand All @@ -184,7 +187,7 @@
<el-input v-model="autoFunc.funcDesc" placeholder="请输入方法介绍" />
</el-form-item>
<el-form-item label="方法名:">
<el-input v-model="autoFunc.funcName" placeholder="请输入方法名" />
<el-input @blur="autoFunc.funcName = toUpperCase(autoFunc.funcName)" v-model="autoFunc.funcName" placeholder="请输入方法名" />
</el-form-item>
<el-form-item label="方法:">
<el-select v-model="autoFunc.method" placeholder="请选择方法">
Expand Down Expand Up @@ -246,7 +249,7 @@ import { ElMessage, ElMessageBox } from "element-plus";
import { ref,onMounted } from "vue";
import { formatDate } from "@/utils/format";
import { toUpperCase } from "@/utils/stringFun"
import {useAppStore} from "@/pinia";
import {useAppStore} from "@/pinia";
import { Marked } from "marked";
import { markedHighlight } from "marked-highlight";
import hljs from 'highlight.js'
Expand All @@ -256,6 +259,8 @@ defineOptions({
name: "AutoCodeAdmin",
});
const aiLoading = ref(false)
const formData = ref({
id: undefined,
deleteApi: true,
Expand Down Expand Up @@ -305,7 +310,12 @@ const addFuncBtn = (row) => {
autoFunc.value.method = ""
autoFunc.value.funcName = ""
autoFunc.value.router = ""
autoFunc.value.funcDesc = "方法介绍"
autoFunc.value.funcDesc = ""
autoFunc.value.isAuth = false
autoFunc.value.isAi = false
autoFunc.value.apiFunc = ""
autoFunc.value.serverFunc = ""
autoFunc.value.jsFunc = ""
funcFlag.value = true;
};
Expand All @@ -319,6 +329,30 @@ const runFunc = async () =>{
// 首字母自动转换为大写
autoFunc.value.funcName = toUpperCase(autoFunc.value.funcName)
if (!autoFunc.value.funcName) {
ElMessage.error("请输入方法名")
return
}
if (!autoFunc.value.method) {
ElMessage.error("请选择方法")
return
}
if (!autoFunc.value.router) {
ElMessage.error("请输入路由")
return
}
if (!autoFunc.value.funcDesc) {
ElMessage.error("请输入方法介绍")
return
}
if (autoFunc.value.isAi){
if (!autoFunc.value.apiFunc || !autoFunc.value.serverFunc || !autoFunc.value.jsFunc) {
ElMessage.error("请先使用AI帮写完成基础代码,如果生成失败请重新调用")
return
}
}
const res = await addFunc(autoFunc.value)
if (res.code === 0) {
ElMessage.success("增加方法成功");
Expand Down Expand Up @@ -446,13 +480,19 @@ const goAutoCode = (row,isAdd) => {
const aiAddFunc = async () =>{
aiLoading.value = true
autoFunc.value.apiFunc = ""
autoFunc.value.serverFunc = ""
autoFunc.value.jsFunc = ""
if (!autoFunc.value.prompt) {
ElMessage.error("请输入提示信息")
return
}
const res = await addFunc({...autoFunc.value,isPreview:true})
if (res.code !== 0) {
aiLoading.value = false
ElMessage.error(res.msg)
return
}
Expand All @@ -463,6 +503,7 @@ const aiAddFunc = async () =>{
prompt: autoFunc.value.prompt,
command: "addFunc"
})
aiLoading.value = false
if (aiRes.code === 0) {
const aiData = JSON.parse(aiRes.data)
autoFunc.value.apiFunc = aiData.api
Expand Down

0 comments on commit bb8210b

Please sign in to comment.