blackList.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package v1
  2. import (
  3. "context"
  4. "designs/app/common/request"
  5. "designs/common"
  6. "designs/config"
  7. "designs/global"
  8. "github.com/gin-gonic/gin"
  9. "github.com/go-redis/redis/v8"
  10. "strconv"
  11. "strings"
  12. )
  13. /* 排行类型 */
  14. const (
  15. RankTypeByEver = 1 //永久不过期
  16. RankTypeByDay = 2 //日榜
  17. RankTypeByWeek = 3 //周榜
  18. RankTypeByMonth = 4 //月榜
  19. )
  20. // AddUserToBlackList 将玩家加入黑名单
  21. func AddUserToBlackList(c *gin.Context) {
  22. form := request.Check(c, &struct {
  23. Code string `json:"code" binding:"required"`
  24. Gid string `json:"gid" binding:"required"`
  25. Pf string `json:"pf" binding:"required"`
  26. }{})
  27. userKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.user_table_key") + form.Code
  28. //存在用户 读取数据
  29. userdata, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  30. if err != nil {
  31. common.RetJson(1003, "用户数据不存在", err.Error(), c)
  32. return
  33. }
  34. curHid := userdata["hid"]
  35. //清除省榜数据
  36. rankDataKeys := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_province_table_key") + curHid + ":" + strconv.Itoa(RankTypeByEver) + ":*"
  37. keys, _ := global.App.Redis.Keys(context.Background(), rankDataKeys).Result()
  38. for _, val := range keys {
  39. defScore, _ := global.App.Redis.ZScore(context.Background(), val, form.Code).Result()
  40. lastindex := strings.LastIndex(val, ":")
  41. if lastindex != -1 {
  42. curField := val[lastindex+1:]
  43. //删除出省排行数据
  44. global.App.Redis.ZRem(context.Background(), val, form.Code)
  45. //处理省排行汇总数据
  46. rankSumDataKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_province_table_key") + "Sum" + ":" + curField
  47. global.App.Redis.ZIncrBy(context.Background(), rankSumDataKey, -defScore, curHid).Result()
  48. }
  49. }
  50. //清除排行榜数据
  51. rankDataKeyTotal := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_table_key") + strconv.Itoa(RankTypeByEver) + ":*"
  52. keysTotal, _ := global.App.Redis.Keys(context.Background(), rankDataKeyTotal).Result()
  53. for _, val := range keysTotal {
  54. lastindex := strings.LastIndex(val, ":")
  55. if lastindex != -1 {
  56. //清除排行榜数据
  57. global.App.Redis.ZRem(context.Background(), val, form.Code)
  58. }
  59. }
  60. //加入黑名单队列
  61. blackListKey := config.Get("app.black_list_table")
  62. err = global.App.Redis.ZAdd(context.Background(), blackListKey, &redis.Z{Member: userKey}).Err()
  63. if err != nil {
  64. common.RetJson(1003, "加入黑名单失败", "参数错误2"+err.Error(), c)
  65. return
  66. }
  67. common.RetJson(0, "加入黑名单成功", "", c)
  68. }
  69. // DeleteUserFormBlackList 将玩家从从黑名单中去掉
  70. func DeleteUserFormBlackList(c *gin.Context) {
  71. form := request.Check(c, &struct {
  72. Code string `json:"code" binding:"required"`
  73. Gid string `json:"gid" binding:"required"`
  74. Pf string `json:"pf" binding:"required"`
  75. }{})
  76. userKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.user_table_key") + form.Code
  77. //存在用户 读取数据
  78. _, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
  79. if err != nil {
  80. common.RetJson(1003, "用户数据不存在", err.Error(), c)
  81. return
  82. }
  83. //从黑名单队列中删除
  84. blackListKey := config.Get("app.black_list_table")
  85. err = global.App.Redis.ZRem(context.Background(), blackListKey, userKey).Err()
  86. if err != nil {
  87. common.RetJson(1003, "从黑名单中移除失败", "参数错误2", c)
  88. return
  89. }
  90. common.RetJson(0, "从黑名单中移除成功", "", c)
  91. }
  92. // ReadBlackList 查看目前黑名单有哪些人
  93. func ReadBlackList(c *gin.Context) {
  94. blackListKey := config.Get("app.black_list_table")
  95. data, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
  96. if err != nil {
  97. common.RetJson(1003, "读取黑名单列表失败", "参数错误2", c)
  98. return
  99. }
  100. var userData []interface{}
  101. for _, value := range data {
  102. user, err := global.App.Redis.HGetAll(context.Background(), value).Result()
  103. if err != nil {
  104. common.RetJson(1003, "GetUserData err", "", c)
  105. return
  106. }
  107. userData = append(userData, user)
  108. }
  109. common.RetJson(0, "读取黑名单列表成功", userData, c)
  110. }