dao.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package message
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/job/main/videoup/conf"
  8. "go-common/library/log"
  9. xhttp "go-common/library/net/http/blademaster"
  10. "strings"
  11. "time"
  12. )
  13. // Dao is message dao.
  14. type Dao struct {
  15. c *conf.Config
  16. client *xhttp.Client
  17. uri string
  18. pushURI string
  19. }
  20. // New new a message dao.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. c: c,
  24. client: xhttp.NewClient(c.HTTPClient),
  25. uri: c.Host.Message + "/api/notify/send.user.notify.do",
  26. pushURI: c.Host.API + "/x/internal/push/single",
  27. }
  28. return
  29. }
  30. // Send send message to upper.
  31. func (d *Dao) Send(c context.Context, mc, title, msg string, mid int64, ts int64) (err error) {
  32. params := url.Values{}
  33. source := strings.Split(mc, "_")
  34. params.Set("type", "json")
  35. params.Set("source", source[0])
  36. params.Set("data_type", "4")
  37. params.Set("mc", mc)
  38. params.Set("title", title)
  39. params.Set("context", msg)
  40. params.Set("mid_list", strconv.FormatInt(mid, 10))
  41. var res struct {
  42. Code int `json:"code"`
  43. }
  44. if err = d.client.Post(c, d.uri, "", params, &res); err != nil {
  45. log.Error("message url(%s) error(%v)", d.uri+"?"+params.Encode(), err)
  46. return
  47. }
  48. log.Info("send.user.notify.do send mid(%d) message(%s) url(%s) code(%v)", mid, msg, d.uri+"?"+params.Encode(), res.Code)
  49. if res.Code != 0 {
  50. log.Error("message url(%s) error(%v)", d.uri+"?"+params.Encode(), res.Code)
  51. err = fmt.Errorf("message send failed")
  52. }
  53. return
  54. }
  55. //PushMsg 发送推送消息给创作姬APP TODO deprecated
  56. func (d *Dao) PushMsg(c context.Context, mid int64, title string, msg string) (err error) {
  57. var res struct {
  58. Code int `json:"code"`
  59. Message string `json:"message"`
  60. }
  61. params := url.Values{}
  62. params.Set("appkey", d.c.HTTPClient.Key)
  63. params.Set("appsecret", d.c.HTTPClient.Secret)
  64. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  65. params.Set("app_id", d.c.Host.Push.AppID)
  66. params.Set("business_id", d.c.Host.Push.BusinessID)
  67. params.Set("token", d.c.Host.Push.Token)
  68. params.Set("link_type", "8")
  69. params.Set("mid", strconv.FormatInt(mid, 10))
  70. params.Set("alert_title", title)
  71. params.Set("alert_body", msg)
  72. log.Info("start send PushMsg:url(%s)", d.pushURI+"?"+params.Encode())
  73. if err = d.client.Post(c, d.pushURI, "", params, &res); err != nil {
  74. log.Error("message PushMsg error(%v), url(%s)", err, d.pushURI+"?"+params.Encode())
  75. return
  76. }
  77. log.Info("after send PushMsg:response(%v) url(%s)", res, d.pushURI+"?"+params.Encode())
  78. if res.Code != 0 {
  79. log.Info("message PushMsg response is failed, res message(%+v), url(%s)", res, d.pushURI+"?"+params.Encode())
  80. err = fmt.Errorf("message PushMsg send failed")
  81. }
  82. return
  83. }