activity.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/openplatform/article/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. // HandleActivity add or delete activity
  11. func (d *Dao) HandleActivity(c context.Context, mid, aid, actID int64, state int, ip string) (err error) {
  12. params := url.Values{}
  13. params.Set("mid", strconv.FormatInt(mid, 10))
  14. params.Set("oid", strconv.FormatInt(aid, 10))
  15. params.Set("state", strconv.Itoa(state)) //-1 0-待审 1
  16. params.Set("type", strconv.Itoa(12))
  17. var res struct {
  18. Code int `json:"code"`
  19. }
  20. log.Info("HandleActivity url(%s)", d.c.Article.ActAddURI+"?"+params.Encode())
  21. if err = d.httpClient.RESTfulPost(c, d.c.Article.ActAddURI, ip, params, &res, actID); err != nil {
  22. log.Error("activity: HandleActivity url(%s) response(%s) error(%+v)", d.c.Article.ActAddURI+"?"+params.Encode(), res, err)
  23. err = ecode.CreativeActivityErr
  24. PromError("activity:活动绑定")
  25. return
  26. }
  27. if res.Code != 0 {
  28. log.Error("activity: HandleActivity url(%s) res(%v)", d.c.Article.ActAddURI+"?"+params.Encode(), res)
  29. err = ecode.CreativeActivityErr
  30. PromError("activity:活动绑定")
  31. }
  32. return
  33. }
  34. // DelActivity delete activity
  35. func (d *Dao) DelActivity(c context.Context, aid int64, ip string) (err error) {
  36. params := url.Values{}
  37. params.Set("oid", strconv.FormatInt(aid, 10))
  38. params.Set("otype", strconv.Itoa(12))
  39. params.Set("state", strconv.Itoa(-1))
  40. var res struct {
  41. Code int `json:"code"`
  42. }
  43. if err = d.httpClient.Post(c, d.c.Article.ActDelURI, ip, params, &res); err != nil {
  44. log.Error("DelActivity url(%s) response(%s) error(%+v)", d.c.Article.ActDelURI+"?"+params.Encode(), res, err)
  45. err = ecode.CreativeActivityErr
  46. PromError("activity:活动取消绑定")
  47. return
  48. }
  49. if res.Code != 0 {
  50. log.Error("DelActivity url(%s) res(%v)", d.c.Article.ActDelURI+"?"+params.Encode(), res)
  51. err = ecode.CreativeActivityErr
  52. PromError("activity:活动取消绑定")
  53. }
  54. return
  55. }
  56. // Activity .
  57. func (d *Dao) Activity(c context.Context) (resp map[int64]*model.Activity, err error) {
  58. var res struct {
  59. Code int `json:"errno"`
  60. Msg string `json:"msg"`
  61. Data []*model.Activity `json:"data"`
  62. }
  63. err = d.httpClient.Get(c, d.c.Article.ActURI, "", nil, &res)
  64. if err != nil {
  65. PromError("activity:在线活动")
  66. log.Error("activity: d.client.Get(%s) error(%+v)", d.c.Article.ActURI+"?", err)
  67. return
  68. }
  69. if res.Code != 0 {
  70. PromError("activity:在线活动")
  71. log.Error("activity: url(%s) res code(%d) msg: %s", d.c.Article.ActURI+"?", res.Code, res.Msg)
  72. err = ecode.Int(res.Code)
  73. return
  74. }
  75. for _, act := range res.Data {
  76. if resp == nil {
  77. resp = make(map[int64]*model.Activity)
  78. }
  79. resp[act.ID] = act
  80. }
  81. return
  82. }