menu.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "iot_manager_service/app/middleware"
  5. "iot_manager_service/app/system/model"
  6. "iot_manager_service/app/system/service"
  7. "iot_manager_service/util/common"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. )
  12. var Menu = new(menu)
  13. type menu struct{}
  14. func (c *menu) GetDetail(ctx *gin.Context) {
  15. id := ctx.Query("id")
  16. iId, err := strconv.Atoi(id)
  17. if err != nil {
  18. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  19. return
  20. }
  21. detail, err := service.MenuService.Get(iId)
  22. if err != nil {
  23. ctx.JSON(http.StatusOK, err)
  24. return
  25. }
  26. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, detail))
  27. }
  28. func (c *menu) List(ctx *gin.Context) {
  29. }
  30. func (c *menu) LazyList(ctx *gin.Context) {
  31. id := ctx.Query("parentId")
  32. name := ctx.Query("name")
  33. code := ctx.Query("code")
  34. alias := ctx.Query("alias")
  35. parentId := -1
  36. if id != "" {
  37. parentId, _ = strconv.Atoi(id)
  38. }
  39. menus, err := service.MenuService.LazyList(parentId, name, code, alias)
  40. if err != nil {
  41. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  42. return
  43. }
  44. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus))
  45. }
  46. func (c *menu) MenuList(ctx *gin.Context) {
  47. }
  48. func (c *menu) LazyMenuList(ctx *gin.Context) {
  49. }
  50. func (c *menu) Submit(ctx *gin.Context) {
  51. var req model.MenuSubmit
  52. if err := ctx.ShouldBindJSON(&req); err != nil {
  53. ctx.JSON(http.StatusOK, common.ParamsInvalidResponse(err.Error(), nil))
  54. return
  55. }
  56. err := service.MenuService.Submit(req)
  57. ctx.JSON(http.StatusOK, err)
  58. }
  59. func (c *menu) Remove(ctx *gin.Context) {
  60. }
  61. func (c *menu) Routes(ctx *gin.Context) {
  62. value, _ := ctx.Get(middleware.Authorization)
  63. claims := value.(*middleware.Claims)
  64. menus, err := service.MenuService.Routes(claims.RoleId)
  65. if err != nil {
  66. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  67. return
  68. }
  69. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus))
  70. }
  71. func (c *menu) RoutesExt(cxt *gin.Context) {
  72. }
  73. // 由于子菜单 没有分的很详细,这里直接先让他们全部有这个权限 2022-12-07 dsx
  74. func (c *menu) Buttons(ctx *gin.Context) {
  75. value, _ := ctx.Get(middleware.Authorization)
  76. claims := value.(*middleware.Claims)
  77. menus, err := service.MenuService.Routes(claims.RoleId)
  78. if err != nil {
  79. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  80. return
  81. }
  82. var menus2 []model.MenuRouteDetail
  83. subMenus := make(map[string]string)
  84. subMenus["add"] = "新增"
  85. subMenus["edit"] = "修改"
  86. subMenus["view"] = "查看"
  87. subMenus["delete"] = "删除"
  88. for _, menu := range menus {
  89. if menu.Children != nil {
  90. for _, child := range menu.Children {
  91. var menus3 []model.MenuRouteDetail
  92. i := 1
  93. for k, v := range subMenus {
  94. var menu4 model.MenuRouteDetail
  95. menu4.ID = child.ID + i
  96. menu4.ParentId = child.ID
  97. child.Path = strings.ReplaceAll(child.Path, "-", "")
  98. subPaths := strings.Split(child.Path, "/")
  99. subPath := subPaths[len(subPaths)-1]
  100. menu4.Code = subPath + "_" + k
  101. menu4.Name = v
  102. menu4.Path = child.Path + "/" + k
  103. menu4.Source = k
  104. menus3 = append(menus3, menu4)
  105. i++
  106. }
  107. child.Children = menus3
  108. menus2 = append(menus2, child)
  109. }
  110. }
  111. }
  112. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus2))
  113. }
  114. func (c *menu) Tree(ctx *gin.Context) {
  115. menus, err := service.MenuService.Tree()
  116. if err != nil {
  117. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  118. return
  119. }
  120. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, menus))
  121. }
  122. func (c *menu) GrantTree(ctx *gin.Context) {
  123. //菜单权限
  124. tree, err := service.MenuService.GetTree()
  125. if err != nil {
  126. ctx.JSON(http.StatusOK, common.FailResponse(err.Error(), nil))
  127. return
  128. }
  129. rsp := model.ResGrantTree{
  130. Menu: tree,
  131. DataScope: nil,
  132. ApiScope: nil,
  133. }
  134. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
  135. //数据权限
  136. //接口权限
  137. }
  138. func (c *menu) RoleTreeKeys(ctx *gin.Context) {
  139. roleIds, _ := strconv.Atoi(ctx.Query("roleIds"))
  140. menuIds := service.RoleMenuService.GetMenuIdByRoleId(roleIds)
  141. rsp := model.ResMenuIds{
  142. Menu: menuIds,
  143. DataScope: nil,
  144. ApiScope: nil,
  145. }
  146. ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
  147. }
  148. func (c *menu) GrantTopTree(ctx *gin.Context) {
  149. }
  150. func (c *menu) TopTreeKeys(ctx *gin.Context) {
  151. }
  152. func (c *menu) TopMenu(ctx *gin.Context) {
  153. }
  154. func (c *menu) authRoutes(ctx *gin.Context) {
  155. }