189 lines
5.2 KiB
Go
189 lines
5.2 KiB
Go
package server
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type menuRequest struct {
|
|
ID uint `json:"ID"`
|
|
ParentID uint `json:"parentId"`
|
|
Path string `json:"path"`
|
|
Name string `json:"name"`
|
|
Hidden bool `json:"hidden"`
|
|
Component string `json:"component"`
|
|
Sort int `json:"sort"`
|
|
Meta struct {
|
|
ActiveName string `json:"activeName"`
|
|
KeepAlive bool `json:"keepAlive"`
|
|
DefaultMenu bool `json:"defaultMenu"`
|
|
Title string `json:"title"`
|
|
Icon string `json:"icon"`
|
|
CloseTab bool `json:"closeTab"`
|
|
TransitionType string `json:"transitionType"`
|
|
} `json:"meta"`
|
|
MenuButtons []struct {
|
|
ID uint `json:"ID"`
|
|
Name string `json:"name"`
|
|
Description string `json:"desc"`
|
|
} `json:"menuBtn"`
|
|
}
|
|
|
|
func menuDomain(v menuRequest) *biz.Menu {
|
|
return &biz.Menu{ID: v.ID, ParentID: v.ParentID, Path: v.Path, Name: v.Name, Hidden: v.Hidden, Component: v.Component, Sort: v.Sort, ActiveName: v.Meta.ActiveName, KeepAlive: v.Meta.KeepAlive, DefaultMenu: v.Meta.DefaultMenu, Title: v.Meta.Title, Icon: v.Meta.Icon, CloseTab: v.Meta.CloseTab, TransitionType: v.Meta.TransitionType}
|
|
}
|
|
|
|
func menuButtons(v menuRequest, menuID uint) []*biz.MenuButton {
|
|
buttons := make([]*biz.MenuButton, 0, len(v.MenuButtons))
|
|
for _, button := range v.MenuButtons {
|
|
buttons = append(buttons, &biz.MenuButton{ID: button.ID, Name: button.Name, Description: button.Description, MenuID: menuID})
|
|
}
|
|
return buttons
|
|
}
|
|
|
|
func registerMenuRoutes(group *gin.RouterGroup, svc *service.AccessService) {
|
|
router := group.Group("/menu")
|
|
router.POST("/getMenuList", func(c *gin.Context) {
|
|
items, err := svc.Menus(c.Request.Context())
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, pageResult{List: items, Total: int64(len(items)), Page: 1, PageSize: 999}, "获取成功")
|
|
})
|
|
router.POST("/getBaseMenuTree", func(c *gin.Context) {
|
|
items, err := svc.MenuTree(c.Request.Context())
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"menus": items}, "获取成功")
|
|
})
|
|
router.POST("/addBaseMenu", func(c *gin.Context) {
|
|
var req menuRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
menu := menuDomain(req)
|
|
if err := svc.CreateMenu(c.Request.Context(), menu); err != nil {
|
|
fail(c, "新增失败")
|
|
return
|
|
}
|
|
if err := svc.ReplaceMenuButtons(c.Request.Context(), menu.ID, menuButtons(req, menu.ID)); err != nil {
|
|
fail(c, "按钮保存失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/updateBaseMenu", func(c *gin.Context) {
|
|
var req menuRequest
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.UpdateMenu(c.Request.Context(), menuDomain(req)); err != nil {
|
|
fail(c, "更新失败")
|
|
return
|
|
}
|
|
if err := svc.ReplaceMenuButtons(c.Request.Context(), req.ID, menuButtons(req, req.ID)); err != nil {
|
|
fail(c, "按钮保存失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/deleteBaseMenu", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"ID"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.DeleteMenu(c.Request.Context(), req.ID); err != nil {
|
|
fail(c, err.Error())
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/getBaseMenuById", func(c *gin.Context) {
|
|
var req struct {
|
|
ID uint `json:"id"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
item, err := svc.Menu(c.Request.Context(), req.ID)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"menu": item}, "获取成功")
|
|
})
|
|
router.POST("/addMenuAuthority", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
Menus []struct {
|
|
ID uint `json:"ID"`
|
|
} `json:"menus"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
ids := make([]uint, 0, len(req.Menus))
|
|
for _, m := range req.Menus {
|
|
ids = append(ids, m.ID)
|
|
}
|
|
if err := svc.SetAuthorityMenus(c.Request.Context(), req.AuthorityID, ids); err != nil {
|
|
fail(c, "添加失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
router.POST("/getMenuAuthority", func(c *gin.Context) {
|
|
var req struct {
|
|
AuthorityID uint `json:"authorityId"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
menus, err := svc.AuthorityMenus(c.Request.Context(), req.AuthorityID)
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, gin.H{"menus": menus}, "获取成功")
|
|
})
|
|
router.GET("/getMenuRoles", func(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Query("menuId"), 10, 64)
|
|
ids, err := svc.MenuRoleIDs(c.Request.Context(), uint(id))
|
|
if err != nil {
|
|
fail(c, "获取失败")
|
|
return
|
|
}
|
|
writeResult(c, codeSuccess, ids, "获取成功")
|
|
})
|
|
router.POST("/setMenuRoles", func(c *gin.Context) {
|
|
var req struct {
|
|
MenuID uint `json:"menuId"`
|
|
AuthorityIDs []uint `json:"authorityIds"`
|
|
}
|
|
if c.ShouldBindJSON(&req) != nil {
|
|
fail(c, "参数错误")
|
|
return
|
|
}
|
|
if err := svc.SetMenuRoles(c.Request.Context(), req.MenuID, req.AuthorityIDs); err != nil {
|
|
fail(c, "设置失败")
|
|
return
|
|
}
|
|
ok(c)
|
|
})
|
|
}
|