live.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package live
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "go-common/app/interface/main/app-wall/conf"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. httpx "go-common/library/net/http/blademaster"
  13. )
  14. const (
  15. _liveURL = "/gift/simGift"
  16. _addVipURL = "/user/v0/Vip/addVip"
  17. )
  18. // Dao is live dao
  19. type Dao struct {
  20. client *httpx.Client
  21. liveURL string
  22. addVipURL string
  23. }
  24. // New live dao.
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: httpx.NewClient(c.HTTPClient),
  28. liveURL: c.Host.Live + _liveURL,
  29. addVipURL: c.Host.APILive + _addVipURL,
  30. }
  31. return
  32. }
  33. // Pack
  34. func (d *Dao) Pack(c context.Context, mid int64, cardType int) (err error) {
  35. var res struct {
  36. Code int `json:"code"`
  37. }
  38. upack := map[string]map[string]interface{}{
  39. "header": map[string]interface{}{},
  40. "body": map[string]interface{}{
  41. "uid": mid,
  42. "card_type": cardType,
  43. },
  44. }
  45. bytesData, err := json.Marshal(upack)
  46. if err != nil {
  47. log.Error("json.Marshal error(%v)", err)
  48. return
  49. }
  50. req, err := http.NewRequest("POST", d.liveURL, bytes.NewReader(bytesData))
  51. if err != nil {
  52. log.Error("http.NewRequest error(%v)", err)
  53. return
  54. }
  55. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  56. req.Header.Set("X-BACKEND-BILI-REAL-IP", "")
  57. log.Info("unicom pack mid(%d) card_type(%d)", mid, cardType)
  58. if err = d.client.Do(c, req, &res); err != nil || res.Code != 0 {
  59. err = ecode.Int(res.Code)
  60. log.Error("unicom pack d.client.Do(%s) mid(%d) card_type(%d) error(%v)", d.liveURL, mid, cardType, err)
  61. return
  62. }
  63. return
  64. }
  65. // AddVip add live vip
  66. func (d *Dao) AddVip(c context.Context, mid int64, day int) (msg string, err error) {
  67. params := url.Values{}
  68. params.Set("vip_type", "1")
  69. params.Set("day", strconv.Itoa(day))
  70. params.Set("uid", strconv.FormatInt(mid, 10))
  71. params.Set("platform", "main")
  72. var res struct {
  73. Code int `json:"code"`
  74. Msg string `json:"message"`
  75. }
  76. if err = d.client.Post(c, d.addVipURL, "", params, &res); err != nil {
  77. log.Error("live add vip url(%v) error(%v)", d.addVipURL+"?"+params.Encode(), err)
  78. return
  79. }
  80. msg = res.Msg
  81. if res.Code != 0 {
  82. err = ecode.Int(res.Code)
  83. log.Error("live add vip url(%v) res code(%d)", d.addVipURL+"?"+params.Encode(), res.Code)
  84. return
  85. }
  86. return
  87. }