Browse Source

修改v1/sys_menu.go的备注错误及代码优化

main
SliverHorn 4 years ago
parent
commit
e938c8c00b
  1. 144
      server/api/v1/sys_menu.go
  2. 9
      server/model/request/common.go
  3. 5
      server/model/request/sys_menu.go
  4. 2
      server/service/sys_authority.go
  5. 5
      server/service/sys_menu.go

144
server/api/v1/sys_menu.go

@ -11,18 +11,15 @@ import (
"github.com/gin-gonic/gin"
)
// @Tags authorityAndMenu
// @Tags AuthorityMenu
// @Summary 获取用户动态路由
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body request.RegisterAndLoginStruct true "可以什么都不填"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Param data body request.Empty true "空"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getMenu [post]
func GetMenu(c *gin.Context) {
claims, _ := c.Get("claims")
waitUse := claims.(*request.CustomClaims)
err, menus := service.GetMenuTree(waitUse.AuthorityId)
if err != nil {
if err, menus := service.GetMenuTree(getUserAuthorityId(c)); err != nil {
response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
} else {
response.OkWithData(resp.SysMenusResponse{Menus: menus}, c)
@ -34,15 +31,14 @@ func GetMenu(c *gin.Context) {
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.PageInfo true "分页获取基础menu列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Param data body request.PageInfo true "页码, 每页大小"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getMenuList [post]
func GetMenuList(c *gin.Context) {
var pageInfo request.PageInfo
_ = c.ShouldBindJSON(&pageInfo)
PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
if PageVerifyErr != nil {
response.FailWithMessage(PageVerifyErr.Error(), c)
if err := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"]); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err, menuList, total := service.GetInfoList()
@ -63,8 +59,8 @@ func GetMenuList(c *gin.Context) {
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysBaseMenu true "新增菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Param data body model.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /menu/addBaseMenu [post]
func AddBaseMenu(c *gin.Context) {
var menu model.SysBaseMenu
@ -76,21 +72,15 @@ func AddBaseMenu(c *gin.Context) {
"Component": {utils.NotEmpty()},
"Sort": {utils.Ge("0")},
}
MenuVerifyErr := utils.Verify(menu, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
if err := utils.Verify(menu, MenuVerify); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
MetaVerify := utils.Rules{
"Title": {utils.NotEmpty()},
}
MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
if MetaVerifyErr != nil {
response.FailWithMessage(MetaVerifyErr.Error(), c)
if err := utils.Verify(menu.Meta, utils.Rules{"Title": {utils.NotEmpty()}}); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err := service.AddBaseMenu(menu)
if err != nil {
if err := service.AddBaseMenu(menu); err != nil {
response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
} else {
response.OkWithMessage("添加成功", c)
@ -101,66 +91,55 @@ func AddBaseMenu(c *gin.Context) {
// @Summary 获取用户动态路由
// @Security ApiKeyAuth
// @Produce application/json
// @Param data body request.RegisterAndLoginStruct true "可以什么都不填"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
// @Param data body request.Empty true "空"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getBaseMenuTree [post]
func GetBaseMenuTree(c *gin.Context) {
err, menus := service.GetBaseMenuTree()
if err != nil {
if err, menus := service.GetBaseMenuTree(); err != nil {
response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
} else {
response.OkWithData(resp.SysBaseMenusResponse{Menus: menus}, c)
}
}
// @Tags authorityAndMenu
// @Tags AuthorityAndMenu
// @Summary 增加menu和角色关联关系
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.AddMenuAuthorityInfo true "增加menu和角色关联关系"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Param data body request.AddMenuAuthorityInfo true "角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /menu/addMenuAuthority [post]
func AddMenuAuthority(c *gin.Context) {
var addMenuAuthorityInfo request.AddMenuAuthorityInfo
_ = c.ShouldBindJSON(&addMenuAuthorityInfo)
MenuVerify := utils.Rules{
"AuthorityId": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(addMenuAuthorityInfo, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
var params request.AddMenuAuthorityInfo
_ = c.ShouldBindJSON(&params)
if err := utils.Verify(params, utils.Rules{"AuthorityId": {"notEmpty"}}); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err := service.AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId)
if err != nil {
if err := service.AddMenuAuthority(params.Menus, params.AuthorityId); err != nil {
response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
} else {
response.OkWithMessage("添加成功", c)
}
}
// @Tags authorityAndMenu
// @Tags AuthorityAndMenu
// @Summary 获取指定角色menu
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.AuthorityIdInfo true "增加menu和角色关联关系"
// @Param data body request.GetAuthorityId true "角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/GetMenuAuthority [post]
func GetMenuAuthority(c *gin.Context) {
var authorityIdInfo request.AuthorityIdInfo
_ = c.ShouldBindJSON(&authorityIdInfo)
MenuVerify := utils.Rules{
"AuthorityId": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(authorityIdInfo, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
var param request.GetAuthorityId
_ = c.ShouldBindJSON(&param)
if err := utils.Verify(param, utils.Rules{"AuthorityId": {"notEmpty"}}); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err, menus := service.GetMenuAuthority(authorityIdInfo.AuthorityId)
if err != nil {
if err, menus := service.GetMenuAuthority(&param); err != nil {
response.FailWithDetailed(response.ERROR, resp.SysMenusResponse{Menus: menus}, fmt.Sprintf("添加失败,%v", err), c)
} else {
response.Result(response.SUCCESS, gin.H{"menus": menus}, "获取成功", c)
@ -172,19 +151,17 @@ func GetMenuAuthority(c *gin.Context) {
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "删除菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Param data body request.GetById true "菜单id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /menu/deleteBaseMenu [post]
func DeleteBaseMenu(c *gin.Context) {
var idInfo request.GetById
_ = c.ShouldBindJSON(&idInfo)
IdVerifyErr := utils.Verify(idInfo, utils.CustomizeMap["IdVerify"])
if IdVerifyErr != nil {
response.FailWithMessage(IdVerifyErr.Error(), c)
var param request.GetById
_ = c.ShouldBindJSON(&param)
if err := utils.Verify(param, utils.CustomizeMap["IdVerify"]); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err := service.DeleteBaseMenu(idInfo.Id)
if err != nil {
if err := service.DeleteBaseMenu(param.Id); err != nil {
response.FailWithMessage(fmt.Sprintf("删除失败:%v", err), c)
} else {
response.OkWithMessage("删除成功", c)
@ -197,8 +174,8 @@ func DeleteBaseMenu(c *gin.Context) {
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysBaseMenu true "更新菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Param data body model.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /menu/updateBaseMenu [post]
func UpdateBaseMenu(c *gin.Context) {
var menu model.SysBaseMenu
@ -210,24 +187,18 @@ func UpdateBaseMenu(c *gin.Context) {
"Component": {utils.NotEmpty()},
"Sort": {utils.Ge("0")},
}
MenuVerifyErr := utils.Verify(menu, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
if err := utils.Verify(menu, MenuVerify); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
MetaVerify := utils.Rules{
"Title": {utils.NotEmpty()},
}
MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
if MetaVerifyErr != nil {
response.FailWithMessage(MetaVerifyErr.Error(), c)
if err := utils.Verify(menu.Meta, utils.Rules{"Title": {utils.NotEmpty()}}); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err := service.UpdateBaseMenu(menu)
if err != nil {
response.FailWithMessage(fmt.Sprintf("修改失败:%v", err), c)
if err := service.UpdateBaseMenu(menu); err != nil {
response.FailWithMessage(fmt.Sprintf("更新失败:%v", err), c)
} else {
response.OkWithMessage("修改成功", c)
response.OkWithMessage("更新成功", c)
}
}
@ -236,23 +207,18 @@ func UpdateBaseMenu(c *gin.Context) {
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.GetById true "根据id获取菜单"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Param data body request.GetById true "菜单id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getBaseMenuById [post]
func GetBaseMenuById(c *gin.Context) {
var idInfo request.GetById
_ = c.ShouldBindJSON(&idInfo)
MenuVerify := utils.Rules{
"Id": {"notEmpty"},
}
MenuVerifyErr := utils.Verify(idInfo, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
if err := utils.Verify(idInfo, utils.Rules{"Id": {"notEmpty"}}); err != nil {
response.FailWithMessage(err.Error(), c)
return
}
err, menu := service.GetBaseMenuById(idInfo.Id)
if err != nil {
response.FailWithMessage(fmt.Sprintf("查询失败:%v", err), c)
if err, menu := service.GetBaseMenuById(idInfo.Id); err != nil {
response.FailWithMessage(fmt.Sprintf("获取失败:%v", err), c)
} else {
response.OkWithData(resp.SysBaseMenuResponse{Menu: menu}, c)
}

9
server/model/request/common.go

@ -13,4 +13,11 @@ type GetById struct {
type IdsReq struct {
Ids []int `json:"ids" form:"ids"`
}
}
// Get role by id structure
type GetAuthorityId struct {
AuthorityId string
}
type Empty struct {}

5
server/model/request/sys_menu.go

@ -7,8 +7,3 @@ type AddMenuAuthorityInfo struct {
Menus []model.SysBaseMenu
AuthorityId string
}
// Get role by id structure
type AuthorityIdInfo struct {
AuthorityId string
}

2
server/service/sys_authority.go

@ -39,7 +39,7 @@ func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, autho
return errors.New("存在相同角色id"), authority
}
copyInfo.Authority.Children = []model.SysAuthority{}
err, menus := GetMenuAuthority(copyInfo.OldAuthorityId)
err, menus := GetMenuAuthority(&request.GetAuthorityId{AuthorityId: copyInfo.OldAuthorityId})
var baseMenu []model.SysBaseMenu
for _, v := range menus {
intNum, _ := strconv.Atoi(v.MenuId)

5
server/service/sys_menu.go

@ -4,6 +4,7 @@ import (
"errors"
"gin-vue-admin/global"
"gin-vue-admin/model"
"gin-vue-admin/model/request"
"gorm.io/gorm"
"strconv"
)
@ -155,9 +156,9 @@ func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error)
// @return err error
// @return menus []SysBaseMenu
func GetMenuAuthority(authorityId string) (err error, menus []model.SysMenu) {
func GetMenuAuthority(info *request.GetAuthorityId) (err error, menus []model.SysMenu) {
err = global.GVA_DB.Where("authority_id = ? ", info.AuthorityId).Order("sort").Find(&menus).Error
//sql := "SELECT authority_menu.keep_alive,authority_menu.default_menu,authority_menu.created_at,authority_menu.updated_at,authority_menu.deleted_at,authority_menu.menu_level,authority_menu.parent_id,authority_menu.path,authority_menu.`name`,authority_menu.hidden,authority_menu.component,authority_menu.title,authority_menu.icon,authority_menu.sort,authority_menu.menu_id,authority_menu.authority_id FROM authority_menu WHERE authority_menu.authority_id = ? ORDER BY authority_menu.sort ASC"
err = global.GVA_DB.Where("authority_id = ? ", authorityId).Order("sort").Find(&menus).Error
//err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
return err, menus
}
Loading…
Cancel
Save