schedule.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package jpushclient
  2. import (
  3. "encoding/json"
  4. "time"
  5. )
  6. type Schedule struct {
  7. Cid string `json:"cid"`
  8. Name string `json:"name"`
  9. Enabled bool `json:"enabled"`
  10. Trigger map[string]interface{} `json:"trigger"`
  11. Push *PayLoad `json:"push"`
  12. }
  13. func NewSchedule(name,cid string, enabled bool, push *PayLoad) *Schedule {
  14. return &Schedule{
  15. Cid: cid,
  16. Name: name,
  17. Enabled: enabled,
  18. Push: push,
  19. }
  20. }
  21. func (s *Schedule) SingleTrigger(t time.Time) {
  22. s.Trigger = map[string]interface{}{
  23. "single": map[string]interface{}{
  24. "time": t.Format("2006-01-02 15:04:05"),
  25. },
  26. }
  27. }
  28. func (s *Schedule) PeriodicalTrigger(start time.Time, end time.Time, time time.Time, timeUnit string, frequency int, point []string) {
  29. s.Trigger = map[string]interface{}{
  30. "periodical": map[string]interface{}{
  31. "start": start.Format("2006-01-02 15:04:05"),
  32. "end": end.Format("2006-01-02 15:04:05"),
  33. "time": start.Format("15:04:05"),
  34. "time_unit": timeUnit,
  35. "frequency": frequency,
  36. "point": point,
  37. },
  38. }
  39. }
  40. func (this *Schedule) ToBytes() ([]byte, error) {
  41. content, err := json.Marshal(this)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return content, nil
  46. }