新增菜单详情,增加返回api。修改部门详情返回数据
This commit is contained in:
parent
2e6df8c974
commit
a22dd7c38c
@ -2,7 +2,6 @@ package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/api/responses/menuResponse"
|
||||
@ -138,8 +137,8 @@ func (r *Menu) PutMenu(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 业务处理
|
||||
MenuService := service.MenuService{}
|
||||
_, err = MenuService.PutMenu(menuId, MenuRequest.PutMenu)
|
||||
menuService := service.MenuService{}
|
||||
_, err = menuService.PutMenu(menuId, MenuRequest.PutMenu)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
@ -188,42 +187,28 @@ func (r *Menu) GetMenu(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
AdminMenuDao := dao.AdminMenuDao{}
|
||||
menuService := service.MenuService{}
|
||||
|
||||
adminMenu, _ := AdminMenuDao.GetAdminMenuById(menuId)
|
||||
if adminMenu == nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 转换int64为string
|
||||
MenuId := strconv.FormatInt(adminMenu.MenuId, 10)
|
||||
// 获取菜单详情
|
||||
adminMenu, err := menuService.GetMenu(menuId)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
ParentId := strconv.FormatInt(adminMenu.ParentId, 10)
|
||||
// 获取菜单api
|
||||
getAdminMenuApis, err := menuService.GetAdminMenuApi(menuId)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
GetMenuResponse := &menuResponse.GetMenu{
|
||||
MenuId: MenuId,
|
||||
MenuName: adminMenu.MenuName,
|
||||
MenuTitle: adminMenu.MenuTitle,
|
||||
ParentId: ParentId,
|
||||
MenuStatus: adminMenu.MenuStatus,
|
||||
MenuType: adminMenu.MenuType,
|
||||
Permission: adminMenu.Permission,
|
||||
OrderNum: adminMenu.OrderNum,
|
||||
Icon: adminMenu.Icon,
|
||||
Path: adminMenu.Path,
|
||||
Component: adminMenu.Component,
|
||||
CreatedAt: adminMenu.CreatedAt,
|
||||
UpdatedAt: adminMenu.UpdatedAt,
|
||||
// 处理返回值
|
||||
getMenuResponse, err := menuResponse.GetMenuResponse(adminMenu, getAdminMenuApis)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.OkWithData(GetMenuResponse, c)
|
||||
responses.OkWithData(getMenuResponse, c)
|
||||
}
|
||||
|
||||
@ -12,6 +12,15 @@ type AdminMenuApiDao struct {
|
||||
// GetAdminMenuApiListByMenuID 获取菜单api-菜单id
|
||||
// menuId 菜单id
|
||||
func (r *AdminMenuApiDao) GetAdminMenuApiListByMenuID(menuId int64) (m []*model.AdminMenuApi, err error) {
|
||||
err = global.Db.Where("menu_id = ?", menuId).Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetAdminMenuApiListWithAPIByMenuID 获取菜单api,关联api模型-菜单id
|
||||
func (r *AdminMenuApiDao) GetAdminMenuApiListWithAPIByMenuID(menuId int64) (m []*model.AdminMenuApi, err error) {
|
||||
err = global.Db.Where("menu_id = ?", menuId).Preload("API").Find(&m).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -34,3 +43,15 @@ func (r *AdminMenuApiDao) DeleteAdminMenuApiByMenuId(tx *gorm.DB, menuId int64)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAdminMenuApiApiIdByMenuId 获取菜单接口id-菜单id
|
||||
func (r *AdminMenuApiDao) GetAdminMenuApiApiIdByMenuId(menuId int64) (m []int64, err error) {
|
||||
err = global.Db.Model(&model.AdminRoleMenu{}).
|
||||
Where("menu_id = ?", menuId).
|
||||
Pluck("api_id", &m).
|
||||
Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
@ -130,7 +130,7 @@ func Auth() gin.HandlerFunc {
|
||||
// 获取菜单对应api
|
||||
adminMenuApiDao := dao.AdminMenuApiDao{}
|
||||
for _, v := range adminRoleMenu {
|
||||
AdminMenuApi, _ := adminMenuApiDao.GetAdminMenuApiListByMenuID(v.MenuID)
|
||||
AdminMenuApi, _ := adminMenuApiDao.GetAdminMenuApiListWithAPIByMenuID(v.MenuID)
|
||||
if AdminMenuApi == nil {
|
||||
// 菜单无需权限
|
||||
c.Next()
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package menuResponse
|
||||
|
||||
import "hospital-admin-api/api/model"
|
||||
import (
|
||||
"hospital-admin-api/api/model"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetMenuList 获取菜单列表
|
||||
type GetMenuList struct {
|
||||
@ -19,18 +22,66 @@ type GetMenuList struct {
|
||||
}
|
||||
|
||||
// GetMenu 菜单详情
|
||||
type GetMenu struct {
|
||||
MenuId string `json:"menu_id"`
|
||||
MenuName string `json:"menu_name"` // 菜单名称
|
||||
MenuTitle string `json:"menu_title"` // 菜单名称
|
||||
ParentId string `json:"parent_id"` // 父菜单ID(0表示一级)
|
||||
MenuStatus int `json:"menu_status"` // 菜单状态(0:隐藏 1:正常)此优先级最高
|
||||
MenuType int `json:"menu_type"` // 菜单类型(1:模块 2:菜单 3:按钮)
|
||||
Permission string `json:"permission"` // 标识
|
||||
OrderNum int `json:"order_num"` // 显示顺序
|
||||
Icon string `json:"icon"` // 图标地址
|
||||
Path string `json:"path"` // 页面地址(#表示当前页)
|
||||
Component string `json:"component"` // 组件名称
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||
type getMenu struct {
|
||||
MenuId string `json:"menu_id"`
|
||||
MenuName string `json:"menu_name"` // 菜单名称
|
||||
MenuTitle string `json:"menu_title"` // 菜单名称
|
||||
ParentId string `json:"parent_id"` // 父菜单ID(0表示一级)
|
||||
MenuStatus int `json:"menu_status"` // 菜单状态(0:隐藏 1:正常)此优先级最高
|
||||
MenuType int `json:"menu_type"` // 菜单类型(1:模块 2:菜单 3:按钮)
|
||||
Permission string `json:"permission"` // 标识
|
||||
OrderNum int `json:"order_num"` // 显示顺序
|
||||
Icon string `json:"icon"` // 图标地址
|
||||
Path string `json:"path"` // 页面地址(#表示当前页)
|
||||
Component string `json:"component"` // 组件名称
|
||||
Api []*getAdminMenuApi `json:"api"` // 接口数据
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||
}
|
||||
|
||||
// 接口id
|
||||
type getAdminMenuApi struct {
|
||||
ApiId string `json:"api_id"` // 接口id
|
||||
ApiName string `json:"api_name"` // 接口名称
|
||||
}
|
||||
|
||||
// GetMenuResponse 菜单详情
|
||||
func GetMenuResponse(adminMenu *model.AdminMenu, adminMenuApi []*model.AdminMenuApi) (*getMenu, error) {
|
||||
var getMenuResponse *getMenu
|
||||
|
||||
getAdminMenuApis := make([]*getAdminMenuApi, len(adminMenuApi))
|
||||
|
||||
if adminMenu != nil {
|
||||
getMenuResponse = &getMenu{
|
||||
MenuId: strconv.Itoa(int(adminMenu.MenuId)),
|
||||
MenuName: adminMenu.MenuName,
|
||||
MenuTitle: adminMenu.MenuTitle,
|
||||
ParentId: strconv.Itoa(int(adminMenu.ParentId)),
|
||||
MenuStatus: adminMenu.MenuStatus,
|
||||
MenuType: adminMenu.MenuType,
|
||||
Permission: adminMenu.Permission,
|
||||
OrderNum: adminMenu.OrderNum,
|
||||
Icon: adminMenu.Icon,
|
||||
Path: adminMenu.Path,
|
||||
Component: adminMenu.Component,
|
||||
CreatedAt: adminMenu.CreatedAt,
|
||||
UpdatedAt: adminMenu.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
if getMenuResponse != nil && len(adminMenuApi) != 0 {
|
||||
for i, v := range adminMenuApi {
|
||||
result := &getAdminMenuApi{
|
||||
ApiId: strconv.FormatInt(v.ApiId, 10),
|
||||
ApiName: v.API.APIName,
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
getAdminMenuApis[i] = result
|
||||
}
|
||||
|
||||
getMenuResponse.Api = getAdminMenuApis
|
||||
}
|
||||
|
||||
return getMenuResponse, nil
|
||||
}
|
||||
|
||||
@ -9,7 +9,7 @@ import (
|
||||
type getPostPage struct {
|
||||
PostId string `json:"post_id"` // 主键id
|
||||
PostName string `json:"post_name"` // 岗位名称
|
||||
DeptStatus int `json:"dept_status"` // 岗位状态(1:正常 2:删除)
|
||||
PostStatus int `json:"post_status"` // 岗位状态(1:正常 2:删除)
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||
}
|
||||
@ -18,7 +18,7 @@ type getPostPage struct {
|
||||
type GetPost struct {
|
||||
PostId string `json:"post_id"` // 主键id
|
||||
PostName string `json:"post_name"` // 岗位名称
|
||||
DeptStatus int `json:"dept_status"` // 岗位状态(1:正常 2:删除)
|
||||
PostStatus int `json:"post_status"` // 岗位状态(1:正常 2:删除)
|
||||
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||
}
|
||||
@ -34,7 +34,7 @@ func GetPostPageResponse(adminPost []*model.AdminPost) []getPostPage {
|
||||
getPostPageResponse := getPostPage{
|
||||
PostId: strconv.Itoa(int(v.PostId)),
|
||||
PostName: v.PostName,
|
||||
DeptStatus: v.PostStatus,
|
||||
PostStatus: v.PostStatus,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
@ -52,7 +52,7 @@ func GetPostResponse(adminPost *model.AdminPost) *GetPost {
|
||||
return &GetPost{
|
||||
PostId: strconv.Itoa(int(adminPost.PostId)),
|
||||
PostName: adminPost.PostName,
|
||||
DeptStatus: adminPost.PostStatus,
|
||||
PostStatus: adminPost.PostStatus,
|
||||
CreatedAt: adminPost.CreatedAt,
|
||||
UpdatedAt: adminPost.UpdatedAt,
|
||||
}
|
||||
|
||||
@ -304,3 +304,25 @@ func (r *MenuService) DeleteMenu(c *gin.Context, DeleteMenuRequest requests.Dele
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// GetMenu 菜单详情
|
||||
func (r *MenuService) GetMenu(menuId int64) (*model.AdminMenu, error) {
|
||||
adminMenuDao := dao.AdminMenuDao{}
|
||||
|
||||
adminMenu, _ := adminMenuDao.GetAdminMenuById(menuId)
|
||||
if adminMenu == nil {
|
||||
return nil, errors.New("数据错误")
|
||||
}
|
||||
return adminMenu, nil
|
||||
}
|
||||
|
||||
// GetAdminMenuApi 获取菜单关联api-菜单id
|
||||
func (r *MenuService) GetAdminMenuApi(menuId int64) ([]*model.AdminMenuApi, error) {
|
||||
adminMenuApiDao := dao.AdminMenuApiDao{}
|
||||
adminMenuApis, err := adminMenuApiDao.GetAdminMenuApiListWithAPIByMenuID(menuId)
|
||||
if err != nil {
|
||||
return adminMenuApis, errors.New("数据错误")
|
||||
}
|
||||
|
||||
return adminMenuApis, nil
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user