bangumi.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package bangumi
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/web-show/conf"
  8. "go-common/library/log"
  9. httpx "go-common/library/net/http/blademaster"
  10. )
  11. // Dao struct
  12. type Dao struct {
  13. client *httpx.Client
  14. isbpURL string
  15. }
  16. // New Init
  17. func New(c *conf.Config) (dao *Dao) {
  18. dao = &Dao{
  19. client: httpx.NewClient(c.HTTPClient),
  20. isbpURL: c.Host.Bangumi + "/api/bp",
  21. }
  22. return
  23. }
  24. // IsBp check user bp.
  25. func (dao *Dao) IsBp(c context.Context, mid, aid int64, ip string) (is bool) {
  26. params := url.Values{}
  27. params.Set("build", "web-show")
  28. params.Set("platform", "Golang")
  29. params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
  30. params.Set("aid", strconv.FormatInt(aid, 10))
  31. params.Set("mid", strconv.FormatInt(mid, 10))
  32. var res struct {
  33. Code int `json:"code"`
  34. Result bool `json:"result"`
  35. }
  36. if err := dao.client.Get(c, dao.isbpURL, ip, params, &res); err != nil {
  37. log.Error("bangumi url(%s) error(%v) ", dao.isbpURL+"?"+params.Encode(), err)
  38. return
  39. }
  40. // FIXME why two state
  41. if res.Code != 0 && res.Code != 1 {
  42. log.Error("bangumi Isbp api fail(%d)", res.Code)
  43. return
  44. }
  45. is = res.Result
  46. return
  47. }