dao.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package dynamic
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/videoup/conf"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. const (
  12. _userCheckURI = "/lottery_svr/v0/lottery_svr/user_check"
  13. _lotteryBindURI = "/lottery_svr/v0/lottery_svr/bind"
  14. )
  15. // Dao define
  16. type Dao struct {
  17. c *conf.Config
  18. client *bm.Client
  19. LotteryBindURL string
  20. UserCheckURL string
  21. }
  22. // New init dao
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. c: c,
  26. client: bm.NewClient(c.HTTPClient.Write),
  27. LotteryBindURL: c.Host.Dynamic + _lotteryBindURI,
  28. UserCheckURL: c.Host.Dynamic + _userCheckURI,
  29. }
  30. return
  31. }
  32. // LotteryBind fn
  33. func (d *Dao) LotteryBind(c context.Context, lotteryID, aid, mid int64, ip string) (err error) {
  34. params := url.Values{}
  35. params.Set("lottery_id", strconv.FormatInt(lotteryID, 10))
  36. params.Set("business_type", "8")
  37. params.Set("business_id", strconv.FormatInt(aid, 10))
  38. params.Set("sender_uid", strconv.FormatInt(mid, 10))
  39. var res struct {
  40. Code int `json:"code"`
  41. }
  42. if err = d.client.Post(c, d.LotteryBindURL, ip, params, &res); err != nil {
  43. log.Error("LotteryBind url(%s) response(%s) error(%v)", d.LotteryBindURL+"?"+params.Encode(), res, err)
  44. err = ecode.CreativeLotteryAPIErr
  45. return
  46. }
  47. log.Info("LotteryBind d.LotteryBindURL url(%s)", d.LotteryBindURL+"?"+params.Encode(), res.Code)
  48. if res.Code != 0 {
  49. log.Error("LotteryBind url(%s) res(%v)", d.LotteryBindURL, res)
  50. err = ecode.CreativeLotteryAPIErr
  51. return
  52. }
  53. return
  54. }
  55. // UserCheck fn
  56. func (d *Dao) UserCheck(c context.Context, mid int64, ip string) (ret int, err error) {
  57. params := url.Values{}
  58. params.Set("sender_uid", strconv.FormatInt(mid, 10))
  59. params.Set("business_type", "8")
  60. var res struct {
  61. Code int `json:"code"`
  62. Data struct {
  63. Result int `json:"result"`
  64. } `json:"data"`
  65. }
  66. if err = d.client.Get(c, d.UserCheckURL, ip, params, &res); err != nil {
  67. log.Error("UserCheck url(%s) response(%s) error(%v)", d.UserCheckURL+"?"+params.Encode(), res, err)
  68. err = ecode.CreativeLotteryAPIErr
  69. return
  70. }
  71. log.Info("UserCheck d.UserCheckURL url(%s)", d.UserCheckURL+"?"+params.Encode(), res.Code)
  72. if res.Code != 0 {
  73. log.Error("UserCheck url(%s) res(%v)", d.UserCheckURL, res)
  74. err = ecode.CreativeLotteryAPIErr
  75. return
  76. }
  77. ret = res.Data.Result
  78. return
  79. }