berserker.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package dao
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "go-common/app/interface/openplatform/article/model"
  13. "go-common/library/log"
  14. "go-common/library/xstr"
  15. )
  16. var _queryStr = `{"select":[{"name":"tid"},{"name":"oid"},{"name":"log_date"}],"where":{"tid":{"in":[%s]}},"page":{"limit":10,"skip":0}}`
  17. // BerserkerTagArts .
  18. func (d *Dao) BerserkerTagArts(c context.Context, tags []int64) (aids []int64, err error) {
  19. var (
  20. query string
  21. res struct {
  22. Code int
  23. Msg string
  24. Result []struct {
  25. Tid int64 `json:"tid"`
  26. Oid string `json:"oid"`
  27. LogDate string `json:"log_date"`
  28. }
  29. }
  30. tmps = make(map[int64]bool)
  31. aid int64
  32. date time.Time
  33. now = time.Now()
  34. )
  35. query = fmt.Sprintf(_queryStr, xstr.JoinInts(tags))
  36. if err = d.berserkerQuery(c, query, &res); err != nil {
  37. return
  38. }
  39. if res.Code != 200 {
  40. log.Error("s.BerserkerTagArts.query code(%d) msg(%s)", res.Code, res.Msg)
  41. return
  42. }
  43. for _, v := range res.Result {
  44. if date, err = time.Parse("20060102", v.LogDate); err != nil {
  45. log.Error("s.BerserkerTagArts.time.Parse(%s) error(%+v)", v.LogDate, err)
  46. return
  47. }
  48. if now.Sub(date) > time.Hour*60 {
  49. continue
  50. }
  51. ids := strings.Split(v.Oid, ",")
  52. var ts []int64
  53. for _, id := range ids {
  54. if aid, err = strconv.ParseInt(id, 10, 64); err != nil {
  55. log.Error("s.BerserkerTagArts.ParseInt(%s) error(%+v)", id, err)
  56. return
  57. }
  58. if !tmps[aid] {
  59. aids = append(aids, aid)
  60. tmps[aid] = true
  61. }
  62. ts = append(ts, aid)
  63. }
  64. d.AddCacheAidsByTag(c, v.Tid, &model.TagArts{Tid: v.Tid, Aids: ts})
  65. }
  66. return
  67. }
  68. func (d *Dao) berserkerQuery(c context.Context, query string, res interface{}) (err error) {
  69. var (
  70. params = url.Values{}
  71. now = time.Now().Format("2006-01-02 15:04:05")
  72. sign string
  73. req *http.Request
  74. )
  75. sign = d.sign(now)
  76. params.Set("appKey", d.c.Berserker.AppKey)
  77. params.Set("signMethod", "md5")
  78. params.Set("timestamp", now)
  79. params.Set("version", "1.0")
  80. params.Set("query", query)
  81. params.Set("sign", sign)
  82. req, err = http.NewRequest(http.MethodGet, d.c.Berserker.URL+"?"+params.Encode(), nil)
  83. if err != nil {
  84. log.Error("d.berserkerQuery.NewRequest error(%+v)", err)
  85. return
  86. }
  87. return d.httpClient.Do(c, req, res)
  88. }
  89. // Sign calc appkey and appsecret sign.
  90. func (d *Dao) sign(ts string) string {
  91. str := d.c.Berserker.AppSecret + "appKey" + d.c.Berserker.AppKey + "timestamp" + ts + "version1.0" + d.c.Berserker.AppSecret
  92. mh := md5.Sum([]byte(str))
  93. return strings.ToUpper(hex.EncodeToString(mh[:]))
  94. }