message.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/admin/main/reply/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _msgReplyDel = "1_1_2"
  14. _msgReportAccept = "1_3_1"
  15. _msgTypeSystem = 4
  16. // api
  17. _apiMsgSend = "http://message.bilibili.co/api/notify/send.user.notify.do"
  18. )
  19. // SendReplyDelMsg send delete reply message.
  20. func (d *Dao) SendReplyDelMsg(c context.Context, mid int64, title, msg string, now time.Time) (err error) {
  21. return d.sendMsg(c, _msgReplyDel, title, msg, _msgTypeSystem, 0, []int64{mid}, "", now.Unix())
  22. }
  23. // SendReportAcceptMsg send report message.
  24. func (d *Dao) SendReportAcceptMsg(c context.Context, mid int64, title, msg string, now time.Time) (err error) {
  25. return d.sendMsg(c, _msgReportAccept, title, msg, _msgTypeSystem, 0, []int64{mid}, "", now.Unix())
  26. }
  27. func (d *Dao) sendMsg(c context.Context, mc, title, msg string, typ int, pub int64, mids []int64, info string, ts int64) (err error) {
  28. params := url.Values{}
  29. params.Set("type", "json")
  30. params.Set("source", "1")
  31. params.Set("mc", mc)
  32. params.Set("title", title)
  33. params.Set("data_type", strconv.Itoa(typ))
  34. params.Set("context", msg)
  35. params.Set("mid_list", xstr.JoinInts(mids))
  36. params.Set("publisher", strconv.FormatInt(pub, 10))
  37. params.Set("ext_info", info)
  38. var res struct {
  39. Code int `json:"code"`
  40. }
  41. if err = d.httpClient.Post(c, _apiMsgSend, "", params, &res); err != nil {
  42. log.Error("sendMsg error(%v) params(%v)", err, params)
  43. return
  44. }
  45. if res.Code != ecode.OK.Code() {
  46. err = model.ErrMsgSend
  47. log.Error("sendMsg failed(%v) error(%v)", _apiMsgSend+"?"+params.Encode(), res.Code)
  48. }
  49. log.Info("sendMsg(mc:%s title:%s msg:%s type:%d mids:%v error(%v)", mc, title, msg, typ, mids, err)
  50. return
  51. }