sys_menu.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. err, menuList, total := service.GetInfoList()
  41. if err != nil {
  42. response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
  43. } else {
  44. response.OkWithData(resp.PageResult{
  45. List: menuList,
  46. Total: total,
  47. Page: pageInfo.Page,
  48. PageSize: pageInfo.PageSize,
  49. }, c)
  50. }
  51. }
  52. // @Tags menu
  53. // @Summary 新增菜单
  54. // @Security ApiKeyAuth
  55. // @accept application/json
  56. // @Produce application/json
  57. // @Param data body model.SysBaseMenu true "新增菜单"
  58. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  59. // @Router /menu/addBaseMenu [post]
  60. func AddBaseMenu(c *gin.Context) {
  61. var menu model.SysBaseMenu
  62. _ = c.ShouldBindJSON(&menu)
  63. MenuVerify := utils.Rules{
  64. "Path": {utils.NotEmpty()},
  65. "ParentId": {utils.NotEmpty()},
  66. "Name": {utils.NotEmpty()},
  67. "Component": {utils.NotEmpty()},
  68. "Sort": {utils.Ge("0")},
  69. }
  70. MenuVerifyErr := utils.Verify(menu, MenuVerify)
  71. if MenuVerifyErr != nil {
  72. response.FailWithMessage(MenuVerifyErr.Error(), c)
  73. return
  74. }
  75. MetaVerify := utils.Rules{
  76. "Title": {utils.NotEmpty()},
  77. }
  78. MetaVerifyErr := utils.Verify(menu.Meta, MetaVerify)
  79. if MetaVerifyErr != nil {
  80. response.FailWithMessage(MetaVerifyErr.Error(), c)
  81. return
  82. }
  83. err := service.AddBaseMenu(menu)
  84. if err != nil {
  85. response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
  86. } else {
  87. response.OkWithMessage("添加成功", c)
  88. }
  89. }
  90. // @Tags authorityAndMenu
  91. // @Summary 获取用户动态路由
  92. // @Security ApiKeyAuth
  93. // @Produce application/json
  94. // @Param data body request.RegisterAndLoginStruct true "可以什么都不填"
  95. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  96. // @Router /menu/getBaseMenuTree [post]
  97. func GetBaseMenuTree(c *gin.Context) {
  98. err, menus := service.GetBaseMenuTree()
  99. if err != nil {
  100. response.FailWithMessage(fmt.Sprintf("获取失败,%v", err), c)
  101. } else {
  102. response.OkWithData(resp.SysBaseMenusResponse{Menus: menus}, c)
  103. }
  104. }
  105. // @Tags authorityAndMenu
  106. // @Summary 增加menu和角色关联关系
  107. // @Security ApiKeyAuth
  108. // @accept application/json
  109. // @Produce application/json
  110. // @Param data body request.AddMenuAuthorityInfo true "增加menu和角色关联关系"
  111. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  112. // @Router /menu/addMenuAuthority [post]
  113. func AddMenuAuthority(c *gin.Context) {
  114. var addMenuAuthorityInfo request.AddMenuAuthorityInfo
  115. _ = c.ShouldBindJSON(&addMenuAuthorityInfo)
  116. err := service.AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId)
  117. if err != nil {
  118. response.FailWithMessage(fmt.Sprintf("添加失败,%v", err), c)
  119. } else {
  120. response.OkWithMessage("添加成功", c)
  121. }
  122. }
  123. // @Tags authorityAndMenu
  124. // @Summary 获取指定角色menu
  125. // @Security ApiKeyAuth
  126. // @accept application/json
  127. // @Produce application/json
  128. // @Param data body request.AuthorityIdInfo true "增加menu和角色关联关系"
  129. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  130. // @Router /menu/GetMenuAuthority [post]
  131. func GetMenuAuthority(c *gin.Context) {
  132. var authorityIdInfo request.AuthorityIdInfo
  133. _ = c.ShouldBindJSON(&authorityIdInfo)
  134. err, menus := service.GetMenuAuthority(authorityIdInfo.AuthorityId)
  135. if err != nil {
  136. response.FailWithDetailed(response.ERROR, resp.SysMenusResponse{Menus: menus}, fmt.Sprintf("添加失败,%v", err), c)
  137. } else {
  138. response.Result(response.SUCCESS, gin.H{"menus": menus}, "获取成功", c)
  139. }
  140. }
  141. // @Tags menu
  142. // @Summary 删除菜单
  143. // @Security ApiKeyAuth
  144. // @accept application/json
  145. // @Produce application/json
  146. // @Param data body request.GetById true "删除菜单"
  147. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  148. // @Router /menu/deleteBaseMenu [post]
  149. func DeleteBaseMenu(c *gin.Context) {
  150. var idInfo request.GetById
  151. _ = c.ShouldBindJSON(&idInfo)
  152. err := service.DeleteBaseMenu(idInfo.Id)
  153. if err != nil {
  154. response.FailWithMessage(fmt.Sprintf("删除失败:%v", err), c)
  155. } else {
  156. response.OkWithMessage("删除成功", c)
  157. }
  158. }
  159. // @Tags menu
  160. // @Summary 更新菜单
  161. // @Security ApiKeyAuth
  162. // @accept application/json
  163. // @Produce application/json
  164. // @Param data body model.SysBaseMenu true "更新菜单"
  165. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  166. // @Router /menu/updateBaseMenu [post]
  167. func UpdateBaseMenu(c *gin.Context) {
  168. var menu model.SysBaseMenu
  169. _ = c.ShouldBindJSON(&menu)
  170. err := service.UpdateBaseMenu(menu)
  171. if err != nil {
  172. response.FailWithMessage(fmt.Sprintf("修改失败:%v", err), c)
  173. } else {
  174. response.OkWithMessage("修改成功", c)
  175. }
  176. }
  177. // @Tags menu
  178. // @Summary 根据id获取菜单
  179. // @Security ApiKeyAuth
  180. // @accept application/json
  181. // @Produce application/json
  182. // @Param data body request.GetById true "根据id获取菜单"
  183. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  184. // @Router /menu/getBaseMenuById [post]
  185. func GetBaseMenuById(c *gin.Context) {
  186. var idInfo request.GetById
  187. _ = c.ShouldBindJSON(&idInfo)
  188. err, menu := service.GetBaseMenuById(idInfo.Id)
  189. if err != nil {
  190. response.FailWithMessage(fmt.Sprintf("查询失败:%v", err), c)
  191. } else {
  192. response.OkWithData(resp.SysBaseMenuResponse{Menu: menu}, c)
  193. }
  194. }