msg.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/library/ecode"
  6. "go-common/library/xstr"
  7. "github.com/pkg/errors"
  8. )
  9. // MutliSendSysMsg Mutli send sys msg.
  10. func (d *Dao) MutliSendSysMsg(c context.Context, allUids []int64, title string, context string, ip string) (err error) {
  11. var times int
  12. ulen := len(allUids)
  13. if ulen%100 == 0 {
  14. times = ulen / 100
  15. } else {
  16. times = ulen/100 + 1
  17. }
  18. var uids []int64
  19. for i := 0; i < times; i++ {
  20. if i == times-1 {
  21. uids = allUids[i*100:]
  22. } else {
  23. uids = allUids[i*100 : (i+1)*100]
  24. }
  25. if err = d.SendSysMsg(c, uids, title, context, ip); err != nil {
  26. err = errors.Wrapf(err, "d.SendSysMsg(%+v,%s,%s,%s)", uids, title, context, ip)
  27. continue
  28. }
  29. }
  30. return
  31. }
  32. // SendSysMsg send sys msg.
  33. func (d *Dao) SendSysMsg(c context.Context, uids []int64, title string, context string, ip string) (err error) {
  34. params := url.Values{}
  35. params.Set("mc", "2_1_13")
  36. params.Set("title", title)
  37. params.Set("data_type", "4")
  38. params.Set("context", context)
  39. params.Set("mid_list", xstr.JoinInts(uids))
  40. var res struct {
  41. Code int `json:"code"`
  42. Data *struct {
  43. Status int8 `json:"status"`
  44. Remark string `json:"remark"`
  45. } `json:"data"`
  46. }
  47. if err = d.client.Post(c, d.msgURL, ip, params, &res); err != nil {
  48. err = errors.Wrapf(err, "SendSysMsg d.client.Post(%s)", d.msgURL+"?"+params.Encode())
  49. return
  50. }
  51. if res.Code != 0 {
  52. err = errors.Wrapf(ecode.Int(res.Code), "SendSysMsg d.client.Post(%s,%d)", d.msgURL+"?"+params.Encode(), res.Code)
  53. }
  54. return
  55. }