api.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package game
  2. import (
  3. "context"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/app/interface/main/creative/conf"
  9. "go-common/app/interface/main/creative/dao/tool"
  10. "go-common/app/interface/main/creative/model/game"
  11. "go-common/library/ecode"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _gameListURI = "/game/list"
  16. _gameInfoURI = "/game/info"
  17. )
  18. // List fn
  19. func (d *Dao) List(c context.Context, keywordStr, ip string) (gameList []*game.ListItem, err error) {
  20. params := url.Values{}
  21. params.Set("appkey", conf.Conf.Game.App.Key)
  22. params.Set("appsecret", conf.Conf.Game.App.Secret)
  23. params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
  24. params.Set("keyword", keywordStr)
  25. var res struct {
  26. Code int `json:"code"`
  27. Data []*game.ListItem `json:"data"`
  28. }
  29. var (
  30. query, _ = tool.Sign(params)
  31. url string
  32. )
  33. url = d.gameListURL + "?" + query
  34. req, err := http.NewRequest("GET", url, nil)
  35. if err != nil {
  36. log.Error("http.NewRequest(%s) error(%v), ip(%s)", url, err, ip)
  37. err = ecode.CreativeGameOpenAPIErr
  38. return
  39. }
  40. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  41. if err = d.client.Do(c, req, &res); err != nil {
  42. log.Error("d.client.Do(%s) error(%v); ip(%s)", url, err, ip)
  43. err = ecode.CreativeGameOpenAPIErr
  44. return
  45. }
  46. log.Info("game list url(%+v)", url)
  47. if res.Code != 0 {
  48. log.Error("game open api url(%s) res(%v); ip(%s), code(%d)", url, res, ip, res.Code)
  49. err = ecode.CreativeGameOpenAPIErr
  50. }
  51. gameList = res.Data
  52. return
  53. }
  54. // Info fn
  55. func (d *Dao) Info(c context.Context, gameBaseID int64, platForType int, ip string) (info *game.Info, err error) {
  56. params := url.Values{}
  57. params.Set("game_base_id", strconv.FormatInt(gameBaseID, 10))
  58. params.Set("platform_type", strconv.Itoa(platForType))
  59. params.Set("appkey", conf.Conf.Game.App.Key)
  60. params.Set("appsecret", conf.Conf.Game.App.Secret)
  61. params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
  62. var res struct {
  63. Code int `json:"code"`
  64. Msg string `json:"message"`
  65. Data *game.Info `json:"data"`
  66. }
  67. var (
  68. query, _ = tool.Sign(params)
  69. url string
  70. )
  71. url = d.gameInfoURL + "?" + query
  72. req, err := http.NewRequest("GET", url, nil)
  73. if err != nil {
  74. log.Error("http.NewRequest(%s) error(%v), ip(%s)", url, err, ip)
  75. err = ecode.CreativeGameOpenAPIErr
  76. return
  77. }
  78. req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
  79. if err = d.client.Do(c, req, &res); err != nil {
  80. log.Error("d.client.Do(%s) error(%v); ip(%s)", url, err, ip)
  81. err = ecode.CreativeGameOpenAPIErr
  82. return
  83. }
  84. log.Info("game info url(%+v)", url)
  85. if res.Code != 0 {
  86. log.Error("game open api url(%s) res(%v); ip(%s), code(%d)", url, res, ip, res.Code)
  87. err = ecode.CreativeGameOpenAPIErr
  88. }
  89. info = res.Data
  90. return
  91. }