dao.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package message
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/assist/conf"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. "net/url"
  9. "strconv"
  10. )
  11. const (
  12. _messageURI = "/api/notify/send.user.notify.do"
  13. )
  14. // Dao is message dao.
  15. type Dao struct {
  16. c *conf.Config
  17. client *bm.Client
  18. uri string
  19. }
  20. // New new a message dao.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. c: c,
  24. client: bm.NewClient(c.HTTPClient.Slow),
  25. uri: c.Host.Message + _messageURI,
  26. }
  27. return d
  28. }
  29. // Send send message form uper to assistMid.
  30. func (d *Dao) Send(c context.Context, mc, title, msg string, mid int64) (err error) {
  31. params := url.Values{}
  32. params.Set("type", "json")
  33. params.Set("source", "1")
  34. params.Set("data_type", "4")
  35. params.Set("mc", mc)
  36. params.Set("title", title)
  37. params.Set("context", msg)
  38. params.Set("mid_list", strconv.FormatInt(mid, 10))
  39. var res struct {
  40. Code int `json:"code"`
  41. }
  42. if err = d.client.Post(c, d.uri, "", params, &res); err != nil {
  43. log.Error("message url(%s) error(%v)", d.uri+"?"+params.Encode(), err)
  44. return
  45. }
  46. log.Info("SendSysNotify url: (%s)", d.uri+"?"+params.Encode())
  47. if res.Code != 0 {
  48. log.Error("message url(%s) error(%v)", d.uri+"?"+params.Encode(), res.Code)
  49. err = fmt.Errorf("message send failed")
  50. }
  51. return
  52. }