shopping.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package shopping
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "net/http"
  7. "go-common/app/interface/main/app-wall/conf"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. httpx "go-common/library/net/http/blademaster"
  11. )
  12. const (
  13. _couponURL = "/mall-marketing/coupon_code/create"
  14. )
  15. // Dao is shopping dao
  16. type Dao struct {
  17. client *httpx.Client
  18. couponURL string
  19. }
  20. // New shopping dao.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. client: httpx.NewClient(c.HTTPClient),
  24. // url
  25. couponURL: c.Host.Mall + _couponURL,
  26. }
  27. return
  28. }
  29. // Coupon user vip
  30. func (d *Dao) Coupon(c context.Context, couponID string, mid int64, uname string) (msg string, err error) {
  31. var res struct {
  32. Code int `json:"code"`
  33. Msg string `json:"message"`
  34. }
  35. data := map[string]interface{}{
  36. "couponId": couponID,
  37. "mid": mid,
  38. "uname": uname,
  39. }
  40. var (
  41. bytesData []byte
  42. req *http.Request
  43. )
  44. if bytesData, err = json.Marshal(data); err != nil {
  45. log.Error("json.Marshal error(%v)", err)
  46. return
  47. }
  48. if req, err = http.NewRequest("POST", d.couponURL, bytes.NewReader(bytesData)); err != nil {
  49. log.Error("http.NewRequest error(%v)", err)
  50. return
  51. }
  52. req.Header.Set("Content-Type", "application/json;charset=UTF-8")
  53. req.Header.Set("X-BACKEND-BILI-REAL-IP", "")
  54. log.Info("coupon vip mid(%d) couponID(%s)", mid, couponID)
  55. if err = d.client.Do(c, req, &res); err != nil {
  56. log.Error("coupon vip url(%v) error(%v)", d.couponURL, err)
  57. return
  58. }
  59. msg = res.Msg
  60. if res.Code != 0 {
  61. err = ecode.Int(res.Code)
  62. log.Error("coupon vip url(%v) res code(%d)", d.couponURL, res.Code)
  63. return
  64. }
  65. return
  66. }