account.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. )
  12. const (
  13. _accTagsURI = "/api/tag/get"
  14. _accTagsSetURI = "/api/tag/set"
  15. _liveMetalURI = "/fans_medal/v1/medal/get_medal_opened"
  16. _isAnsweredURI = "/x/internal/credit/labour/isanswered"
  17. )
  18. // AccTags get account tags.
  19. func (d *Dao) AccTags(c context.Context, mid int64) (data json.RawMessage, err error) {
  20. var (
  21. params = url.Values{}
  22. ip = metadata.String(c, metadata.RemoteIP)
  23. )
  24. params.Set("mids", strconv.FormatInt(mid, 10))
  25. var res struct {
  26. Code int `json:"code"`
  27. List json.RawMessage `json:"list"`
  28. }
  29. if err = d.httpR.Get(c, d.accTagsURL, ip, params, &res); err != nil {
  30. log.Error("d.httpR.Get(%s) error(%v)", d.accTagsURL, err)
  31. return
  32. }
  33. if res.Code != ecode.OK.Code() {
  34. log.Error("d.httpR.Get(%s) code(%d) error", d.accTagsURL, res.Code)
  35. err = ecode.Int(res.Code)
  36. return
  37. }
  38. data = res.List
  39. return
  40. }
  41. // SetAccTags set account tags.
  42. func (d *Dao) SetAccTags(c context.Context, tags, ck string) (err error) {
  43. var (
  44. params = url.Values{}
  45. ip = metadata.String(c, metadata.RemoteIP)
  46. )
  47. params.Set("tags", tags)
  48. var req *http.Request
  49. if req, err = d.httpW.NewRequest(http.MethodGet, d.accTagsSetURL, ip, params); err != nil {
  50. log.Error("d.httpW.NewRequest(%s) error(%v)", d.accTagsSetURL, err)
  51. return
  52. }
  53. req.Header.Set("Cookie", ck)
  54. var res struct {
  55. Code int `json:"code"`
  56. }
  57. if err = d.httpW.Do(c, req, &res); err != nil {
  58. log.Error("d.httpW.Do(%s) error(%v)", d.accTagsSetURL, err)
  59. return
  60. }
  61. if res.Code != ecode.OK.Code() {
  62. log.Error("d.httpW.Get(%s) code(%d) error", d.accTagsSetURL, res.Code)
  63. err = ecode.Int(res.Code)
  64. }
  65. return
  66. }
  67. // IsAnswered get if block account answered.
  68. func (d *Dao) IsAnswered(c context.Context, mid, start int64) (status int, err error) {
  69. var (
  70. params = url.Values{}
  71. ip = metadata.String(c, metadata.RemoteIP)
  72. )
  73. params.Set("mid", strconv.FormatInt(mid, 10))
  74. params.Set("start", strconv.FormatInt(start, 10))
  75. var res struct {
  76. Code int `json:"code"`
  77. Data struct {
  78. Status int `json:"status"`
  79. } `json:"data"`
  80. }
  81. if err = d.httpR.Get(c, d.isAnsweredURL, ip, params, &res); err != nil {
  82. log.Error("d.httpR.Get(%s) error(%v)", d.isAnsweredURL, err)
  83. return
  84. }
  85. if res.Code != ecode.OK.Code() {
  86. log.Error("d.httpR.Get(%s) code(%d) error", d.isAnsweredURL, res.Code)
  87. err = ecode.Int(res.Code)
  88. return
  89. }
  90. status = res.Data.Status
  91. return
  92. }