email.go 2.7 KB

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