reply_service.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package extern
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "path"
  7. "time"
  8. "go-common/library/log"
  9. )
  10. const (
  11. PathDeleteReplyByIds = "/x/internal/v2/reply/del"
  12. )
  13. type Reply struct {
  14. Id int64 `json:"id"`
  15. OId int64 `json:"oid"`
  16. OType int64 `json:"typ"`
  17. }
  18. var replySvrCli *ReplyServiceClient
  19. type ReplyServiceClient struct {
  20. *commonClient
  21. host string
  22. }
  23. type ReplyServiceResp struct {
  24. Code int `json:"code"`
  25. Message string `json:"messge"`
  26. Data interface{} `json:"data"`
  27. }
  28. type Replys []*Reply
  29. func (rs Replys) OIds() string {
  30. var s string
  31. for _, r := range rs {
  32. s += fmt.Sprintf("%d,", r.OId)
  33. }
  34. return s
  35. }
  36. func (rs Replys) Ids() string {
  37. var s string
  38. for _, r := range rs {
  39. s += fmt.Sprintf("%d,", r.Id)
  40. }
  41. return s[:len(s)-1]
  42. }
  43. func (rs Replys) OTypes() string {
  44. var s string
  45. for _, r := range rs {
  46. s += fmt.Sprintf("%d,", r.OType)
  47. }
  48. return s[:len(s)-1]
  49. }
  50. func (self *ReplyServiceClient) DeleteReply(ctx context.Context, adminId int64, rs []*Reply) error {
  51. val := url.Values{}
  52. val.Add("adid", fmt.Sprintf("%d", adminId))
  53. val.Add("adname", "antispam")
  54. val.Add("oid", Replys(rs).OIds())
  55. val.Add("rpid", Replys(rs).Ids())
  56. val.Add("type", Replys(rs).OTypes())
  57. val.Add("moral", "0")
  58. val.Add("notify", "false")
  59. val.Add("remark", "")
  60. val.Add("ftime", "")
  61. val.Add("reason", "delete by antispam")
  62. return self.do(ctx, PathDeleteReplyByIds, val, &ReplyServiceResp{}, replySvrCli.httpCli.Post)
  63. }
  64. func (rs *ReplyServiceClient) do(ctx context.Context,
  65. urlPath string, params url.Values, resp *ReplyServiceResp,
  66. fn func(ctx context.Context, uri string, ip string, params url.Values, resp interface{}) error,
  67. ) error {
  68. params.Set("appkey", rs.key)
  69. params.Set("appsecret", rs.secret)
  70. params.Set("ts", fmt.Sprintf("%d", time.Now().Unix()+int64(10)))
  71. urlAddr := path.Join(rs.host + urlPath)
  72. err := fn(ctx, urlAddr, "", params, resp)
  73. if err != nil {
  74. return err
  75. }
  76. if resp.Code != 0 {
  77. err = fmt.Errorf("Call reply service(%s), response code is not 0, resp:%v", urlAddr+"?"+params.Encode(), resp)
  78. log.Error("%v", err)
  79. return err
  80. }
  81. log.Info("Call reply service(%s) successful, resp: %v", urlAddr+"?"+params.Encode(), resp)
  82. return nil
  83. }