msg.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package msg
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. "go-common/library/xstr"
  8. "github.com/pkg/errors"
  9. )
  10. // MutliSendSysMsg Mutli send sys msg.
  11. func (d *Dao) MutliSendSysMsg(c context.Context, allUids []int64, mc, title string, context string, ip string) (err error) {
  12. var times int
  13. ulen := len(allUids)
  14. if ulen%100 == 0 {
  15. times = ulen / 100
  16. } else {
  17. times = ulen/100 + 1
  18. }
  19. var uids []int64
  20. for i := 0; i < times; i++ {
  21. if i == times-1 {
  22. uids = allUids[i*100:]
  23. } else {
  24. uids = allUids[i*100 : (i+1)*100]
  25. }
  26. if err = d.SendSysMsg(c, uids, mc, title, context, ip); err != nil {
  27. continue
  28. }
  29. }
  30. return
  31. }
  32. // SendSysMsg send sys msg.
  33. func (d *Dao) SendSysMsg(c context.Context, uids []int64, mc, title string, context string, ip string) (err error) {
  34. params := url.Values{}
  35. params.Set("mc", mc)
  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. return
  49. }
  50. if res.Code != 0 {
  51. err = errors.Wrapf(ecode.Int(res.Code), "SendSysMsg d.client.Post(%s,%d)", d.msgURL+"?"+params.Encode(), res.Code)
  52. } else {
  53. log.Info("send msg ok, resdata=%+v", res.Data)
  54. }
  55. return
  56. }