sys_menu.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. resp "gin-vue-admin/model/response"
  8. "gin-vue-admin/service"
  9. "github.com/gin-gonic/gin"
  10. )
  11. // @Tags authorityAndMenu
  12. // @Summary 获取用户动态路由
  13. // @Security ApiKeyAuth
  14. // @Produce application/json
  15. // @Param data body api.RegisterAndLoginStruct true "可以什么都不填"
  16. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  17. // @Router /menu/getMenu [post]
  18. func GetMenu(c *gin.Context) {
  19. claims, _ := c.Get("claims")
  20. waitUse := claims.(*request.CustomClaims)
  21. err, menus := service.GetMenuTree(waitUse.AuthorityId)
  22. if err != nil {
  23. response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
  24. } else {
  25. response.OkWithData(resp.SysMenusResponse{Menus: menus}, c)
  26. }
  27. }
  28. // @Tags menu
  29. // @Summary 分页获取基础menu列表
  30. // @Security ApiKeyAuth
  31. // @accept application/json
  32. // @Produce application/json
  33. // @Param data body model.PageInfo true "分页获取基础menu列表"
  34. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  35. // @Router /menu/getMenuList [post]
  36. func GetMenuList(c *gin.Context) {
  37. var pageInfo request.PageInfo
  38. _ = c.ShouldBindJSON(&pageInfo)
  39. err, menuList, total := service.GetInfoList(pageInfo)
  40. if err != nil {
  41. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  42. } else {
  43. response.OkWithData(resp.PageResult{
  44. List: menuList,
  45. Total: total,
  46. Page: pageInfo.Page,
  47. PageSize: pageInfo.PageSize,
  48. }, c)
  49. }
  50. }
  51. // @Tags menu
  52. // @Summary 新增菜单
  53. // @Security ApiKeyAuth
  54. // @accept application/json
  55. // @Produce application/json
  56. // @Param data body model.SysBaseMenu true "新增菜单"
  57. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  58. // @Router /menu/addBaseMenu [post]
  59. func AddBaseMenu(c *gin.Context) {
  60. var menu model.SysBaseMenu
  61. _ = c.ShouldBindJSON(&menu)
  62. err := service.AddBaseMenu(menu)
  63. if err != nil {
  64. response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
  65. } else {
  66. response.OkWithMessage("添加成功", c)
  67. }
  68. }
  69. // @Tags authorityAndMenu
  70. // @Summary 获取用户动态路由
  71. // @Security ApiKeyAuth
  72. // @Produce application/json
  73. // @Param data body api.RegisterAndLoginStruct true "可以什么都不填"
  74. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  75. // @Router /menu/getBaseMenuTree [post]
  76. func GetBaseMenuTree(c *gin.Context) {
  77. err, menus := service.GetBaseMenuTree()
  78. if err != nil {
  79. response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
  80. } else {
  81. response.OkWithData(resp.SysBaseMenusResponse{Menus: menus}, c)
  82. }
  83. }
  84. // @Tags authorityAndMenu
  85. // @Summary 增加menu和角色关联关系
  86. // @Security ApiKeyAuth
  87. // @accept application/json
  88. // @Produce application/json
  89. // @Param data body model.AddMenuAuthorityInfo true "增加menu和角色关联关系"
  90. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  91. // @Router /menu/addMenuAuthority [post]
  92. func AddMenuAuthority(c *gin.Context) {
  93. var addMenuAuthorityInfo request.AddMenuAuthorityInfo
  94. _ = c.ShouldBindJSON(&addMenuAuthorityInfo)
  95. err := service.AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId)
  96. if err != nil {
  97. response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
  98. } else {
  99. response.OkWithMessage("添加成功", c)
  100. }
  101. }
  102. // @Tags authorityAndMenu
  103. // @Summary 获取指定角色menu
  104. // @Security ApiKeyAuth
  105. // @accept application/json
  106. // @Produce application/json
  107. // @Param data body model.AuthorityIdInfo true "增加menu和角色关联关系"
  108. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  109. // @Router /menu/GetMenuAuthority [post]
  110. func GetMenuAuthority(c *gin.Context) {
  111. var authorityIdInfo request.AuthorityIdInfo
  112. _ = c.ShouldBindJSON(&authorityIdInfo)
  113. err, menus := service.GetMenuAuthority(authorityIdInfo.AuthorityId)
  114. if err != nil {
  115. response.FailWithDetailed(response.ERROR, resp.SysMenusResponse{Menus: menus}, fmt.Sprintf("添加失败,%v", err), c)
  116. } else {
  117. response.Result(response.SUCCESS, gin.H{"menus": menus}, "获取成功", c)
  118. }
  119. }
  120. // @Tags menu
  121. // @Summary 删除菜单
  122. // @Security ApiKeyAuth
  123. // @accept application/json
  124. // @Produce application/json
  125. // @Param data body model.GetById true "删除菜单"
  126. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  127. // @Router /menu/deleteBaseMenu [post]
  128. func DeleteBaseMenu(c *gin.Context) {
  129. var idInfo request.GetById
  130. _ = c.ShouldBindJSON(&idInfo)
  131. err := service.DeleteBaseMenu(idInfo.Id)
  132. if err != nil {
  133. response.FailWithMessage(fmt.Sprintf("删除失败:%v", err), c)
  134. } else {
  135. response.OkWithMessage("删除成功", c)
  136. }
  137. }
  138. // @Tags menu
  139. // @Summary 更新菜单
  140. // @Security ApiKeyAuth
  141. // @accept application/json
  142. // @Produce application/json
  143. // @Param data body model.SysBaseMenu true "更新菜单"
  144. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  145. // @Router /menu/updateBaseMenu [post]
  146. func UpdateBaseMenu(c *gin.Context) {
  147. var menu model.SysBaseMenu
  148. _ = c.ShouldBindJSON(&menu)
  149. err := service.UpdateBaseMenu(menu)
  150. if err != nil {
  151. response.FailWithMessage(fmt.Sprintf("修改失败:%v", err), c)
  152. } else {
  153. response.OkWithMessage("修改成功", c)
  154. }
  155. }
  156. // @Tags menu
  157. // @Summary 根据id获取菜单
  158. // @Security ApiKeyAuth
  159. // @accept application/json
  160. // @Produce application/json
  161. // @Param data body model.GetById true "根据id获取菜单"
  162. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  163. // @Router /menu/getBaseMenuById [post]
  164. func GetBaseMenuById(c *gin.Context) {
  165. var idInfo request.GetById
  166. _ = c.ShouldBindJSON(&idInfo)
  167. err, menu := service.GetBaseMenuById(idInfo.Id)
  168. if err != nil {
  169. response.FailWithMessage(fmt.Sprintf("查询失败:%v", err), c)
  170. } else {
  171. response.OkWithData(resp.SysBaseMenuResponse{Menu: menu}, c)
  172. }
  173. }