sys_menu.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/middleware"
  5. "gin-vue-admin/model"
  6. "github.com/gin-gonic/gin"
  7. )
  8. // @Tags authorityAndMenu
  9. // @Summary 获取用户动态路由
  10. // @Security ApiKeyAuth
  11. // @Produce application/json
  12. // @Param data body api.RegisterAndLoginStuct true "可以什么都不填"
  13. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  14. // @Router /menu/getMenu [post]
  15. func GetMenu(c *gin.Context) {
  16. claims, _ := c.Get("claims")
  17. waitUse := claims.(*middleware.CustomClaims)
  18. err, menus := new(model.SysMenu).GetMenuTree(waitUse.AuthorityId)
  19. if err != nil {
  20. servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{"menus": menus})
  21. } else {
  22. servers.ReportFormat(c, true, "获取成功", gin.H{"menus": menus})
  23. }
  24. }
  25. // @Tags menu
  26. // @Summary 分页获取基础menu列表
  27. // @Security ApiKeyAuth
  28. // @accept application/json
  29. // @Produce application/json
  30. // @Param data body model.PageInfo true "分页获取基础menu列表"
  31. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  32. // @Router /menu/getMenuList [post]
  33. func GetMenuList(c *gin.Context) {
  34. var pageInfo model.PageInfo
  35. _ = c.ShouldBindJSON(&pageInfo)
  36. err, menuList, total := new(model.SysBaseMenu).GetInfoList(pageInfo)
  37. if err != nil {
  38. servers.ReportFormat(c, false, fmt.Sprintf("获取数据失败,%v", err), gin.H{})
  39. } else {
  40. servers.ReportFormat(c, true, "获取数据成功", gin.H{
  41. "list": menuList,
  42. "total": total,
  43. "page": pageInfo.Page,
  44. "pageSize": pageInfo.PageSize,
  45. })
  46. }
  47. }
  48. // @Tags menu
  49. // @Summary 新增菜单
  50. // @Security ApiKeyAuth
  51. // @accept application/json
  52. // @Produce application/json
  53. // @Param data body sysModel.SysBaseMenu true "新增菜单"
  54. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  55. // @Router /menu/addBaseMenu [post]
  56. func AddBaseMenu(c *gin.Context) {
  57. var addMenu model.SysBaseMenu
  58. _ = c.ShouldBindJSON(&addMenu)
  59. err := addMenu.AddBaseMenu()
  60. if err != nil {
  61. servers.ReportFormat(c, false, fmt.Sprintf("添加失败,%v", err), gin.H{})
  62. } else {
  63. servers.ReportFormat(c, true, fmt.Sprintf("添加成功,%v", err), gin.H{})
  64. }
  65. }
  66. // @Tags authorityAndMenu
  67. // @Summary 获取用户动态路由
  68. // @Security ApiKeyAuth
  69. // @Produce application/json
  70. // @Param data body api.RegisterAndLoginStuct true "可以什么都不填"
  71. // @Success 200 {string} string "{"success":true,"data":{},"msg":"返回成功"}"
  72. // @Router /menu/getBaseMenuTree [post]
  73. func GetBaseMenuTree(c *gin.Context) {
  74. err, menus := new(model.SysBaseMenu).GetBaseMenuTree()
  75. if err != nil {
  76. servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{"menus": menus})
  77. } else {
  78. servers.ReportFormat(c, true, "获取成功", gin.H{"menus": menus})
  79. }
  80. }
  81. type AddMenuAuthorityInfo struct {
  82. Menus []model.SysBaseMenu
  83. AuthorityId string
  84. }
  85. // @Tags authorityAndMenu
  86. // @Summary 增加menu和角色关联关系
  87. // @Security ApiKeyAuth
  88. // @accept application/json
  89. // @Produce application/json
  90. // @Param data body api.AddMenuAuthorityInfo true "增加menu和角色关联关系"
  91. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  92. // @Router /menu/addMenuAuthority [post]
  93. func AddMenuAuthority(c *gin.Context) {
  94. var addMenuAuthorityInfo AddMenuAuthorityInfo
  95. _ = c.ShouldBindJSON(&addMenuAuthorityInfo)
  96. err := new(model.SysMenu).AddMenuAuthority(addMenuAuthorityInfo.Menus, addMenuAuthorityInfo.AuthorityId)
  97. if err != nil {
  98. servers.ReportFormat(c, false, fmt.Sprintf("添加失败,%v", err), gin.H{})
  99. } else {
  100. servers.ReportFormat(c, true, fmt.Sprintf("添加成功,%v", err), gin.H{})
  101. }
  102. }
  103. type AuthorityIdInfo struct {
  104. AuthorityId string
  105. }
  106. // @Tags authorityAndMenu
  107. // @Summary 获取指定角色menu
  108. // @Security ApiKeyAuth
  109. // @accept application/json
  110. // @Produce application/json
  111. // @Param data body api.AuthorityIdInfo true "增加menu和角色关联关系"
  112. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  113. // @Router /menu/addMenuAuthority [post]
  114. func GetMenuAuthority(c *gin.Context) {
  115. var authorityIdInfo AuthorityIdInfo
  116. _ = c.ShouldBindJSON(&authorityIdInfo)
  117. err, menus := new(model.SysMenu).GetMenuAuthority(authorityIdInfo.AuthorityId)
  118. if err != nil {
  119. servers.ReportFormat(c, false, fmt.Sprintf("获取失败:%v", err), gin.H{"menus": menus})
  120. } else {
  121. servers.ReportFormat(c, true, "获取成功", gin.H{"menus": menus})
  122. }
  123. }
  124. type IdInfo struct {
  125. Id float64
  126. }
  127. // @Tags menu
  128. // @Summary 删除菜单
  129. // @Security ApiKeyAuth
  130. // @accept application/json
  131. // @Produce application/json
  132. // @Param data body api.IdInfo true "删除菜单"
  133. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  134. // @Router /menu/deleteBaseMenu [post]
  135. func DeleteBaseMenu(c *gin.Context) {
  136. var idInfo IdInfo
  137. _ = c.ShouldBindJSON(&idInfo)
  138. err := new(model.SysBaseMenu).DeleteBaseMenu(idInfo.Id)
  139. if err != nil {
  140. servers.ReportFormat(c, false, fmt.Sprintf("删除失败:%v", err), gin.H{})
  141. } else {
  142. servers.ReportFormat(c, true, "删除成功", gin.H{})
  143. }
  144. }
  145. // @Tags menu
  146. // @Summary 更新菜单
  147. // @Security ApiKeyAuth
  148. // @accept application/json
  149. // @Produce application/json
  150. // @Param data body sysModel.SysBaseMenu true "更新菜单"
  151. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  152. // @Router /menu/updateBaseMenu [post]
  153. func UpdateBaseMenu(c *gin.Context) {
  154. var menu model.SysBaseMenu
  155. _ = c.ShouldBindJSON(&menu)
  156. err := menu.UpdateBaseMenu()
  157. if err != nil {
  158. servers.ReportFormat(c, false, fmt.Sprintf("修改失败:%v", err), gin.H{})
  159. } else {
  160. servers.ReportFormat(c, true, "修改成功", gin.H{})
  161. }
  162. }
  163. type GetById struct {
  164. Id float64 `json:"id"`
  165. }
  166. // @Tags menu
  167. // @Summary 根据id获取菜单
  168. // @Security ApiKeyAuth
  169. // @accept application/json
  170. // @Produce application/json
  171. // @Param data body api.GetById true "根据id获取菜单"
  172. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  173. // @Router /menu/getBaseMenuById [post]
  174. func GetBaseMenuById(c *gin.Context) {
  175. var idInfo GetById
  176. _ = c.ShouldBindJSON(&idInfo)
  177. err, menu := new(model.SysBaseMenu).GetBaseMenuById(idInfo.Id)
  178. if err != nil {
  179. servers.ReportFormat(c, false, fmt.Sprintf("查询失败:%v", err), gin.H{})
  180. } else {
  181. servers.ReportFormat(c, true, "查询成功", gin.H{"menu": menu})
  182. }
  183. }