sys_menu.go 6.1 KB

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