提交 e938c8c0 编写于 作者: m0_50812349's avatar m0_50812349

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

上级 1ed7d3c6
...@@ -11,18 +11,15 @@ import ( ...@@ -11,18 +11,15 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// @Tags authorityAndMenu // @Tags AuthorityMenu
// @Summary 获取用户动态路由 // @Summary 获取用户动态路由
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Produce application/json // @Produce application/json
// @Param data body request.RegisterAndLoginStruct true "可以什么都不填" // @Param data body request.Empty true "空"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getMenu [post] // @Router /menu/getMenu [post]
func GetMenu(c *gin.Context) { func GetMenu(c *gin.Context) {
claims, _ := c.Get("claims") if err, menus := service.GetMenuTree(getUserAuthorityId(c)); err != nil {
waitUse := claims.(*request.CustomClaims)
err, menus := service.GetMenuTree(waitUse.AuthorityId)
if err != nil {
response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c) response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
} else { } else {
response.OkWithData(resp.SysMenusResponse{Menus: menus}, c) response.OkWithData(resp.SysMenusResponse{Menus: menus}, c)
...@@ -34,15 +31,14 @@ func GetMenu(c *gin.Context) { ...@@ -34,15 +31,14 @@ func GetMenu(c *gin.Context) {
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce application/json // @Produce application/json
// @Param data body request.PageInfo true "分页获取基础menu列表" // @Param data body request.PageInfo true "页码, 每页大小"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getMenuList [post] // @Router /menu/getMenuList [post]
func GetMenuList(c *gin.Context) { func GetMenuList(c *gin.Context) {
var pageInfo request.PageInfo var pageInfo request.PageInfo
_ = c.ShouldBindJSON(&pageInfo) _ = c.ShouldBindJSON(&pageInfo)
PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"]) if err := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"]); err != nil {
if PageVerifyErr != nil { response.FailWithMessage(err.Error(), c)
response.FailWithMessage(PageVerifyErr.Error(), c)
return return
} }
err, menuList, total := service.GetInfoList() err, menuList, total := service.GetInfoList()
...@@ -63,8 +59,8 @@ func GetMenuList(c *gin.Context) { ...@@ -63,8 +59,8 @@ func GetMenuList(c *gin.Context) {
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce application/json // @Produce application/json
// @Param data body model.SysBaseMenu true "新增菜单" // @Param data body model.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /menu/addBaseMenu [post] // @Router /menu/addBaseMenu [post]
func AddBaseMenu(c *gin.Context) { func AddBaseMenu(c *gin.Context) {
var menu model.SysBaseMenu var menu model.SysBaseMenu
...@@ -76,21 +72,15 @@ func AddBaseMenu(c *gin.Context) { ...@@ -76,21 +72,15 @@ func AddBaseMenu(c *gin.Context) {
"Component": {utils.NotEmpty()}, "Component": {utils.NotEmpty()},
"Sort": {utils.Ge("0")}, "Sort": {utils.Ge("0")},
} }
MenuVerifyErr := utils.Verify(menu, MenuVerify) if err := utils.Verify(menu, MenuVerify); err != nil {
if MenuVerifyErr != nil { response.FailWithMessage(err.Error(), c)
response.FailWithMessage(MenuVerifyErr.Error(), c)
return return
} }
MetaVerify := utils.Rules{ if err := utils.Verify(menu.Meta, utils.Rules{"Title": {utils.NotEmpty()}}); err != nil {
"Title": {utils.NotEmpty()}, response.FailWithMessage(err.Error(), c)
}
MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
if MetaVerifyErr != nil {
response.FailWithMessage(MetaVerifyErr.Error(), c)
return return
} }
err := service.AddBaseMenu(menu) if err := service.AddBaseMenu(menu); err != nil {
if err != nil {
response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c) response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
} else { } else {
response.OkWithMessage("添加成功", c) response.OkWithMessage("添加成功", c)
...@@ -101,66 +91,55 @@ func AddBaseMenu(c *gin.Context) { ...@@ -101,66 +91,55 @@ func AddBaseMenu(c *gin.Context) {
// @Summary 获取用户动态路由 // @Summary 获取用户动态路由
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @Produce application/json // @Produce application/json
// @Param data body request.RegisterAndLoginStruct true "可以什么都不填" // @Param data body request.Empty true "空"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getBaseMenuTree [post] // @Router /menu/getBaseMenuTree [post]
func GetBaseMenuTree(c *gin.Context) { func GetBaseMenuTree(c *gin.Context) {
err, menus := service.GetBaseMenuTree() if err, menus := service.GetBaseMenuTree(); err != nil {
if err != nil {
response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c) response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
} else { } else {
response.OkWithData(resp.SysBaseMenusResponse{Menus: menus}, c) response.OkWithData(resp.SysBaseMenusResponse{Menus: menus}, c)
} }
} }
// @Tags authorityAndMenu // @Tags AuthorityAndMenu
// @Summary 增加menu和角色关联关系 // @Summary 增加menu和角色关联关系
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce application/json // @Produce application/json
// @Param data body request.AddMenuAuthorityInfo true "增加menu和角色关联关系" // @Param data body request.AddMenuAuthorityInfo true "角色ID"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
// @Router /menu/addMenuAuthority [post] // @Router /menu/addMenuAuthority [post]
func AddMenuAuthority(c *gin.Context) { func AddMenuAuthority(c *gin.Context) {
var addMenuAuthorityInfo request.AddMenuAuthorityInfo var params request.AddMenuAuthorityInfo
_ = c.ShouldBindJSON(&addMenuAuthorityInfo) _ = c.ShouldBindJSON(&params)
MenuVerify := utils.Rules{ if err := utils.Verify(params, utils.Rules{"AuthorityId": {"notEmpty"}}); err != nil {
"AuthorityId": {"notEmpty"}, response.FailWithMessage(err.Error(), c)
}
MenuVerifyErr := utils.Verify(addMenuAuthorityInfo, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
return return
} }
err := service.AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId) if err := service.AddMenuAuthority(params.Menus, params.AuthorityId); err != nil {
if err != nil {
response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c) response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
} else { } else {
response.OkWithMessage("添加成功", c) response.OkWithMessage("添加成功", c)
} }
} }
// @Tags authorityAndMenu // @Tags AuthorityAndMenu
// @Summary 获取指定角色menu // @Summary 获取指定角色menu
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce 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":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /menu/GetMenuAuthority [post] // @Router /menu/GetMenuAuthority [post]
func GetMenuAuthority(c *gin.Context) { func GetMenuAuthority(c *gin.Context) {
var authorityIdInfo request.AuthorityIdInfo var param request.GetAuthorityId
_ = c.ShouldBindJSON(&authorityIdInfo) _ = c.ShouldBindJSON(&param)
MenuVerify := utils.Rules{ if err := utils.Verify(param, utils.Rules{"AuthorityId": {"notEmpty"}}); err != nil {
"AuthorityId": {"notEmpty"}, response.FailWithMessage(err.Error(), c)
}
MenuVerifyErr := utils.Verify(authorityIdInfo, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
return return
} }
err, menus := service.GetMenuAuthority(authorityIdInfo.AuthorityId) if err, menus := service.GetMenuAuthority(&param); err != nil {
if err != nil {
response.FailWithDetailed(response.ERROR, resp.SysMenusResponse{Menus: menus}, fmt.Sprintf("添加失败,%v", err), c) response.FailWithDetailed(response.ERROR, resp.SysMenusResponse{Menus: menus}, fmt.Sprintf("添加失败,%v", err), c)
} else { } else {
response.Result(response.SUCCESS, gin.H{"menus": menus}, "获取成功", c) response.Result(response.SUCCESS, gin.H{"menus": menus}, "获取成功", c)
...@@ -172,19 +151,17 @@ func GetMenuAuthority(c *gin.Context) { ...@@ -172,19 +151,17 @@ func GetMenuAuthority(c *gin.Context) {
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce application/json // @Produce application/json
// @Param data body request.GetById true "删除菜单" // @Param data body request.GetById true "菜单id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /menu/deleteBaseMenu [post] // @Router /menu/deleteBaseMenu [post]
func DeleteBaseMenu(c *gin.Context) { func DeleteBaseMenu(c *gin.Context) {
var idInfo request.GetById var param request.GetById
_ = c.ShouldBindJSON(&idInfo) _ = c.ShouldBindJSON(&param)
IdVerifyErr := utils.Verify(idInfo, utils.CustomizeMap["IdVerify"]) if err := utils.Verify(param, utils.CustomizeMap["IdVerify"]); err != nil {
if IdVerifyErr != nil { response.FailWithMessage(err.Error(), c)
response.FailWithMessage(IdVerifyErr.Error(), c)
return return
} }
err := service.DeleteBaseMenu(idInfo.Id) if err := service.DeleteBaseMenu(param.Id); err != nil {
if err != nil {
response.FailWithMessage(fmt.Sprintf("删除失败:%v", err), c) response.FailWithMessage(fmt.Sprintf("删除失败:%v", err), c)
} else { } else {
response.OkWithMessage("删除成功", c) response.OkWithMessage("删除成功", c)
...@@ -197,8 +174,8 @@ func DeleteBaseMenu(c *gin.Context) { ...@@ -197,8 +174,8 @@ func DeleteBaseMenu(c *gin.Context) {
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce application/json // @Produce application/json
// @Param data body model.SysBaseMenu true "更新菜单" // @Param data body model.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
// @Router /menu/updateBaseMenu [post] // @Router /menu/updateBaseMenu [post]
func UpdateBaseMenu(c *gin.Context) { func UpdateBaseMenu(c *gin.Context) {
var menu model.SysBaseMenu var menu model.SysBaseMenu
...@@ -210,24 +187,18 @@ func UpdateBaseMenu(c *gin.Context) { ...@@ -210,24 +187,18 @@ func UpdateBaseMenu(c *gin.Context) {
"Component": {utils.NotEmpty()}, "Component": {utils.NotEmpty()},
"Sort": {utils.Ge("0")}, "Sort": {utils.Ge("0")},
} }
MenuVerifyErr := utils.Verify(menu, MenuVerify) if err := utils.Verify(menu, MenuVerify); err != nil {
if MenuVerifyErr != nil { response.FailWithMessage(err.Error(), c)
response.FailWithMessage(MenuVerifyErr.Error(), c)
return return
} }
MetaVerify := utils.Rules{ if err := utils.Verify(menu.Meta, utils.Rules{"Title": {utils.NotEmpty()}}); err != nil {
"Title": {utils.NotEmpty()}, response.FailWithMessage(err.Error(), c)
}
MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
if MetaVerifyErr != nil {
response.FailWithMessage(MetaVerifyErr.Error(), c)
return return
} }
err := service.UpdateBaseMenu(menu) if err := service.UpdateBaseMenu(menu); err != nil {
if err != nil { response.FailWithMessage(fmt.Sprintf("更新失败:%v", err), c)
response.FailWithMessage(fmt.Sprintf("修改失败:%v", err), c)
} else { } else {
response.OkWithMessage("修改成功", c) response.OkWithMessage("更新成功", c)
} }
} }
...@@ -236,23 +207,18 @@ func UpdateBaseMenu(c *gin.Context) { ...@@ -236,23 +207,18 @@ func UpdateBaseMenu(c *gin.Context) {
// @Security ApiKeyAuth // @Security ApiKeyAuth
// @accept application/json // @accept application/json
// @Produce application/json // @Produce application/json
// @Param data body request.GetById true "根据id获取菜单" // @Param data body request.GetById true "菜单id"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}" // @Success 200 {string} string "{"success":true,"data":{},"msg":"操作成功"}"
// @Router /menu/getBaseMenuById [post] // @Router /menu/getBaseMenuById [post]
func GetBaseMenuById(c *gin.Context) { func GetBaseMenuById(c *gin.Context) {
var idInfo request.GetById var idInfo request.GetById
_ = c.ShouldBindJSON(&idInfo) _ = c.ShouldBindJSON(&idInfo)
MenuVerify := utils.Rules{ if err := utils.Verify(idInfo, utils.Rules{"Id": {"notEmpty"}}); err != nil {
"Id": {"notEmpty"}, response.FailWithMessage(err.Error(), c)
}
MenuVerifyErr := utils.Verify(idInfo, MenuVerify)
if MenuVerifyErr != nil {
response.FailWithMessage(MenuVerifyErr.Error(), c)
return return
} }
err, menu := service.GetBaseMenuById(idInfo.Id) if err, menu := service.GetBaseMenuById(idInfo.Id); err != nil {
if err != nil { response.FailWithMessage(fmt.Sprintf("获取失败:%v", err), c)
response.FailWithMessage(fmt.Sprintf("查询失败:%v", err), c)
} else { } else {
response.OkWithData(resp.SysBaseMenuResponse{Menu: menu}, c) response.OkWithData(resp.SysBaseMenuResponse{Menu: menu}, c)
} }
......
...@@ -13,4 +13,11 @@ type GetById struct { ...@@ -13,4 +13,11 @@ type GetById struct {
type IdsReq struct { type IdsReq struct {
Ids []int `json:"ids" form:"ids"` Ids []int `json:"ids" form:"ids"`
} }
\ No newline at end of file
// Get role by id structure
type GetAuthorityId struct {
AuthorityId string
}
type Empty struct {}
\ No newline at end of file
...@@ -7,8 +7,3 @@ type AddMenuAuthorityInfo struct { ...@@ -7,8 +7,3 @@ type AddMenuAuthorityInfo struct {
Menus []model.SysBaseMenu Menus []model.SysBaseMenu
AuthorityId string AuthorityId string
} }
// Get role by id structure
type AuthorityIdInfo struct {
AuthorityId string
}
...@@ -39,7 +39,7 @@ func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, autho ...@@ -39,7 +39,7 @@ func CopyAuthority(copyInfo response.SysAuthorityCopyResponse) (err error, autho
return errors.New("存在相同角色id"), authority return errors.New("存在相同角色id"), authority
} }
copyInfo.Authority.Children = []model.SysAuthority{} copyInfo.Authority.Children = []model.SysAuthority{}
err, menus := GetMenuAuthority(copyInfo.OldAuthorityId) err, menus := GetMenuAuthority(&request.GetAuthorityId{AuthorityId: copyInfo.OldAuthorityId})
var baseMenu []model.SysBaseMenu var baseMenu []model.SysBaseMenu
for _, v := range menus { for _, v := range menus {
intNum, _ := strconv.Atoi(v.MenuId) intNum, _ := strconv.Atoi(v.MenuId)
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"errors" "errors"
"gin-vue-admin/global" "gin-vue-admin/global"
"gin-vue-admin/model" "gin-vue-admin/model"
"gin-vue-admin/model/request"
"gorm.io/gorm" "gorm.io/gorm"
"strconv" "strconv"
) )
...@@ -155,9 +156,9 @@ func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error) ...@@ -155,9 +156,9 @@ func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error)
// @return err error // @return err error
// @return menus []SysBaseMenu // @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" //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 //err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
return err, menus return err, menus
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册