dao.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package message
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/app/interface/main/creative/dao/tool"
  6. "go-common/app/interface/main/creative/model/message"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. httpx "go-common/library/net/http/blademaster"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "time"
  14. )
  15. const (
  16. _getUpListURI = "/api/notify/get.up.list.do"
  17. )
  18. // Dao define
  19. type Dao struct {
  20. c *conf.Config
  21. // http
  22. client *httpx.Client
  23. // uri
  24. getUpListURL string
  25. }
  26. // New init dao
  27. func New(c *conf.Config) (d *Dao) {
  28. d = &Dao{
  29. c: c,
  30. client: httpx.NewClient(c.HTTPClient.Normal),
  31. getUpListURL: c.Host.Message + _getUpListURI,
  32. }
  33. return
  34. }
  35. // GetUpList fn
  36. func (d *Dao) GetUpList(c context.Context, mid int64, ak, ck, ip string) (data []*message.Message, err error) {
  37. data = make([]*message.Message, 0)
  38. var (
  39. res struct {
  40. Code int `json:"code"`
  41. Data []*message.Message `json:"data"`
  42. }
  43. req *http.Request
  44. )
  45. params := url.Values{}
  46. params.Set("access_key", ak)
  47. params.Set("appkey", conf.Conf.App.Key)
  48. params.Set("appsecret", conf.Conf.App.Secret)
  49. params.Set("mid", strconv.FormatInt(mid, 10))
  50. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  51. var (
  52. query, _ = tool.Sign(params)
  53. url string
  54. )
  55. url = d.getUpListURL + "?" + query
  56. if req, err = http.NewRequest("GET", url, nil); err != nil {
  57. log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  58. err = ecode.CreativeMessageAPIErr
  59. return
  60. }
  61. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  62. if len(ck) > 0 {
  63. req.Header.Set("Cookie", ck)
  64. }
  65. if err = d.client.Do(c, req, &res); err != nil {
  66. log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
  67. err = ecode.CreativeMessageAPIErr
  68. return
  69. }
  70. if res.Code != 0 {
  71. log.Error("GetUpList res.code!=0 url(%s) res(%v), mid(%d), ip(%s)", url, res, mid, ip)
  72. err = ecode.CreativeMessageAPIErr
  73. return
  74. }
  75. for _, v := range res.Data {
  76. t, _ := time.ParseInLocation("2006-01-02 15:04:05", v.TimeAt, time.Local)
  77. v.TimeStamp = t.Unix()
  78. }
  79. data = res.Data
  80. return
  81. }