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.

226 lines
7.8 KiB

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