msg.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package msg
  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, mc, 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, mc, title, context, ip); err != nil {
  26. continue
  27. }
  28. }
  29. return
  30. }
  31. // SendSysMsg send sys msg.
  32. func (d *Dao) SendSysMsg(c context.Context, uids []int64, mc, title string, context string, ip string) (err error) {
  33. params := url.Values{}
  34. params.Set("mc", mc)
  35. params.Set("title", title)
  36. params.Set("data_type", "4")
  37. params.Set("context", context)
  38. params.Set("mid_list", xstr.JoinInts(uids))
  39. var res struct {
  40. Code int `json:"code"`
  41. Data *struct {
  42. Status int8 `json:"status"`
  43. Remark string `json:"remark"`
  44. } `json:"data"`
  45. }
  46. if err = d.client.Post(c, d.msgURL, ip, params, &res); err != nil {
  47. return
  48. }
  49. if res.Code != 0 {
  50. err = errors.Wrapf(ecode.Int(res.Code), "SendSysMsg d.client.Post(%s,%d)", d.msgURL+"?"+params.Encode(), res.Code)
  51. }
  52. return
  53. }