msg.go 1.6 KB

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