vip.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package vip
  2. import (
  3. "context"
  4. "net/url"
  5. "go-common/app/interface/main/reply/conf"
  6. "go-common/app/interface/main/reply/model/vip"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. _emojiURL = "/internal/v1/emoji/list"
  12. )
  13. type result struct {
  14. Code int `json:"code"`
  15. Data []*struct {
  16. ID int64 `json:"id"`
  17. Name string `json:"name"`
  18. URL string `json:"path"`
  19. State int8 `json:"deleted"`
  20. Emojis []*struct {
  21. ID int64 `json:"id"`
  22. Name string `json:"name"`
  23. URL string `json:"path"`
  24. State int8 `json:"deleted"`
  25. Remark string `json:"remark"`
  26. } `json:"emojiList"`
  27. } `json:"data"`
  28. }
  29. // Dao dao.
  30. type Dao struct {
  31. emojiURL string
  32. httpClient *bm.Client
  33. }
  34. // New new a dao and return.
  35. func New(c *conf.Config) *Dao {
  36. d := &Dao{
  37. httpClient: bm.NewClient(c.HTTPClient),
  38. emojiURL: c.Reply.VipURL + _emojiURL,
  39. }
  40. return d
  41. }
  42. // Emoji return emojis to cache
  43. func (dao *Dao) Emoji(c context.Context) (emjs []*vip.Emoji, emjM map[string]int64, err error) {
  44. var res result
  45. params := url.Values{}
  46. if err = dao.httpClient.Get(c, dao.emojiURL, "", params, &res); err != nil {
  47. log.Error("vip url(%s) error(%v)", dao.emojiURL+"?"+params.Encode(), err)
  48. return
  49. }
  50. if res.Code != 0 {
  51. log.Error("vip url(%s) error(%v)", dao.emojiURL+"?"+params.Encode(), res.Code)
  52. return
  53. }
  54. emjs = make([]*vip.Emoji, 0, len(res.Data))
  55. emjM = make(map[string]int64)
  56. for _, d := range res.Data {
  57. var tmp = &vip.Emoji{
  58. Pid: d.ID,
  59. Pname: d.Name,
  60. Purl: d.URL,
  61. Pstate: d.State,
  62. }
  63. emoTmps := make([]*vip.Face, 0, len(d.Emojis))
  64. for _, e := range d.Emojis {
  65. var emoTmp = &vip.Face{
  66. ID: e.ID,
  67. Name: e.Name,
  68. URL: e.URL,
  69. State: e.State,
  70. }
  71. emoTmps = append(emoTmps, emoTmp)
  72. if d.State == 0 && e.State == 0 {
  73. emjM[e.Name] = e.ID
  74. }
  75. }
  76. tmp.Emojis = emoTmps
  77. emjs = append(emjs, tmp)
  78. }
  79. return
  80. }