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.

199 lines
6.3 KiB

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