dao.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package game
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "time"
  10. "go-common/app/interface/main/app-view/conf"
  11. "go-common/app/interface/main/app-view/model"
  12. "go-common/app/interface/main/app-view/model/game"
  13. "go-common/library/ecode"
  14. httpx "go-common/library/net/http/blademaster"
  15. "github.com/pkg/errors"
  16. )
  17. const (
  18. _infoURL = "/game/info"
  19. )
  20. type Dao struct {
  21. client *httpx.Client
  22. infoURL string
  23. key string
  24. secret string
  25. }
  26. func New(c *conf.Config) (d *Dao) {
  27. d = &Dao{
  28. client: httpx.NewClient(c.HTTPGame),
  29. infoURL: c.Host.Game + _infoURL,
  30. key: c.HTTPGame.Key,
  31. secret: c.HTTPGame.Secret,
  32. }
  33. return
  34. }
  35. func (d *Dao) Info(c context.Context, gameID int64, plat int8) (info *game.Info, err error) {
  36. var platType int
  37. if model.IsAndroid(plat) {
  38. platType = 1
  39. } else if model.IsIOS(plat) {
  40. platType = 2
  41. }
  42. if platType == 0 {
  43. return
  44. }
  45. var req *http.Request
  46. params := url.Values{}
  47. params.Set("appkey", d.key)
  48. params.Set("game_base_id", strconv.FormatInt(gameID, 10))
  49. params.Set("platform_type", strconv.Itoa(platType))
  50. params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1e6, 10))
  51. mh := md5.Sum([]byte(params.Encode() + d.secret))
  52. params.Set("sign", hex.EncodeToString(mh[:]))
  53. if req, err = d.client.NewRequest("GET", d.infoURL, "", params); err != nil {
  54. return
  55. }
  56. var res struct {
  57. Code int `json:"code"`
  58. Data *game.Info `json:"data"`
  59. }
  60. if err = d.client.Do(c, req, &res); err != nil {
  61. return
  62. }
  63. if res.Code != ecode.OK.Code() {
  64. err = errors.Wrap(ecode.Int(res.Code), d.infoURL+"?"+params.Encode())
  65. return
  66. }
  67. info = res.Data
  68. return
  69. }