You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
7.2 KiB

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