sys_menu.go 9.0 KB

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