notify.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package dao
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "github.com/pkg/errors"
  9. )
  10. //notify status.
  11. const (
  12. _notifySuccess = "1"
  13. )
  14. // Notify point change .
  15. func (d *Dao) Notify(c context.Context, notifyURL string, mid int64, orderID string, ip string) (err error) {
  16. params := url.Values{}
  17. params.Set("mid", strconv.FormatInt(mid, 10))
  18. params.Set("order_id", orderID)
  19. params.Set("status", _notifySuccess)
  20. req, err := d.client.NewRequest(http.MethodPost, notifyURL, ip, params)
  21. if err != nil {
  22. err = errors.Wrapf(err, "Notify NewRequest(%s)", notifyURL+"?"+params.Encode())
  23. return
  24. }
  25. var res struct {
  26. Code int `json:"code"`
  27. }
  28. if err = d.client.Do(c, req, &res); err != nil {
  29. err = errors.Wrapf(err, "Notify d.client.Do(%s)", notifyURL+"?"+params.Encode())
  30. return
  31. }
  32. if res.Code != 0 {
  33. err = errors.Wrapf(err, "Notify code != 0 (%s)", notifyURL+"?"+params.Encode())
  34. return
  35. }
  36. return
  37. }
  38. // NotifyCacheDel notify cache del.
  39. func (d *Dao) NotifyCacheDel(c context.Context, notifyURL string, mid int64, ip string) (err error) {
  40. params := url.Values{}
  41. params.Set("mid", strconv.FormatInt(mid, 10))
  42. req, err := d.client.NewRequest(http.MethodGet, notifyURL, ip, params)
  43. if err != nil {
  44. err = errors.Wrapf(err, "NotifyCacheDel NewRequest(%s)", notifyURL+"?"+params.Encode())
  45. return
  46. }
  47. var res struct {
  48. Code int `json:"code"`
  49. }
  50. if err = d.client.Do(c, req, &res); err != nil {
  51. err = errors.Wrapf(err, "NotifyCacheDel d.client.Do(%s)", notifyURL+"?"+params.Encode())
  52. return
  53. }
  54. if res.Code != 0 {
  55. err = errors.Wrapf(err, "NotifyCacheDel code != 0 (%s)", notifyURL+"?"+params.Encode())
  56. return
  57. }
  58. log.Info("notify suc(%d) url(%s)", mid, notifyURL)
  59. return
  60. }