send.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package message
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. // Send message to upper
  12. func (d *Dao) Send(c context.Context, mc, title, msg string, mids []int64, ts int64) (err error) {
  13. params := url.Values{}
  14. source := strings.Split(mc, "_")
  15. params.Set("type", "json")
  16. params.Set("source", source[0])
  17. params.Set("data_type", "4")
  18. params.Set("mc", mc)
  19. params.Set("title", title)
  20. params.Set("context", msg)
  21. var midList string
  22. for _, mid := range mids {
  23. midList += strconv.FormatInt(mid, 10)
  24. midList += ","
  25. }
  26. midList = strings.TrimSuffix(midList, ",")
  27. params.Set("mid_list", midList)
  28. var res struct {
  29. Code int `json:"code"`
  30. }
  31. log.Info("params:%v", params)
  32. if err = d.client.Post(c, d.uri, "", params, &res); err != nil {
  33. log.Error("growup-admin message url(%s) error(%v)", d.uri+"?"+params.Encode(), err)
  34. return
  35. }
  36. log.Info("message res code:%d", res.Code)
  37. if res.Code != 0 {
  38. log.Error("growup-admin message url(%s) error(%v)", d.uri+"?"+params.Encode(), err)
  39. err = fmt.Errorf("message send failed")
  40. }
  41. return
  42. }
  43. // NotifyTask notify task finish
  44. func (d *Dao) NotifyTask(c context.Context, mids []int64) (err error) {
  45. params := url.Values{}
  46. params.Set("mids", xstr.JoinInts(mids))
  47. var res struct {
  48. Code int `json:"code"`
  49. }
  50. log.Info("creative notify task params:%v", params)
  51. if err = d.client.Post(c, d.creativeURL, "", params, &res); err != nil {
  52. log.Error("growup-admin creative notify task url(%s) error(%v)", d.creativeURL+"?"+params.Encode(), err)
  53. return
  54. }
  55. log.Info("creative notify task res code:%d", res.Code)
  56. if res.Code != 0 {
  57. log.Error("growup-admin creative notify task url(%s) error(%v)", d.creativeURL+"?"+params.Encode(), err)
  58. err = fmt.Errorf("creative notify task send failed")
  59. }
  60. return
  61. }