peer.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package my
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/lejianwen/rustdesk-api/http/request/admin"
  5. "github.com/lejianwen/rustdesk-api/http/response"
  6. "github.com/lejianwen/rustdesk-api/service"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type Peer struct {
  11. }
  12. // List 列表
  13. // @Tags 我的设备
  14. // @Summary 设备列表
  15. // @Description 设备列表
  16. // @Accept json
  17. // @Produce json
  18. // @Param page query int false "页码"
  19. // @Param page_size query int false "页大小"
  20. // @Param time_ago query int false "时间"
  21. // @Param id query string false "ID"
  22. // @Param hostname query string false "主机名"
  23. // @Param uuids query string false "uuids 用逗号分隔"
  24. // @Success 200 {object} response.Response{data=model.PeerList}
  25. // @Failure 500 {object} response.Response
  26. // @Router /admin/my/peer/list [get]
  27. // @Security token
  28. func (ct *Peer) List(c *gin.Context) {
  29. query := &admin.PeerQuery{}
  30. if err := c.ShouldBindQuery(query); err != nil {
  31. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  32. return
  33. }
  34. u := service.AllService.UserService.CurUser(c)
  35. res := service.AllService.PeerService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
  36. tx.Where("user_id = ?", u.Id)
  37. if query.TimeAgo > 0 {
  38. lt := time.Now().Unix() - int64(query.TimeAgo)
  39. tx.Where("last_online_time < ?", lt)
  40. }
  41. if query.TimeAgo < 0 {
  42. lt := time.Now().Unix() + int64(query.TimeAgo)
  43. tx.Where("last_online_time > ?", lt)
  44. }
  45. if query.Id != "" {
  46. tx.Where("id like ?", "%"+query.Id+"%")
  47. }
  48. if query.Hostname != "" {
  49. tx.Where("hostname like ?", "%"+query.Hostname+"%")
  50. }
  51. if query.Uuids != "" {
  52. tx.Where("uuid in (?)", query.Uuids)
  53. }
  54. })
  55. response.Success(c, res)
  56. }