reply.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package dao
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "time"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _replyType = "12"
  12. _replyStateOpen = "0" // 0: open, 1: close
  13. _replyStateClose = "1"
  14. _replyURL = "http://api.bilibili.co/x/internal/v2/reply/subject/regist"
  15. )
  16. // OpenReply opens article's reply.
  17. func (d *Dao) OpenReply(c context.Context, aid, mid int64) (err error) {
  18. defer func() {
  19. if err == nil {
  20. return
  21. }
  22. time.Sleep(time.Second)
  23. if e := d.PushReply(c, aid, mid); e != nil {
  24. log.Error("d.PushReply(%d,%d) error(%+v)", aid, mid, e)
  25. }
  26. }()
  27. params := url.Values{}
  28. params.Set("mid", strconv.FormatInt(mid, 10))
  29. params.Set("oid", strconv.FormatInt(aid, 10))
  30. params.Set("type", _replyType)
  31. params.Set("state", _replyStateOpen)
  32. var res struct {
  33. Code int `json:"code"`
  34. }
  35. if err = d.httpClient.Post(c, _replyURL, "", params, &res); err != nil {
  36. log.Error("d.httpClient.Post(%s) error(%+v)", _replyURL+"?"+params.Encode(), err)
  37. PromError("reply:打开评论")
  38. return
  39. }
  40. if res.Code != ecode.OK.Code() {
  41. log.Error("d.httpClient.Post(%s) code(%d)", _replyURL+"?"+params.Encode(), res.Code)
  42. PromError("reply:打开评论状态码异常")
  43. err = ecode.Int(res.Code)
  44. }
  45. return
  46. }
  47. // CloseReply close article's reply.
  48. func (d *Dao) CloseReply(c context.Context, aid, mid int64) (err error) {
  49. defer func() {
  50. if err == nil {
  51. return
  52. }
  53. time.Sleep(time.Second)
  54. if e := d.PushReply(c, aid, mid); e != nil {
  55. log.Error("d.PushReply(%d,%d) error(%+v)", aid, mid, e)
  56. }
  57. }()
  58. params := url.Values{}
  59. params.Set("mid", strconv.FormatInt(mid, 10))
  60. params.Set("oid", strconv.FormatInt(aid, 10))
  61. params.Set("type", _replyType)
  62. params.Set("state", _replyStateClose)
  63. var res struct {
  64. Code int `json:"code"`
  65. }
  66. if err = d.httpClient.Post(c, _replyURL, "", params, &res); err != nil {
  67. log.Error("d.httpClient.Post(%s) error(%+v)", _replyURL+"?"+params.Encode(), err)
  68. PromError("reply:打开评论")
  69. return
  70. }
  71. if res.Code != ecode.OK.Code() {
  72. log.Error("d.httpClient.Post(%s) code(%d)", _replyURL+"?"+params.Encode(), res.Code)
  73. PromError("reply:打开评论状态码异常")
  74. err = ecode.Int(res.Code)
  75. }
  76. return
  77. }