sys_menu.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  6. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  7. systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  8. systemRes "github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
  9. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  10. "github.com/gin-gonic/gin"
  11. "go.uber.org/zap"
  12. )
  13. type AuthorityMenuApi struct {
  14. }
  15. // @Tags AuthorityMenu
  16. // @Summary 获取用户动态路由
  17. // @Security ApiKeyAuth
  18. // @Produce application/json
  19. // @Param data body request.Empty true "空"
  20. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  21. // @Router /menu/getMenu [post]
  22. func (a *AuthorityMenuApi) GetMenu(c *gin.Context) {
  23. if err, menus := menuService.GetMenuTree(utils.GetUserAuthorityId(c)); err != nil {
  24. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  25. response.FailWithMessage("获取失败", c)
  26. } else {
  27. if menus == nil {
  28. menus = []system.SysMenu{}
  29. }
  30. response.OkWithDetailed(systemRes.SysMenusResponse{Menus: menus}, "获取成功", c)
  31. }
  32. }
  33. // @Tags AuthorityMenu
  34. // @Summary 获取用户动态路由
  35. // @Security ApiKeyAuth
  36. // @Produce application/json
  37. // @Param data body request.Empty true "空"
  38. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  39. // @Router /menu/getBaseMenuTree [post]
  40. func (a *AuthorityMenuApi) GetBaseMenuTree(c *gin.Context) {
  41. if err, menus := menuService.GetBaseMenuTree(); err != nil {
  42. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  43. response.FailWithMessage("获取失败", c)
  44. } else {
  45. response.OkWithDetailed(systemRes.SysBaseMenusResponse{Menus: menus}, "获取成功", c)
  46. }
  47. }
  48. // @Tags AuthorityMenu
  49. // @Summary 增加menu和角色关联关系
  50. // @Security ApiKeyAuth
  51. // @accept application/json
  52. // @Produce application/json
  53. // @Param data body systemReq.AddMenuAuthorityInfo true "角色ID"
  54. // @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
  55. // @Router /menu/addMenuAuthority [post]
  56. func (a *AuthorityMenuApi) AddMenuAuthority(c *gin.Context) {
  57. var authorityMenu systemReq.AddMenuAuthorityInfo
  58. _ = c.ShouldBindJSON(&authorityMenu)
  59. if err := utils.Verify(authorityMenu, utils.AuthorityIdVerify); err != nil {
  60. response.FailWithMessage(err.Error(), c)
  61. return
  62. }
  63. if err := menuService.AddMenuAuthority(authorityMenu.Menus, authorityMenu.AuthorityId); err != nil {
  64. global.GVA_LOG.Error("添加失败!", zap.Any("err", err))
  65. response.FailWithMessage("添加失败", c)
  66. } else {
  67. response.OkWithMessage("添加成功", c)
  68. }
  69. }
  70. // @Tags AuthorityMenu
  71. // @Summary 获取指定角色menu
  72. // @Security ApiKeyAuth
  73. // @accept application/json
  74. // @Produce application/json
  75. // @Param data body request.GetAuthorityId true "角色ID"
  76. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  77. // @Router /menu/getMenuAuthority [post]
  78. func (a *AuthorityMenuApi) GetMenuAuthority(c *gin.Context) {
  79. var param request.GetAuthorityId
  80. _ = c.ShouldBindJSON(&param)
  81. if err := utils.Verify(param, utils.AuthorityIdVerify); err != nil {
  82. response.FailWithMessage(err.Error(), c)
  83. return
  84. }
  85. if err, menus := menuService.GetMenuAuthority(&param); err != nil {
  86. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  87. response.FailWithDetailed(systemRes.SysMenusResponse{Menus: menus}, "获取失败", c)
  88. } else {
  89. response.OkWithDetailed(gin.H{"menus": menus}, "获取成功", c)
  90. }
  91. }
  92. // @Tags Menu
  93. // @Summary 新增菜单
  94. // @Security ApiKeyAuth
  95. // @accept application/json
  96. // @Produce application/json
  97. // @Param data body system.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
  98. // @Success 200 {string} string "{"success":true,"data":{},"msg":"添加成功"}"
  99. // @Router /menu/addBaseMenu [post]
  100. func (a *AuthorityMenuApi) AddBaseMenu(c *gin.Context) {
  101. var menu system.SysBaseMenu
  102. _ = c.ShouldBindJSON(&menu)
  103. if err := utils.Verify(menu, utils.MenuVerify); err != nil {
  104. response.FailWithMessage(err.Error(), c)
  105. return
  106. }
  107. if err := utils.Verify(menu.Meta, utils.MenuMetaVerify); err != nil {
  108. response.FailWithMessage(err.Error(), c)
  109. return
  110. }
  111. if err := menuService.AddBaseMenu(menu); err != nil {
  112. global.GVA_LOG.Error("添加失败!", zap.Any("err", err))
  113. response.FailWithMessage("添加失败", c)
  114. } else {
  115. response.OkWithMessage("添加成功", c)
  116. }
  117. }
  118. // @Tags Menu
  119. // @Summary 删除菜单
  120. // @Security ApiKeyAuth
  121. // @accept application/json
  122. // @Produce application/json
  123. // @Param data body request.GetById true "菜单id"
  124. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  125. // @Router /menu/deleteBaseMenu [post]
  126. func (a *AuthorityMenuApi) DeleteBaseMenu(c *gin.Context) {
  127. var menu request.GetById
  128. _ = c.ShouldBindJSON(&menu)
  129. if err := utils.Verify(menu, utils.IdVerify); err != nil {
  130. response.FailWithMessage(err.Error(), c)
  131. return
  132. }
  133. if err := baseMenuService.DeleteBaseMenu(menu.ID); err != nil {
  134. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  135. response.FailWithMessage("删除失败", c)
  136. } else {
  137. response.OkWithMessage("删除成功", c)
  138. }
  139. }
  140. // @Tags Menu
  141. // @Summary 更新菜单
  142. // @Security ApiKeyAuth
  143. // @accept application/json
  144. // @Produce application/json
  145. // @Param data body system.SysBaseMenu true "路由path, 父菜单ID, 路由name, 对应前端文件路径, 排序标记"
  146. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  147. // @Router /menu/updateBaseMenu [post]
  148. func (a *AuthorityMenuApi) UpdateBaseMenu(c *gin.Context) {
  149. var menu system.SysBaseMenu
  150. _ = c.ShouldBindJSON(&menu)
  151. if err := utils.Verify(menu, utils.MenuVerify); err != nil {
  152. response.FailWithMessage(err.Error(), c)
  153. return
  154. }
  155. if err := utils.Verify(menu.Meta, utils.MenuMetaVerify); err != nil {
  156. response.FailWithMessage(err.Error(), c)
  157. return
  158. }
  159. if err := baseMenuService.UpdateBaseMenu(menu); err != nil {
  160. global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
  161. response.FailWithMessage("更新失败", c)
  162. } else {
  163. response.OkWithMessage("更新成功", c)
  164. }
  165. }
  166. // @Tags Menu
  167. // @Summary 根据id获取菜单
  168. // @Security ApiKeyAuth
  169. // @accept application/json
  170. // @Produce application/json
  171. // @Param data body request.GetById true "菜单id"
  172. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  173. // @Router /menu/getBaseMenuById [post]
  174. func (a *AuthorityMenuApi) GetBaseMenuById(c *gin.Context) {
  175. var idInfo request.GetById
  176. _ = c.ShouldBindJSON(&idInfo)
  177. if err := utils.Verify(idInfo, utils.IdVerify); err != nil {
  178. response.FailWithMessage(err.Error(), c)
  179. return
  180. }
  181. if err, menu := baseMenuService.GetBaseMenuById(idInfo.ID); err != nil {
  182. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  183. response.FailWithMessage("获取失败", c)
  184. } else {
  185. response.OkWithDetailed(systemRes.SysBaseMenuResponse{Menu: menu}, "获取成功", c)
  186. }
  187. }
  188. // @Tags Menu
  189. // @Summary 分页获取基础menu列表
  190. // @Security ApiKeyAuth
  191. // @accept application/json
  192. // @Produce application/json
  193. // @Param data body request.PageInfo true "页码, 每页大小"
  194. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  195. // @Router /menu/getMenuList [post]
  196. func (a *AuthorityMenuApi) GetMenuList(c *gin.Context) {
  197. var pageInfo request.PageInfo
  198. _ = c.ShouldBindJSON(&pageInfo)
  199. if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
  200. response.FailWithMessage(err.Error(), c)
  201. return
  202. }
  203. if err, menuList, total := menuService.GetInfoList(); err != nil {
  204. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  205. response.FailWithMessage("获取失败", c)
  206. } else {
  207. response.OkWithDetailed(response.PageResult{
  208. List: menuList,
  209. Total: total,
  210. Page: pageInfo.Page,
  211. PageSize: pageInfo.PageSize,
  212. }, "获取成功", c)
  213. }
  214. }