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.

225 lines
7.3 KiB

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