group.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. apiReq "github.com/lejianwen/rustdesk-api/v2/http/request/api"
  5. "github.com/lejianwen/rustdesk-api/v2/http/response"
  6. apiResp "github.com/lejianwen/rustdesk-api/v2/http/response/api"
  7. "github.com/lejianwen/rustdesk-api/v2/model"
  8. "github.com/lejianwen/rustdesk-api/v2/service"
  9. "net/http"
  10. )
  11. type Group struct {
  12. }
  13. // Users 用户列表
  14. // @Tags 群组
  15. // @Summary 用户列表
  16. // @Description 用户列表
  17. // @Accept json
  18. // @Produce json
  19. // @Param page query int false "页码"
  20. // @Param pageSize query int false "每页数量"
  21. // @Param status query int false "状态"
  22. // @Param accessible query string false "accessible"
  23. // @Success 200 {object} response.DataResponse{data=[]apiResp.UserPayload}
  24. // @Failure 500 {object} response.ErrorResponse
  25. // @Router /users [get]
  26. // @Security BearerAuth
  27. func (g *Group) Users(c *gin.Context) {
  28. q := &apiReq.UserListQuery{}
  29. err := c.ShouldBindQuery(&q)
  30. if err != nil {
  31. response.Error(c, err.Error())
  32. return
  33. }
  34. u := service.AllService.UserService.CurUser(c)
  35. gr := service.AllService.GroupService.InfoById(u.GroupId)
  36. userList := &model.UserList{}
  37. if !*u.IsAdmin && gr.Type != model.GroupTypeShare {
  38. //仅能获取到自己
  39. userList.Users = append(userList.Users, u)
  40. userList.Total = 1
  41. } else {
  42. userList = service.AllService.UserService.ListByGroupId(u.GroupId, q.Page, q.PageSize)
  43. }
  44. var data []*apiResp.UserPayload
  45. for _, user := range userList.Users {
  46. up := &apiResp.UserPayload{}
  47. up.FromUser(user)
  48. data = append(data, up)
  49. }
  50. c.JSON(http.StatusOK, response.DataResponse{
  51. Total: uint(userList.Total),
  52. Data: data,
  53. })
  54. }
  55. // Peers
  56. // @Tags 群组
  57. // @Summary 机器
  58. // @Description 机器
  59. // @Accept json
  60. // @Produce json
  61. // @Param page query int false "页码"
  62. // @Param pageSize query int false "每页数量"
  63. // @Param status query int false "状态"
  64. // @Param accessible query string false "accessible"
  65. // @Success 200 {object} response.DataResponse
  66. // @Failure 500 {object} response.Response
  67. // @Router /peers [get]
  68. // @Security BearerAuth
  69. func (g *Group) Peers(c *gin.Context) {
  70. u := service.AllService.UserService.CurUser(c)
  71. q := &apiReq.PeerListQuery{}
  72. err := c.ShouldBindQuery(&q)
  73. if err != nil {
  74. response.Error(c, err.Error())
  75. return
  76. }
  77. gr := service.AllService.GroupService.InfoById(u.GroupId)
  78. users := make([]*model.User, 0, 1)
  79. if !*u.IsAdmin && gr.Type != model.GroupTypeShare {
  80. //仅能获取到自己
  81. users = append(users, u)
  82. } else {
  83. users = service.AllService.UserService.ListIdAndNameByGroupId(u.GroupId)
  84. }
  85. namesById := make(map[uint]string)
  86. userIds := make([]uint, 0)
  87. for _, user := range users {
  88. namesById[user.Id] = user.Username
  89. userIds = append(userIds, user.Id)
  90. }
  91. peerList := service.AllService.PeerService.ListByUserIds(userIds, q.Page, q.PageSize)
  92. var data []*apiResp.GroupPeerPayload
  93. for _, peer := range peerList.Peers {
  94. uname, ok := namesById[peer.UserId]
  95. if !ok {
  96. uname = ""
  97. }
  98. pp := &apiResp.GroupPeerPayload{}
  99. pp.FromPeer(peer, uname)
  100. data = append(data, pp)
  101. }
  102. c.JSON(http.StatusOK, response.DataResponse{
  103. Total: uint(peerList.Total),
  104. Data: data,
  105. })
  106. }