watch.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package wxbroadcastv2
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.jiaxianghudong.com/webs/pkg/rds"
  6. )
  7. var Total = 5 //限制位置
  8. var tagMap = map[int64]string{
  9. 0: "官方直播",
  10. 1: "美女主播",
  11. 2: "人气主播",
  12. 3: "优质主播",
  13. 4: "官方认证",
  14. }
  15. type Watch struct {
  16. Index int `json:"index"`
  17. TagUrl string `json:"tag_url" form:"tag_url"`
  18. Img string `json:"img" form:"img"`
  19. Nickname string `json:"nickname" form:"nickname"`
  20. Uid string `json:"uid" form:"uid"`
  21. IsOfficial bool `json:"is_official" form:"is_official"`
  22. }
  23. type WatchConfig struct {
  24. WatchList []*Watch `json:"watch_list"`
  25. }
  26. func (w *WatchConfig) Store() error {
  27. marshal, _ := json.Marshal(w)
  28. err := rds.Redis.Set(WXBROADCASTWATCH, string(marshal), -1).Err()
  29. return err
  30. }
  31. func (w *WatchConfig) Load() {
  32. val := rds.Redis.Get(WXBROADCASTWATCH).Val()
  33. if val == "" {
  34. w.WatchList = make([]*Watch, Total)
  35. for i := range w.WatchList {
  36. w.WatchList[i] = &Watch{}
  37. }
  38. w.Store()
  39. } else {
  40. json.Unmarshal([]byte(val), w)
  41. }
  42. //for i := range w.WatchList {
  43. // w.WatchList[i].TagUrl = tagMap[w.WatchList[i].TagId]
  44. //}
  45. }
  46. func (w *WatchConfig) StoreV2(channel, appid int32) (err error) {
  47. marshal, _ := json.Marshal(w)
  48. if channel > 0 && appid > 0 {
  49. err = rds.Redis.HSet(WXBROADCASTWATCHSPE, fmt.Sprintf("%v_%v", channel, appid), string(marshal)).Err()
  50. } else {
  51. err = rds.Redis.Set(WXBROADCASTWATCH, string(marshal), -1).Err()
  52. }
  53. return err
  54. }
  55. func (w *WatchConfig) LoadV2(channel, appid int32, reload bool) error {
  56. var val string
  57. if channel > 0 && appid > 0 {
  58. val = rds.Redis.HGet(WXBROADCASTWATCHSPE, fmt.Sprintf("%v_%v", channel, appid)).Val()
  59. }
  60. if (reload && val == "") || (channel == 0 && appid == 0) { //如果是旧的或者是配置通用的
  61. val = rds.Redis.Get(WXBROADCASTWATCH).Val()
  62. }
  63. if val != "" {
  64. json.Unmarshal([]byte(val), w)
  65. }
  66. if len(w.WatchList) == 0 {
  67. val = rds.Redis.Get(WXBROADCASTWATCH).Val()
  68. }
  69. return nil
  70. }