dao.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package msg
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "go-common/app/admin/main/credit/conf"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. )
  12. const (
  13. _msgURL = "/api/notify/send.user.notify.do"
  14. )
  15. // Dao struct info of Dao.
  16. type Dao struct {
  17. // http
  18. client *bm.Client
  19. // conf
  20. c *conf.Config
  21. msgURL string
  22. }
  23. // New new a Dao and return.
  24. func New(c *conf.Config) (d *Dao) {
  25. d = &Dao{
  26. // conf
  27. c: c,
  28. // http client
  29. client: bm.NewClient(c.HTTPClient),
  30. }
  31. d.msgURL = c.Host.Msg + _msgURL
  32. return
  33. }
  34. // SendSysMsg send sys msg.
  35. func (dao *Dao) SendSysMsg(c context.Context, mid int64, title string, context string) (err error) {
  36. params := url.Values{}
  37. params.Set("mc", "2_1_13")
  38. params.Set("title", title)
  39. params.Set("data_type", "4")
  40. params.Set("context", context)
  41. params.Set("mid_list", fmt.Sprintf("%d", mid))
  42. var res struct {
  43. Code int `json:"code"`
  44. Data *struct {
  45. Status int8 `json:"status"`
  46. Remark string `json:"remark"`
  47. } `json:"data"`
  48. }
  49. for i := 0; i <= 5; i++ {
  50. if err = dao.client.Post(c, dao.msgURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
  51. log.Error("sendMsgURI(%s) error(%v)", dao.msgURL+"?"+params.Encode(), err)
  52. continue
  53. }
  54. if res.Code != 0 {
  55. log.Error("sendMsgURI(%s) error(%v)", dao.msgURL+"?"+params.Encode(), res.Code)
  56. err = ecode.Int(res.Code)
  57. continue
  58. }
  59. return
  60. }
  61. return
  62. }