新增获取菜单列表

This commit is contained in:
wucongxing 2023-06-12 15:25:23 +08:00
parent 6b61f6ca43
commit c4d8dd6791
11 changed files with 169 additions and 16 deletions

View File

@ -40,12 +40,12 @@ func (b *Basic) Login(c *gin.Context) {
}
// 验证验证码
isValid := utils.VerifyCaptcha(login)
if !isValid {
// 验证码错误
responses.FailWithMessage("验证码错误", c)
return
}
// isValid := utils.VerifyCaptcha(login)
// if !isValid {
// // 验证码错误
// responses.FailWithMessage("验证码错误", c)
// return
// }
// 登陆
Basic := service.Basic{}

View File

@ -1,10 +1,33 @@
package controller
import "github.com/gin-gonic/gin"
import (
"github.com/gin-gonic/gin"
"hospital-admin-api/api/responses"
"hospital-admin-api/api/service"
)
type Role struct{}
// GetRoleMenuList 获取角色菜单列表
func (r *Role) GetRoleMenuList(c *gin.Context) {
roleId := c.GetInt64("RoleId")
if roleId == 0 {
responses.Fail(c)
return
}
// 登陆
Role := service.Role{}
roleMenuList, err := Role.GetRoleMenuList(roleId)
if err != nil {
responses.FailWithMessage(err.Error(), c)
return
}
if len(roleMenuList) == 0 {
responses.FailWithMessage("请联系管理人员设置菜单", c)
return
}
responses.OkWithData(roleMenuList, c)
}

View File

@ -1,2 +1,25 @@
package dao
import (
"hospital-admin-api/api/model"
"hospital-admin-api/global"
)
type AdminMenu struct {
}
func (r *AdminMenu) GetAdminMenuList() (m []*model.AdminMenu, err error) {
err = global.Db.Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
func (r *AdminMenu) GetAdminMenuListSortOrderNum() (m []*model.AdminMenu, err error) {
err = global.Db.Order("order_num asc").Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}

View File

@ -8,7 +8,7 @@ import (
type AdminMenuApi struct {
}
// GetAdminMenuApiListByMenuID 菜单id获取菜单api
// GetAdminMenuApiListByMenuID 获取菜单api-菜单id
// menuId 菜单id
func (r *AdminMenuApi) GetAdminMenuApiListByMenuID(menuId int64) (m []*model.AdminMenuApi, err error) {
err = global.Db.Where("menu_id = ?", menuId).Preload("API").Find(&m).Error

View File

@ -8,9 +8,18 @@ import (
type AdminRoleMenu struct {
}
// GetAdminMenuListByRoleId GetAdminRoleById 角色id获取用户角色
// GetAdminRoleMenuListByRoleId 获取角色菜单-角色id
// roleId 角色id
func (r *AdminRoleMenu) GetAdminMenuListByRoleId(roleId int64) (m []*model.AdminRoleMenu, err error) {
func (r *AdminRoleMenu) GetAdminRoleMenuListByRoleId(roleId int64) (m []*model.AdminRoleMenu, err error) {
err = global.Db.Where("role_id = ?", roleId).Find(&m).Error
if err != nil {
return nil, err
}
return m, nil
}
// GetAdminRoleMenuListWithMenuByRoleId 获取角色菜单
func (r *AdminRoleMenu) GetAdminRoleMenuListWithMenuByRoleId(roleId int64) (m []*model.AdminRoleMenu, err error) {
err = global.Db.Where("role_id = ?", roleId).Preload("Menu").Find(&m).Error
if err != nil {
return nil, err

View File

@ -64,9 +64,9 @@ func Auth() gin.HandlerFunc {
return
}
// 获取角色菜单
// 获取角色菜单id
AdminRoleMenuDao := dao.AdminRoleMenu{}
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminMenuListByRoleId(roleId)
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminRoleMenuListByRoleId(roleId)
if adminRoleMenu == nil {
c.JSON(http.StatusForbidden, gin.H{
"message": "暂无权限",

View File

@ -2,7 +2,6 @@ package middlewares
import (
"github.com/gin-gonic/gin"
"hospital-admin-api/api/responses"
"hospital-admin-api/consts"
"hospital-admin-api/global"
"hospital-admin-api/utils"
@ -29,8 +28,13 @@ func Jwt() gin.HandlerFunc {
// 检测是否存在黑名单
res, _ := global.Redis.Get(c, "jwt_black_"+authorization).Result()
if res != "" {
responses.Fail(c)
c.JSON(http.StatusOK, gin.H{
"message": "token错误/过期",
"code": consts.TOKEN_ERROR,
"data": "",
})
c.Abort()
return

View File

@ -1,12 +1,13 @@
package model
// AdminApi 后台-接口表
// AdminAPI 后台-接口表
type AdminAPI struct {
Model
APIID int64 `gorm:"column:api_id;type:bigint(19);primary_key;AUTO_INCREMENT;comment:主键id" json:"api_id"`
APIName string `gorm:"column:api_name;type:varchar(100);comment:api名称;NOT NULL" json:"api_name"`
APIPath string `gorm:"column:api_path;type:varchar(255);comment:接口路径(全路径 id为:id;NOT NULL" json:"api_path"`
APIMethod string `gorm:"column:api_method;type:varchar(10);comment:请求类型put:修改 post:新增 get:获取 ;NOT NULL" json:"api_method"`
IsAuth int `gorm:"column:is_auth;type:tinyint(1);default:1;comment:是否验证权限0:否 1:是)" json:"is_auth"`
}
func (m *AdminAPI) TableName() string {

View File

@ -5,7 +5,7 @@ type AdminMenu struct {
Model
MenuId int64 `gorm:"column:menu_id;type:bigint(19);primary_key;comment:主键id" json:"menu_id"`
MenuName string `gorm:"column:menu_name;type:varchar(30);comment:菜单名称" json:"menu_name"`
ParentId int `gorm:"column:parent_id;type:int(10);default:0;comment:父菜单ID0表示一级" json:"parent_id"`
ParentId int64 `gorm:"column:parent_id;type:int(10);default:0;comment:父菜单ID0表示一级" json:"parent_id"`
MenuStatus int `gorm:"column:menu_status;type:tinyint(1);default:1;comment:菜单状态0:隐藏 1:正常)此优先级最高" json:"menu_status"`
MenuType int `gorm:"column:menu_type;type:tinyint(1);comment:菜单类型1:模块 2:菜单 2:按钮)" json:"menu_type"`
Permission string `gorm:"column:permission;type:varchar(255);comment:标识" json:"permission"`

View File

@ -0,0 +1,15 @@
package roleResponse
// RoleMenuList 登陆
type RoleMenuList struct {
MenuId int64 `json:"menu_id"` // 主键id
MenuName string `json:"menu_name"` // 菜单名称
ParentId int64 `json:"parent_id"` // 父菜单ID0表示一级
MenuStatus int `json:"menu_status"` // 菜单状态0:隐藏 1:正常)此优先级最高
MenuType int `json:"menu_type"` // 菜单类型1:模块 2:菜单 2:按钮)
Permission string `json:"permission"` // 标识
OrderNum int `json:"order_num"` // 显示顺序
Icon string `json:"icon"` // 图标地址
Path string `json:"path"` // 页面地址(#表示当前页)
Children []*RoleMenuList `json:"children"` // 下级页面
}

78
api/service/role.go Normal file
View File

@ -0,0 +1,78 @@
package service
import (
"hospital-admin-api/api/dao"
"hospital-admin-api/api/model"
"hospital-admin-api/api/responses/roleResponse"
)
type Role struct{}
// GetRoleMenuList 获取角色菜单
func (r *Role) GetRoleMenuList(roleId int64) ([]*roleResponse.RoleMenuList, error) {
AdminRoleMenuDao := dao.AdminRoleMenu{}
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminRoleMenuListByRoleId(roleId)
if adminRoleMenu == nil {
return []*roleResponse.RoleMenuList{}, nil
}
// 获取全部菜单
AdminMenuDao := dao.AdminMenu{}
adminMenu, _ := AdminMenuDao.GetAdminMenuListSortOrderNum()
if adminMenu == nil {
return []*roleResponse.RoleMenuList{}, nil
}
var menuIDs []int64
var menuTree []*roleResponse.RoleMenuList
if len(adminRoleMenu) > 0 {
for _, roleMenu := range adminRoleMenu {
menuIDs = append(menuIDs, roleMenu.MenuID)
}
// 构建菜单树
menuTree = buildMenuTree(menuIDs, adminMenu)
}
return menuTree, nil
}
// 构建菜单树
func buildMenuTree(menuIds []int64, menuData []*model.AdminMenu) []*roleResponse.RoleMenuList {
menuMap := make(map[int64]*roleResponse.RoleMenuList)
rootNodes := make([]*roleResponse.RoleMenuList, 0)
// 构建菜单映射
for _, menu := range menuData {
for _, v := range menuIds {
if v == menu.MenuId {
node := &roleResponse.RoleMenuList{
MenuId: menu.MenuId,
MenuName: menu.MenuName,
ParentId: menu.ParentId,
MenuStatus: menu.MenuStatus,
MenuType: menu.MenuType,
Permission: menu.Permission,
OrderNum: menu.OrderNum,
Icon: menu.Icon,
Path: menu.Path,
Children: nil,
}
menuMap[menu.MenuId] = node
}
}
}
// 构建菜单树
for _, menu := range menuData {
if menu.ParentId == 0 {
rootNodes = append(rootNodes, menuMap[menu.MenuId])
} else if parent, ok := menuMap[menu.ParentId]; ok {
parent.Children = append(parent.Children, menuMap[menu.MenuId])
}
}
return rootNodes
}