pendant.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package account
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/app/interface/main/answer/conf"
  8. "go-common/app/interface/main/answer/model"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/log"
  11. bm "go-common/library/net/http/blademaster"
  12. "github.com/pkg/errors"
  13. )
  14. // Dao is elec dao.
  15. type Dao struct {
  16. c *conf.Config
  17. mc *memcache.Pool
  18. client *bm.Client
  19. pendant string
  20. beFormal string
  21. extraIds string
  22. }
  23. const (
  24. _wasFormal = -659
  25. )
  26. // New pendant dao
  27. func New(c *conf.Config) (d *Dao) {
  28. d = &Dao{
  29. c: c,
  30. mc: memcache.NewPool(c.Memcache.Config),
  31. client: bm.NewClient(c.HTTPClient.Normal),
  32. pendant: c.Host.API + _multiGivePendant,
  33. beFormal: c.Host.Account + _beFormal,
  34. extraIds: c.Host.ExtraIds,
  35. }
  36. return
  37. }
  38. const (
  39. _multiGivePendant = "/x/internal/pendant/multiGrantByMid"
  40. _beFormal = "/api/internal/member/beFormal"
  41. )
  42. // GivePendant send user pendant
  43. func (d *Dao) GivePendant(c context.Context, mid int64, pid int64, days int, ip string) (err error) {
  44. log.Info(" GivePendant (%d,%d,%d) ", mid, pid, days)
  45. params := url.Values{}
  46. params.Set("mids", strconv.FormatInt(mid, 10))
  47. params.Set("pid", strconv.FormatInt(pid, 10))
  48. params.Set("expire", strconv.FormatInt(int64(days), 10))
  49. var res struct {
  50. Code int `json:"code"`
  51. }
  52. if err = d.client.Post(c, d.pendant, ip, params, &res); err != nil {
  53. log.Error("pendant url(%s) error(%v)", d.pendant+"?"+params.Encode(), err)
  54. log.Error("GivePendant(%d,%d),err:%+v", mid, pid, err)
  55. return
  56. }
  57. if res.Code != 0 {
  58. err = fmt.Errorf("pendant GivePendant failed(%v)", res.Code)
  59. log.Error(" d.client.Get(%s) error(%v)", d.pendant+"?"+params.Encode(), err)
  60. return
  61. }
  62. return
  63. }
  64. // BeFormal become a full member
  65. func (d *Dao) BeFormal(c context.Context, mid int64, ip string) (err error) {
  66. params := url.Values{}
  67. params.Set("mid", strconv.FormatInt(mid, 10))
  68. var res struct {
  69. Code int `json:"code"`
  70. }
  71. if err = d.client.Post(c, d.beFormal, ip, params, &res); err != nil {
  72. err = errors.Wrapf(err, "beFormal url(%s)", d.beFormal+"?"+params.Encode())
  73. log.Error("BeFormal(%d),err:%+v", mid, err)
  74. return
  75. }
  76. if res.Code != 0 && res.Code != _wasFormal {
  77. err = errors.WithStack(fmt.Errorf("beFormal(%d) failed(%v)", mid, res.Code))
  78. log.Error("BeFormal(%d),res:%+v", mid, res)
  79. return
  80. }
  81. log.Info("beFormal suc(%d) ", mid)
  82. return
  83. }
  84. // ExtraIds BigData Extra Question ids.
  85. func (d *Dao) ExtraIds(c context.Context, mid int64, ip string) (done []int64, pend []int64, err error) {
  86. params := url.Values{}
  87. params.Set("mid", strconv.FormatInt(mid, 10))
  88. var res struct {
  89. Code int `json:"code"`
  90. Data *model.ExtraBigData
  91. }
  92. if err = d.client.Get(c, d.extraIds, ip, params, &res); err != nil {
  93. log.Error("ExtraIds url(%s) error(%v)", d.extraIds+"?"+params.Encode(), err)
  94. return
  95. }
  96. if res.Code != 0 || res.Data == nil {
  97. err = fmt.Errorf("ExtraIds failed(%v)", res.Code)
  98. log.Error(" d.client.Get(%s) res(%v) error(%v)", d.extraIds+"?"+params.Encode(), res, err)
  99. return
  100. }
  101. done = res.Data.Done
  102. pend = res.Data.Pend
  103. return
  104. }