httpclient.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package weeklyhonor
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/url"
  7. "time"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. _title = "叮!你有一份荣誉周报待查收!"
  13. _newYearTitle = "嘭~~~啪!你有一份荣誉周报待查收!"
  14. _content = "这周的成就已达成,快进来瞅瞅吧~ #{周报传送门}{\"https://member.bilibili.com/studio/annyroal/upper-honor-weekly/my\"}"
  15. _newYearContent = "这周的成就已达成,元气满满过新年~ #{周报传送门}{\"https://member.bilibili.com/studio/annyroal/upper-honor-weekly/my\"}"
  16. _notifyDataType = "4" // 消息类型:1、回复我的2、@我3、收到的爱4、业务通知
  17. _notifyURL = "/api/notify/send.user.notify.do"
  18. _notifyMC = "1_17_1"
  19. _upMidsURL = "/x/internal/uper/list_up"
  20. )
  21. type newYear struct {
  22. start, end time.Time
  23. }
  24. var (
  25. ny = newYear{
  26. start: time.Date(2019, 2, 3, 0, 0, 0, 0, time.Local),
  27. end: time.Date(2019, 2, 24, 0, 0, 0, 0, time.Local),
  28. }
  29. )
  30. type msgReply struct {
  31. Code int `json:"code"`
  32. Data *data `json:"data"`
  33. }
  34. type data struct {
  35. TotalCount int `json:"total_count"`
  36. ErrorCount int `json:"error_count"`
  37. ErrorMidList []int64 `json:"error_mid_list"`
  38. }
  39. // SendNotify 发送站内信
  40. func (d *Dao) SendNotify(c context.Context, mids []int64) (errMids []int64, err error) {
  41. title, content := getTitleAndContent()
  42. res := msgReply{}
  43. params := url.Values{}
  44. params.Set("mc", _notifyMC)
  45. params.Set("title", title)
  46. params.Set("data_type", _notifyDataType)
  47. params.Set("context", content)
  48. params.Set("mid_list", xstr.JoinInts(mids))
  49. notifyURI := d.c.Host.Message + _notifyURL
  50. if err = d.httpClient.Post(c, notifyURI, "", params, &res); err != nil {
  51. log.Error("d.httpClient.Post(%s,%v,%d)", notifyURI, params, err)
  52. return
  53. }
  54. if res.Code != 0 {
  55. err = errors.New("code != 0")
  56. log.Error("d.httpClient.Post(%s,%v,%v,%d)", notifyURI, params, err, res.Code)
  57. }
  58. if res.Data != nil {
  59. errMids = res.Data.ErrorMidList
  60. log.Info("SendNotify log total_count(%d) error_count(%d) error_mid_list(%v)", res.Data.TotalCount, res.Data.ErrorCount, res.Data.ErrorMidList)
  61. }
  62. return
  63. }
  64. func getTitleAndContent() (string, string) {
  65. now := time.Now()
  66. if now.After(ny.end) || now.Before(ny.start) {
  67. return _title, _content
  68. }
  69. return _newYearTitle, _newYearContent
  70. }
  71. // Deprecated: use UpActivesList instead.
  72. func (d *Dao) UpMids(c context.Context, size int, lastid int64, activeOnly bool) (mids []int64, newid int64, err error) {
  73. res := &struct {
  74. Code int `json:"code"`
  75. Data *struct {
  76. Result []int64 `json:"result"`
  77. LastID int64 `json:"last_id"`
  78. } `json:"data"`
  79. }{}
  80. params := url.Values{}
  81. params.Set("size", fmt.Sprintf("%d", size))
  82. params.Set("last_id", fmt.Sprintf("%d", lastid))
  83. if activeOnly {
  84. // filter by having archive within 180 days
  85. params.Set("activity", "1,2,3")
  86. }
  87. midsURI := d.c.Host.API + _upMidsURL
  88. if err = d.httpClient.Get(c, midsURI, "", params, &res); err != nil {
  89. log.Error("d.httpClient.Get(%s,%v,%d)", midsURI+"?"+params.Encode(), params, err)
  90. return
  91. }
  92. if res.Code != 0 {
  93. err = errors.New("code != 0")
  94. log.Error("d.httpClient.Get(%s,%v,%v,%d)", midsURI, params, err, res.Code)
  95. }
  96. if res != nil && res.Data != nil {
  97. return res.Data.Result, res.Data.LastID, nil
  98. }
  99. return
  100. }