list.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package email
  2. import (
  3. "encoding/json"
  4. log "github.com/sirupsen/logrus"
  5. "github.com/spf13/cast"
  6. "io"
  7. "math"
  8. "net/http"
  9. "pmail/dto"
  10. "pmail/dto/response"
  11. "pmail/services/list"
  12. "pmail/utils/context"
  13. )
  14. type emailListResponse struct {
  15. CurrentPage int `json:"current_page"`
  16. TotalPage int `json:"total_page"`
  17. List []*emilItem `json:"list"`
  18. }
  19. type emilItem struct {
  20. ID int `json:"id"`
  21. Title string `json:"title"`
  22. Desc string `json:"desc"`
  23. Datetime string `json:"datetime"`
  24. IsRead bool `json:"is_read"`
  25. Sender User `json:"sender"`
  26. To []User `json:"to"`
  27. Dangerous bool `json:"dangerous"`
  28. Error string `json:"error"`
  29. }
  30. type User struct {
  31. Name string `json:"Name"`
  32. EmailAddress string `json:"EmailAddress"`
  33. }
  34. type emailRequest struct {
  35. Keyword string `json:"keyword"`
  36. Tag string `json:"tag"`
  37. CurrentPage int `json:"current_page"`
  38. PageSize int `json:"page_size"`
  39. }
  40. func EmailList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  41. var lst []*emilItem
  42. reqBytes, err := io.ReadAll(req.Body)
  43. if err != nil {
  44. log.WithContext(ctx).Errorf("%+v", err)
  45. }
  46. var retData emailRequest
  47. err = json.Unmarshal(reqBytes, &retData)
  48. if err != nil {
  49. log.WithContext(ctx).Errorf("%+v", err)
  50. }
  51. offset := 0
  52. if retData.CurrentPage >= 1 {
  53. offset = (retData.CurrentPage - 1) * retData.PageSize
  54. }
  55. if retData.PageSize == 0 {
  56. retData.PageSize = 15
  57. }
  58. var tagInfo dto.SearchTag = dto.SearchTag{
  59. Type: -1,
  60. Status: -1,
  61. GroupId: -1,
  62. }
  63. _ = json.Unmarshal([]byte(retData.Tag), &tagInfo)
  64. emailList, total := list.GetEmailList(ctx, tagInfo, retData.Keyword, false, offset, retData.PageSize)
  65. for _, email := range emailList {
  66. var sender User
  67. _ = json.Unmarshal([]byte(email.Sender), &sender)
  68. var tos []User
  69. _ = json.Unmarshal([]byte(email.To), &tos)
  70. lst = append(lst, &emilItem{
  71. ID: email.Id,
  72. Title: email.Subject,
  73. Desc: email.Text.String,
  74. Datetime: email.SendDate.Format("2006-01-02 15:04:05"),
  75. IsRead: email.IsRead == 1,
  76. Sender: sender,
  77. To: tos,
  78. Dangerous: email.SPFCheck == 0 && email.DKIMCheck == 0,
  79. Error: email.Error.String,
  80. })
  81. }
  82. ret := emailListResponse{
  83. CurrentPage: retData.CurrentPage,
  84. TotalPage: cast.ToInt(math.Ceil(cast.ToFloat64(total) / cast.ToFloat64(retData.PageSize))),
  85. List: lst,
  86. }
  87. response.NewSuccessResponse(ret).FPrint(w)
  88. }