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.

201 lines
6.3 KiB

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