group.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package api
  2. import (
  3. apiReq "Gwen/http/request/api"
  4. "Gwen/http/response"
  5. apiResp "Gwen/http/response/api"
  6. "Gwen/model"
  7. "Gwen/service"
  8. "github.com/gin-gonic/gin"
  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. u := service.AllService.UserService.CurUser(c)
  29. if !*u.IsAdmin {
  30. gr := service.AllService.GroupService.InfoById(u.GroupId)
  31. if gr.Type != model.GroupTypeShare {
  32. response.Error(c, "不是管理员也不在分享组")
  33. return
  34. }
  35. }
  36. q := &apiReq.UserListQuery{}
  37. err := c.ShouldBindQuery(&q)
  38. if err != nil {
  39. response.Error(c, err.Error())
  40. return
  41. }
  42. userList := service.AllService.UserService.ListByGroupId(u.GroupId, q.Page, q.PageSize)
  43. var data []*apiResp.UserPayload
  44. for _, user := range userList.Users {
  45. up := &apiResp.UserPayload{}
  46. up.FromUser(user)
  47. data = append(data, up)
  48. }
  49. c.JSON(http.StatusOK, response.DataResponse{
  50. Total: uint(userList.Total),
  51. Data: data,
  52. })
  53. }
  54. // Peers
  55. // @Tags 群组
  56. // @Summary 机器
  57. // @Description 机器
  58. // @Accept json
  59. // @Produce json
  60. // @Param page query int false "页码"
  61. // @Param pageSize query int false "每页数量"
  62. // @Param status query int false "状态"
  63. // @Param accessible query string false "accessible"
  64. // @Success 200 {object} response.DataResponse
  65. // @Failure 500 {object} response.Response
  66. // @Router /peers [get]
  67. // @Security BearerAuth
  68. func (g *Group) Peers(c *gin.Context) {
  69. u := service.AllService.UserService.CurUser(c)
  70. if !*u.IsAdmin {
  71. gr := service.AllService.GroupService.InfoById(u.GroupId)
  72. if gr.Type != model.GroupTypeShare {
  73. response.Error(c, "不是管理员也不在分享组")
  74. return
  75. }
  76. }
  77. q := &apiReq.PeerListQuery{}
  78. err := c.ShouldBindQuery(&q)
  79. if err != nil {
  80. response.Error(c, err.Error())
  81. return
  82. }
  83. users := service.AllService.UserService.ListIdAndNameByGroupId(u.GroupId)
  84. namesById := make(map[uint]string)
  85. userIds := make([]uint, 0)
  86. for _, user := range users {
  87. namesById[user.Id] = user.Username
  88. userIds = append(userIds, user.Id)
  89. }
  90. peerList := service.AllService.AddressBookService.ListByUserIds(userIds, q.Page, q.PageSize)
  91. var data []*apiResp.GroupPeerPayload
  92. for _, ab := range peerList.AddressBooks {
  93. uname, ok := namesById[ab.UserId]
  94. if !ok {
  95. uname = ""
  96. }
  97. pp := &apiResp.GroupPeerPayload{}
  98. pp.FromAddressBook(ab, uname)
  99. data = append(data, pp)
  100. }
  101. c.JSON(http.StatusOK, response.DataResponse{
  102. Total: uint(peerList.Total),
  103. Data: data,
  104. })
  105. }