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.

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