list.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Dangerous bool `json:"dangerous"`
  27. Error string `json:"error"`
  28. }
  29. type User struct {
  30. Name string `json:"Name"`
  31. EmailAddress string `json:"EmailAddress"`
  32. }
  33. type emailRequest struct {
  34. Keyword string `json:"keyword"`
  35. Tag string `json:"tag"`
  36. CurrentPage int `json:"current_page"`
  37. PageSize int `json:"page_size"`
  38. }
  39. func EmailList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
  40. var lst []*emilItem
  41. reqBytes, err := io.ReadAll(req.Body)
  42. if err != nil {
  43. log.WithContext(ctx).Errorf("%+v", err)
  44. }
  45. var retData emailRequest
  46. err = json.Unmarshal(reqBytes, &retData)
  47. if err != nil {
  48. log.WithContext(ctx).Errorf("%+v", err)
  49. }
  50. offset := 0
  51. if retData.CurrentPage >= 1 {
  52. offset = (retData.CurrentPage - 1) * retData.PageSize
  53. }
  54. if retData.PageSize == 0 {
  55. retData.PageSize = 15
  56. }
  57. var tagInfo dto.SearchTag = dto.SearchTag{
  58. Type: -1,
  59. Status: -1,
  60. GroupId: -1,
  61. }
  62. _ = json.Unmarshal([]byte(retData.Tag), &tagInfo)
  63. emailList, total := list.GetEmailList(ctx, tagInfo, retData.Keyword, false, offset, retData.PageSize)
  64. for _, email := range emailList {
  65. var sender User
  66. _ = json.Unmarshal([]byte(email.Sender), &sender)
  67. lst = append(lst, &emilItem{
  68. ID: email.Id,
  69. Title: email.Subject,
  70. Desc: email.Text.String,
  71. Datetime: email.SendDate.Format("2006-01-02 15:04:05"),
  72. IsRead: email.IsRead == 1,
  73. Sender: sender,
  74. Dangerous: email.SPFCheck == 0 && email.DKIMCheck == 0,
  75. Error: email.Error.String,
  76. })
  77. }
  78. ret := emailListResponse{
  79. CurrentPage: retData.CurrentPage,
  80. TotalPage: cast.ToInt(math.Ceil(cast.ToFloat64(total) / cast.ToFloat64(retData.PageSize))),
  81. List: lst,
  82. }
  83. response.NewSuccessResponse(ret).FPrint(w)
  84. }