email.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package models
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "time"
  6. )
  7. type Email struct {
  8. Id int `db:"id" json:"id"`
  9. Type int8 `db:"type" json:"type"`
  10. Subject string `db:"subject" json:"subject"`
  11. ReplyTo string `db:"reply_to" json:"reply_to"`
  12. FromName string `db:"from_name" json:"from_name"`
  13. FromAddress string `db:"from_address" json:"from_address"`
  14. To string `db:"to" json:"to"`
  15. Bcc string `db:"bcc" json:"bcc"`
  16. Cc string `db:"cc" json:"cc"`
  17. Text sql.NullString `db:"text" json:"text"`
  18. Html sql.NullString `db:"html" json:"html"`
  19. Sender string `db:"sender" json:"sender"`
  20. Attachments string `db:"attachments" json:"attachments"`
  21. SPFCheck int8 `db:"spf_check" json:"spf_check"`
  22. DKIMCheck int8 `db:"dkim_check" json:"dkim_check"`
  23. Status int8 `db:"status" json:"status"`
  24. CronSendTime time.Time `db:"cron_send_time" json:"cron_send_time"`
  25. UpdateTime time.Time `db:"update_time" json:"update_time"`
  26. SendUserID int `db:"send_user_id" json:"send_user_id"`
  27. IsRead int8 `db:"is_read" json:"is_read"`
  28. Error sql.NullString `db:"error" json:"error"`
  29. SendDate time.Time `db:"send_date" json:"send_date"`
  30. CreateTime time.Time `db:"create_time" json:"create_time"`
  31. }
  32. type attachments struct {
  33. Filename string
  34. ContentType string
  35. Index int
  36. //Content []byte
  37. }
  38. func (d Email) MarshalJSON() ([]byte, error) {
  39. type Alias Email
  40. var allAtt = []attachments{}
  41. var showAtt = []attachments{}
  42. if d.Attachments != "" {
  43. _ = json.Unmarshal([]byte(d.Attachments), &allAtt)
  44. for i, att := range allAtt {
  45. att.Index = i
  46. if att.ContentType == "application/octet-stream" {
  47. showAtt = append(showAtt, att)
  48. }
  49. }
  50. }
  51. return json.Marshal(&struct {
  52. Alias
  53. CronSendTime string `json:"send_time"`
  54. SendDate string `json:"send_date"`
  55. UpdateTime string `json:"update_time"`
  56. CreateTime string `json:"create_time"`
  57. Text string `json:"text"`
  58. Html string `json:"html"`
  59. Error string `json:"error"`
  60. Attachments []attachments `json:"attachments"`
  61. }{
  62. Alias: (Alias)(d),
  63. CronSendTime: d.CronSendTime.Format("2006-01-02 15:04:05"),
  64. UpdateTime: d.UpdateTime.Format("2006-01-02 15:04:05"),
  65. CreateTime: d.CreateTime.Format("2006-01-02 15:04:05"),
  66. SendDate: d.SendDate.Format("2006-01-02 15:04:05"),
  67. Text: d.Text.String,
  68. Html: d.Html.String,
  69. Error: d.Error.String,
  70. Attachments: showAtt,
  71. })
  72. }