dao.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package offer
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/job/main/app-wall/conf"
  6. "go-common/library/cache/redis"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. "github.com/pkg/errors"
  11. )
  12. const (
  13. _active = "/x/wall/offer/active2"
  14. )
  15. // Dao dao
  16. type Dao struct {
  17. c *conf.Config
  18. redis *redis.Pool
  19. client *bm.Client
  20. active string
  21. }
  22. // New init mysql db
  23. func New(c *conf.Config) (dao *Dao) {
  24. dao = &Dao{
  25. c: c,
  26. redis: redis.NewPool(c.Redis.Feed.Config),
  27. client: bm.NewClient(c.HTTPClient),
  28. active: c.Host.APP + _active,
  29. }
  30. return
  31. }
  32. // Close close the resource.
  33. func (dao *Dao) Close() (err error) {
  34. return dao.redis.Close()
  35. }
  36. // Ping dao ping
  37. func (dao *Dao) Ping(c context.Context) (err error) {
  38. return dao.PingRedis(c)
  39. }
  40. func (d *Dao) Active(c context.Context, os, imei, androidid, mac, ip string) (err error) {
  41. params := url.Values{}
  42. params.Set("os", os)
  43. params.Set("imei", imei)
  44. params.Set("androidid", androidid)
  45. params.Set("mac", mac)
  46. var res struct {
  47. Code int `json:"code"`
  48. }
  49. if err = d.client.Post(c, d.active, ip, params, &res); err != nil {
  50. return
  51. }
  52. if res.Code != ecode.OK.Code() {
  53. err = errors.Wrap(ecode.Int(res.Code), d.active+"?"+params.Encode())
  54. if res.Code == ecode.RequestErr.Code() {
  55. log.Error("%+v", err)
  56. err = nil
  57. return
  58. }
  59. }
  60. return
  61. }