feat: 增加AddFunc自动填充方法逻辑功能

This commit is contained in:
pixelMax(奇淼
2024-10-24 18:01:40 +08:00
parent 7d62a55a92
commit f191601ab4
6 changed files with 145 additions and 6 deletions

View File

@@ -98,11 +98,20 @@ func (a *AutoCodeTemplateApi) AddFunc(c *gin.Context) {
response.FailWithMessage(err.Error(), c)
return
}
err = autoCodeTemplateService.AddFunc(info)
var tempMap map[string]string
if info.IsPreview {
tempMap, err = autoCodeTemplateService.GetApiAndServer(info)
} else {
err = autoCodeTemplateService.AddFunc(info)
}
if err != nil {
global.GVA_LOG.Error("注入失败!", zap.Error(err))
response.FailWithMessage("注入失败", c)
} else {
if info.IsPreview {
response.OkWithDetailed(tempMap, "注入成功", c)
return
}
response.OkWithMessage("注入成功", c)
}
}

View File

@@ -252,6 +252,11 @@ type AutoFunc struct {
Method string `json:"method"` // 方法
IsPlugin bool `json:"isPlugin"` // 是否插件
IsAuth bool `json:"isAuth"` // 是否鉴权
IsPreview bool `json:"isPreview"` // 是否预览
IsAi bool `json:"isAi"` // 是否AI
ApiFunc string `json:"apiFunc"` // API方法
ServeFunc string `json:"serveFunc"` // 服务方法
JsFunc string `json:"jsFunc"` // JS方法
}
type InitMenu struct {

View File

@@ -290,6 +290,32 @@ func (s *autoCodeTemplate) AddFunc(info request.AutoFunc) error {
return nil
}
func (s *autoCodeTemplate) GetApiAndServer(info request.AutoFunc) (map[string]string, error) {
autoPkg := model.SysAutoCodePackage{}
err := global.GVA_DB.First(&autoPkg, "package_name = ?", info.Package).Error
if err != nil {
return nil, err
}
if autoPkg.Template != "package" {
info.IsPlugin = true
}
apiStr, err := s.getTemplateStr("api.go", info)
if err != nil {
return nil, err
}
serverStr, err := s.getTemplateStr("server.go", info)
if err != nil {
return nil, err
}
jsStr, err := s.getTemplateStr("api.js", info)
if err != nil {
return nil, err
}
return map[string]string{"api": apiStr, "server": serverStr, "js": jsStr}, nil
}
func (s *autoCodeTemplate) getTemplateStr(t string, info request.AutoFunc) (string, error) {
tempPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "function", t+".tpl")
files, err := template.ParseFiles(tempPath)