dao.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package archive
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. article "go-common/app/interface/openplatform/article/rpc/client"
  7. "go-common/app/job/main/creative/conf"
  8. "go-common/app/job/main/creative/model"
  9. archive "go-common/app/service/main/archive/api/gorpc"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. httpx "go-common/library/net/http/blademaster"
  13. )
  14. // Dao is archive dao.
  15. type Dao struct {
  16. // config
  17. c *conf.Config
  18. // rpc
  19. arc *archive.Service2
  20. art *article.Service
  21. // http client
  22. client *httpx.Client
  23. viewURL string
  24. }
  25. // New init api url
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. c: c,
  29. arc: archive.New2(c.ArchiveRPC),
  30. art: article.New(c.ArticleRPC),
  31. // http client
  32. client: httpx.NewClient(c.HTTPClient.Normal),
  33. viewURL: c.Host.Videoup + "/videoup/view",
  34. }
  35. return
  36. }
  37. // Ping fn
  38. func (d *Dao) Ping(c context.Context) (err error) {
  39. return
  40. }
  41. // Close fn
  42. func (d *Dao) Close() (err error) {
  43. return
  44. }
  45. // View get archive
  46. func (d *Dao) View(c context.Context, mid, aid int64) (av *model.ArcVideo, err error) {
  47. params := url.Values{}
  48. params.Set("mid", strconv.FormatInt(mid, 10))
  49. params.Set("aid", strconv.FormatInt(aid, 10))
  50. var res struct {
  51. Code int `json:"code"`
  52. Data *model.ArcVideo `json:"data"`
  53. }
  54. if err = d.client.Get(c, d.viewURL, "", params, &res); err != nil {
  55. log.Error("archive.view url(%s) mid(%d) error(%v)", d.viewURL+"?"+params.Encode(), mid, err)
  56. err = ecode.CreativeArchiveAPIErr
  57. return
  58. }
  59. if res.Code != 0 {
  60. log.Error("archive.view url(%s) mid(%d) res(%v)", d.viewURL+"?"+params.Encode(), mid, res)
  61. err = ecode.Int(res.Code)
  62. return
  63. }
  64. if res.Data.Archive == nil {
  65. log.Error("archive.view url(%s) mid(%d) res(%v)", d.viewURL+"?"+params.Encode(), mid, res)
  66. return
  67. }
  68. av = res.Data
  69. return
  70. }