fix: 更新优化插件注册 api 和注册 menus 逻辑。
This commit is contained in:
parent
aa84605c4d
commit
f357c676bb
|
|
@ -1,50 +1,53 @@
|
|||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||
)
|
||||
|
||||
func RegisterApis(apis ...system.SysApi) {
|
||||
var count int64
|
||||
var apiPaths []string
|
||||
for i := range apis {
|
||||
apiPaths = append(apiPaths, apis[i].Path)
|
||||
}
|
||||
global.GVA_DB.Find(&[]system.SysApi{}, "path in (?)", apiPaths).Count(&count)
|
||||
if count > 0 {
|
||||
return
|
||||
}
|
||||
err := global.GVA_DB.Create(&apis).Error
|
||||
func RegisterApis( apis ...system.SysApi) {
|
||||
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
||||
for _, api := range apis {
|
||||
err := tx.Model(system.SysApi{}).Where("path = ? AND method = ? AND api_group = ? ", api.Path, api.Method, api.ApiGroup).FirstOrCreate(&api).Error
|
||||
if err != nil {
|
||||
zap.L().Error("注册API失败", zap.Error(err), zap.String("api", api.Path), zap.String("method", api.Method), zap.String("apiGroup", api.ApiGroup))
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
zap.L().Error("注册API失败", zap.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
func RegisterMenus(menus ...system.SysBaseMenu) {
|
||||
var count int64
|
||||
var menuNames []string
|
||||
func RegisterMenus( menus ...system.SysBaseMenu) {
|
||||
parentMenu := menus[0]
|
||||
otherMenus := menus[1:]
|
||||
for i := range menus {
|
||||
menuNames = append(menuNames, menus[i].Name)
|
||||
}
|
||||
global.GVA_DB.Find(&[]system.SysBaseMenu{}, "name in (?)", menuNames).Count(&count)
|
||||
if count > 0 {
|
||||
return
|
||||
}
|
||||
err := global.GVA_DB.Create(&parentMenu).Error
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
for i := range otherMenus {
|
||||
err := global.GVA_DB.Transaction(func(tx *gorm.DB) error {
|
||||
err := tx.Model(system.SysBaseMenu{}).Where("name = ? ", parentMenu.Name).FirstOrCreate(&parentMenu).Error
|
||||
if err != nil {
|
||||
zap.L().Error("注册菜单失败", zap.Error(err))
|
||||
return errors.Wrap(err, "注册菜单失败")
|
||||
}
|
||||
pid := parentMenu.ID
|
||||
otherMenus[i].ParentId = pid
|
||||
}
|
||||
err = global.GVA_DB.Create(&otherMenus).Error
|
||||
for i := range otherMenus {
|
||||
otherMenus[i].ParentId = pid
|
||||
err = tx.Model(system.SysBaseMenu{}).Where("name = ? ", otherMenus[i].Name).FirstOrCreate(&otherMenus[i]).Error
|
||||
if err != nil {
|
||||
zap.L().Error("注册菜单失败", zap.Error(err))
|
||||
return errors.Wrap(err, "注册菜单失败")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
zap.L().Error("注册菜单失败", zap.Error(err))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue