301 lines
8.2 KiB
Go
301 lines
8.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"ems.agt/src/framework/config"
|
|
"ems.agt/src/framework/constants/common"
|
|
"ems.agt/src/framework/constants/menu"
|
|
"ems.agt/src/framework/utils/ctx"
|
|
"ems.agt/src/framework/utils/regular"
|
|
"ems.agt/src/framework/vo/result"
|
|
"ems.agt/src/modules/system/model"
|
|
"ems.agt/src/modules/system/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
)
|
|
|
|
// 实例化控制层 SysMenuController 结构体
|
|
var NewSysMenu = &SysMenuController{
|
|
sysMenuService: service.NewSysMenuImpl,
|
|
}
|
|
|
|
// 菜单信息
|
|
//
|
|
// PATH /system/menu
|
|
type SysMenuController struct {
|
|
// 菜单服务
|
|
sysMenuService service.ISysMenu
|
|
}
|
|
|
|
// 菜单列表
|
|
//
|
|
// GET /list
|
|
func (s *SysMenuController) List(c *gin.Context) {
|
|
query := model.SysMenu{}
|
|
if v, ok := c.GetQuery("menuName"); ok {
|
|
query.MenuName = v
|
|
}
|
|
if v, ok := c.GetQuery("status"); ok {
|
|
query.Status = v
|
|
}
|
|
|
|
userId := ctx.LoginUserToUserID(c)
|
|
if config.IsAdmin(userId) {
|
|
userId = "*"
|
|
}
|
|
data := s.sysMenuService.SelectMenuList(query, userId)
|
|
c.JSON(200, result.OkData(data))
|
|
}
|
|
|
|
// 菜单信息
|
|
//
|
|
// GET /:menuId
|
|
func (s *SysMenuController) Info(c *gin.Context) {
|
|
menuId := c.Param("menuId")
|
|
if menuId == "" {
|
|
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
|
return
|
|
}
|
|
data := s.sysMenuService.SelectMenuById(menuId)
|
|
if data.MenuID == menuId {
|
|
c.JSON(200, result.OkData(data))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 菜单新增
|
|
//
|
|
// POST /
|
|
func (s *SysMenuController) Add(c *gin.Context) {
|
|
var body model.SysMenu
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil || body.MenuID != "" {
|
|
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
|
return
|
|
}
|
|
|
|
// 目录和菜单检查地址唯一
|
|
if menu.TYPE_DIR == body.MenuType || menu.TYPE_MENU == body.MenuType {
|
|
uniqueNenuPath := s.sysMenuService.CheckUniqueMenuPath(body.Path, body.ParentID, "")
|
|
if !uniqueNenuPath {
|
|
// 菜单新增【%s】失败,菜单路由地址已存在
|
|
msg := fmt.Sprintf("Menu add [%s] failed, menu routing address already exists", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
}
|
|
|
|
// 检查名称唯一
|
|
uniqueNenuName := s.sysMenuService.CheckUniqueMenuName(body.MenuName, body.ParentID, "")
|
|
if !uniqueNenuName {
|
|
// 菜单新增【%s】失败,菜单名称已存在
|
|
msg := fmt.Sprintf("Menu add [%s] failed, menu name already exists", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
// 外链菜单需要符合网站http(s)开头
|
|
if body.IsFrame == common.STATUS_NO && !regular.ValidHttp(body.Path) {
|
|
// 菜单新增【%s】失败,非内部地址必须以http(s)://开头
|
|
msg := fmt.Sprintf("Menu adds [%s] failure, non-internal addresses must start with http(s)://", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
body.CreateBy = ctx.LoginUserToUserName(c)
|
|
insertId := s.sysMenuService.InsertMenu(body)
|
|
if insertId != "" {
|
|
c.JSON(200, result.Ok(nil))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 菜单修改
|
|
//
|
|
// PUT /
|
|
func (s *SysMenuController) Edit(c *gin.Context) {
|
|
var body model.SysMenu
|
|
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
|
if err != nil || body.MenuID == "" {
|
|
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
|
return
|
|
}
|
|
|
|
// 上级菜单不能选自己
|
|
if body.MenuID == body.ParentID {
|
|
// 菜单修改【%s】失败,上级菜单不能选择自己
|
|
msg := fmt.Sprintf("Menu modification [%s] fails, the parent menu cannot select itself", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
// 检查数据是否存在
|
|
menuInfo := s.sysMenuService.SelectMenuById(body.MenuID)
|
|
if menuInfo.MenuID != body.MenuID {
|
|
// 没有可访问菜单数据
|
|
c.JSON(200, result.ErrMsg("No accessible menu data"))
|
|
return
|
|
}
|
|
// 父级ID不为0是要检查
|
|
if body.ParentID != "0" {
|
|
menuParent := s.sysMenuService.SelectMenuById(body.ParentID)
|
|
if menuParent.MenuID != body.ParentID {
|
|
c.JSON(200, result.ErrMsg("No accessible menu data"))
|
|
return
|
|
}
|
|
// 禁用菜单时检查父菜单是否使用
|
|
if body.Status == common.STATUS_YES && menuParent.Status == common.STATUS_NO {
|
|
// 上级菜单未启用!
|
|
c.JSON(200, result.ErrMsg("The parent menu is not enabled!"))
|
|
return
|
|
}
|
|
}
|
|
|
|
// 目录和菜单检查地址唯一
|
|
if menu.TYPE_DIR == body.MenuType || menu.TYPE_MENU == body.MenuType {
|
|
uniqueNenuPath := s.sysMenuService.CheckUniqueMenuPath(body.Path, body.ParentID, body.MenuID)
|
|
if !uniqueNenuPath {
|
|
// 菜单修改【%s】失败,菜单路由地址已存在
|
|
msg := fmt.Sprintf("Menu modification [%s] failed, menu routing address already exists", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
}
|
|
|
|
// 检查名称唯一
|
|
uniqueNenuName := s.sysMenuService.CheckUniqueMenuName(body.MenuName, body.ParentID, body.MenuID)
|
|
if !uniqueNenuName {
|
|
// 菜单修改【%s】失败,菜单名称已存在
|
|
msg := fmt.Sprintf("Menu modification [%s] failed, menu name already exists", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
// 外链菜单需要符合网站http(s)开头
|
|
if body.IsFrame == common.STATUS_NO && !regular.ValidHttp(body.Path) {
|
|
// 菜单修改【%s】失败,非内部地址必须以http(s)://开头
|
|
msg := fmt.Sprintf("Menu change [%s] failed, non-internal address must start with http(s)://", body.MenuName)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
// 禁用菜单时检查子菜单是否使用
|
|
if body.Status == common.STATUS_NO {
|
|
hasStatus := s.sysMenuService.HasChildByMenuIdAndStatus(body.MenuID, common.STATUS_YES)
|
|
if hasStatus > 0 {
|
|
// 不允许禁用,存在使用子菜单数:%d
|
|
msg := fmt.Sprintf("Disabling is not allowed, number of submenus present for use: %d", hasStatus)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
}
|
|
|
|
body.UpdateBy = ctx.LoginUserToUserName(c)
|
|
rows := s.sysMenuService.UpdateMenu(body)
|
|
if rows > 0 {
|
|
c.JSON(200, result.Ok(nil))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 菜单删除
|
|
//
|
|
// DELETE /:menuId
|
|
func (s *SysMenuController) Remove(c *gin.Context) {
|
|
menuId := c.Param("menuId")
|
|
if menuId == "" {
|
|
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
|
return
|
|
}
|
|
|
|
// 检查数据是否存在
|
|
menu := s.sysMenuService.SelectMenuById(menuId)
|
|
if menu.MenuID != menuId {
|
|
// 没有可访问菜单数据!
|
|
c.JSON(200, result.ErrMsg("There is no accessible menu data!"))
|
|
return
|
|
}
|
|
|
|
// 检查是否存在子菜单
|
|
hasChild := s.sysMenuService.HasChildByMenuIdAndStatus(menuId, "")
|
|
if hasChild > 0 {
|
|
// 不允许删除,存在子菜单数:%d
|
|
msg := fmt.Sprintf("Deletion not allowed, number of submenus present: %d", hasChild)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
// 检查是否分配给角色
|
|
existRole := s.sysMenuService.CheckMenuExistRole(menuId)
|
|
if existRole > 0 {
|
|
// 不允许删除,菜单已分配给角色数:%d
|
|
msg := fmt.Sprintf("Deletion not allowed, number of roles assigned to the menu: %d", existRole)
|
|
c.JSON(200, result.ErrMsg(msg))
|
|
return
|
|
}
|
|
|
|
rows := s.sysMenuService.DeleteMenuById(menuId)
|
|
if rows > 0 {
|
|
msg := fmt.Sprintf("Deleted successfully: %d", rows)
|
|
c.JSON(200, result.OkMsg(msg))
|
|
return
|
|
}
|
|
c.JSON(200, result.Err(nil))
|
|
}
|
|
|
|
// 菜单树结构列表
|
|
//
|
|
// GET /treeSelect
|
|
func (s *SysMenuController) TreeSelect(c *gin.Context) {
|
|
query := model.SysMenu{}
|
|
if v, ok := c.GetQuery("menuName"); ok {
|
|
query.MenuName = v
|
|
}
|
|
if v, ok := c.GetQuery("status"); ok {
|
|
query.Status = v
|
|
}
|
|
|
|
userId := ctx.LoginUserToUserID(c)
|
|
if config.IsAdmin(userId) {
|
|
userId = "*"
|
|
}
|
|
data := s.sysMenuService.SelectMenuTreeSelectByUserId(query, userId)
|
|
c.JSON(200, result.OkData(data))
|
|
|
|
}
|
|
|
|
// 菜单树结构列表(指定角色)
|
|
//
|
|
// GET /roleMenuTreeSelect/:roleId
|
|
func (s *SysMenuController) RoleMenuTreeSelect(c *gin.Context) {
|
|
roleId := c.Param("roleId")
|
|
if roleId == "" {
|
|
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
|
return
|
|
}
|
|
|
|
query := model.SysMenu{}
|
|
if v, ok := c.GetQuery("menuName"); ok {
|
|
query.MenuName = v
|
|
}
|
|
if v, ok := c.GetQuery("status"); ok {
|
|
query.Status = v
|
|
}
|
|
|
|
userId := ctx.LoginUserToUserID(c)
|
|
if config.IsAdmin(userId) {
|
|
userId = "*"
|
|
}
|
|
menuTreeSelect := s.sysMenuService.SelectMenuTreeSelectByUserId(query, userId)
|
|
checkedKeys := s.sysMenuService.SelectMenuListByRoleId(roleId)
|
|
c.JSON(200, result.OkData(map[string]any{
|
|
"menus": menuTreeSelect,
|
|
"checkedKeys": checkedKeys,
|
|
}))
|
|
}
|