sys_menu.go 6.1 KB

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