sys_apis.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package system
  2. import (
  3. "github.com/demdxx/gocast"
  4. "github.com/gin-gonic/gin"
  5. "go_server/model/common/response"
  6. system2 "go_server/model/system"
  7. "go_server/service/base"
  8. "strings"
  9. )
  10. type ApisService struct {
  11. base.SysCommonService
  12. }
  13. func (s *ApisService) Tree(c *gin.Context) {
  14. roleId := c.GetInt64("roleId")
  15. sysApis, _ := system2.NewApis().SysTree(s.DB())
  16. role, ok := base.GetOne[system2.Role](s.DB(), "id", roleId)
  17. if !ok {
  18. }
  19. personApis := sysApis
  20. if role.Apis != "*" {
  21. apisSplit := strings.Split(role.Apis, ",")
  22. personApis, _ = system2.NewApis().GetUserTree(s.DB(), apisSplit)
  23. }
  24. res := make(map[string]interface{})
  25. res["sysApis"] = sysApis // 系统总树
  26. res["personApis"] = personApis // 当前角色权限[]
  27. response.Resp(c, res)
  28. return
  29. }
  30. // 设置
  31. func (s *ApisService) Set(c *gin.Context) {
  32. userId := c.GetInt64("userId")
  33. // 限制用户增加必须管理员才可以操作
  34. if userId != system2.AdminId {
  35. response.Resp(c, "不允许操作")
  36. return
  37. }
  38. type request struct {
  39. Id interface{} `json:"id" validate:"required"`
  40. ParentId interface{} `json:"parentId"`
  41. Group string `json:"group" gorm:"column:group;type:varchar(50);comment:分组名称"`
  42. Name string `json:"name" gorm:"column:name;type:varchar(50);comment:接口名称"`
  43. }
  44. req := new(request)
  45. if err := c.BindJSON(req); err != nil {
  46. response.Resp(c, err.Error())
  47. return
  48. }
  49. if req.Id == 0 {
  50. response.Resp(c, "API不存在")
  51. return
  52. }
  53. menu, ok := base.GetOne[system2.Apis](s.DB(), "id", req.Id)
  54. if !ok {
  55. response.Resp(c, "API不存在")
  56. return
  57. }
  58. parentId := gocast.ToInt64(req.ParentId)
  59. if parentId != 0 {
  60. _, ok := base.GetOne[system2.Apis](s.DB(), "id", parentId)
  61. if !ok {
  62. response.Resp(c, "上级API不存在")
  63. return
  64. }
  65. }
  66. if req.Group != "" {
  67. menu.Group = req.Group
  68. }
  69. if req.Name != "" {
  70. menu.Name = req.Name
  71. }
  72. if parentId != menu.ParentId {
  73. menu.ParentId = parentId
  74. }
  75. if err := s.DB().Model(&system2.Apis{}).
  76. Where("id", req.Id).
  77. Updates(system2.Apis{
  78. ParentId: menu.ParentId,
  79. Name: menu.Name,
  80. Group: menu.Group,
  81. }).Error; err != nil {
  82. response.Resp(c, response.ResponseCodeFailure)
  83. return
  84. }
  85. response.Resp(c)
  86. return
  87. }
  88. // 创建分组
  89. func (s *ApisService) Create(c *gin.Context) {
  90. userId := c.GetInt64("userId")
  91. // 限制用户增加必须管理员才可以操作
  92. if userId != system2.AdminId {
  93. response.Resp(c, "不允许操作")
  94. return
  95. }
  96. type request struct {
  97. ParentId interface{} `json:"parentId"`
  98. Group string `json:"group" gorm:"column:group;type:varchar(50);comment:分组名称"`
  99. Name string `json:"name" gorm:"column:name;type:varchar(50);comment:接口名称"`
  100. Path string `json:"path" gorm:"column:path;type:varchar(50);comment:分组路径"`
  101. }
  102. req := new(request)
  103. if err := c.BindJSON(req); err != nil {
  104. response.Resp(c, err.Error())
  105. return
  106. }
  107. if req.Name == "" || req.Group == "" || req.Path == "" {
  108. response.Resp(c, "基础信息填写不完整")
  109. return
  110. }
  111. _, ok := base.GetOne[system2.Apis](s.DB(), "path", req.Path)
  112. if ok {
  113. response.Resp(c, "分组已存在")
  114. return
  115. }
  116. parentId := gocast.ToInt64(req.ParentId)
  117. if parentId != 0 {
  118. _, ok := base.GetOne[system2.Apis](s.DB(), "id", parentId)
  119. if !ok {
  120. response.Resp(c, "上级API不存在")
  121. return
  122. }
  123. }
  124. row := &system2.Apis{
  125. Name: req.Name,
  126. Group: req.Group,
  127. Path: req.Path,
  128. ParentId: parentId,
  129. }
  130. if err := s.DB().Create(&row).Error; err != nil {
  131. response.Resp(c, err.Error())
  132. return
  133. }
  134. response.Resp(c)
  135. return
  136. }
  137. func (s *ApisService) Find(c *gin.Context) {
  138. type request[T any] struct {
  139. base.ListRequest[T]
  140. Id *interface{} `form:"id"`
  141. ParentId interface{} `json:"parentId"`
  142. }
  143. req := new(request[system2.Apis])
  144. if err := c.BindQuery(req); err != nil {
  145. response.Resp(c, err.Error())
  146. return
  147. }
  148. db := s.DB()
  149. if req.Id != nil && *req.Id != 0 {
  150. db = db.Where("id", req.Id)
  151. }
  152. if req.ParentId != nil && gocast.ToInt64(req.ParentId) != 0 {
  153. db = db.Where("parent_id", req.ParentId)
  154. }
  155. resp, err := base.NewQueryBaseHandler(system2.NewApis()).List(db, req)
  156. if err != nil {
  157. response.Resp(c, err.Error())
  158. return
  159. }
  160. response.Resp(c, resp)
  161. //base.NewBaseHandler(model.NewApis()).List(c, s.DB())
  162. }
  163. func (s *ApisService) Get(c *gin.Context) {
  164. base.NewBaseHandler(system2.NewApis()).Get(c, s.DB())
  165. }
  166. func (s *ApisService) Del(c *gin.Context) {
  167. base.NewBaseHandler(system2.NewApis()).DeleteOne(c, s.DB())
  168. }