Skip to content

Commit

Permalink
feat: 调整自动化代码逻辑,重复创建的菜单和api需要用户手动取消自动创建 (#1798)
Browse files Browse the repository at this point in the history
* feat: 调整自动化代码逻辑,重复创建的菜单和api需要用户手动取消自动创建

---------

Co-authored-by: wall-js <[email protected]>
  • Loading branch information
wall-js and wall-js authored Jun 27, 2024
1 parent 640f44f commit 45f723c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
9 changes: 5 additions & 4 deletions server/api/v1/system/sys_auto_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,20 @@ func (autoApi *AutoCodeApi) CreateTemp(c *gin.Context) {
var menuId uint
if a.AutoCreateApiToSql {
if ids, err := autoCodeService.AutoCreateApi(&a); err != nil {
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
global.GVA_LOG.Error("自动化创建API失败!", zap.Error(err))
c.Writer.Header().Add("success", "false")
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据或取消自动创建API!"))
return
} else {
apiIds = ids
}
}
if a.AutoCreateMenuToSql {
if id, err := autoCodeService.AutoCreateMenu(&a); err != nil {
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
global.GVA_LOG.Error("自动化创建菜单失败!", zap.Error(err))
c.Writer.Header().Add("success", "false")
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据或取消自动创建菜单!"))
return
} else {
menuId = id
}
Expand Down
9 changes: 7 additions & 2 deletions server/service/system/sys_auto_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/mholt/archiver/v4"
"io"
"mime/multipart"
"os"
Expand All @@ -14,6 +13,8 @@ import (
"strings"
"text/template"

"github.com/mholt/archiver/v4"

ast2 "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"

"github.com/flipped-aurora/gin-vue-admin/server/resource/autocode_template/subcontract"
Expand Down Expand Up @@ -468,7 +469,11 @@ func (autoCodeService *AutoCodeService) AutoCreateApi(a *system.AutoCodeStruct)
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
for _, v := range apiList {
var api system.SysApi
if errors.Is(tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error, gorm.ErrRecordNotFound) {
findErr := tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error
if findErr == nil {
return errors.New("存在相同的API,请关闭自动创建API功能")
}
if errors.Is(findErr, gorm.ErrRecordNotFound) {
if err = tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务
return err
} else {
Expand Down

0 comments on commit 45f723c

Please sign in to comment.