123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- package v1
- import (
- "context"
- "designs/app/common/request"
- "designs/common"
- "designs/config"
- "designs/global"
- "github.com/gin-gonic/gin"
- "github.com/go-redis/redis/v8"
- "strconv"
- "strings"
- )
- /* 排行类型 */
- const (
- RankTypeByEver = 1 //永久不过期
- RankTypeByDay = 2 //日榜
- RankTypeByWeek = 3 //周榜
- RankTypeByMonth = 4 //月榜
- )
- // AddUserToBlackList 将玩家加入黑名单
- func AddUserToBlackList(c *gin.Context) {
- form := request.Check(c, &struct {
- Code string `json:"code" binding:"required"`
- Gid string `json:"gid" binding:"required"`
- Pf string `json:"pf" binding:"required"`
- }{})
- userKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.user_table_key") + form.Code
- //存在用户 读取数据
- userdata, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
- if err != nil {
- common.RetJson(1003, "用户数据不存在", err.Error(), c)
- return
- }
- curHid := userdata["hid"]
- //清除省榜数据
- rankDataKeys := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_province_table_key") + curHid + ":" + strconv.Itoa(RankTypeByEver) + ":*"
- keys, _ := global.App.Redis.Keys(context.Background(), rankDataKeys).Result()
- for _, val := range keys {
- defScore, _ := global.App.Redis.ZScore(context.Background(), val, form.Code).Result()
- lastindex := strings.LastIndex(val, ":")
- if lastindex != -1 {
- curField := val[lastindex+1:]
- //删除出省排行数据
- global.App.Redis.ZRem(context.Background(), val, form.Code)
- //处理省排行汇总数据
- rankSumDataKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_province_table_key") + "Sum" + ":" + curField
- global.App.Redis.ZIncrBy(context.Background(), rankSumDataKey, -defScore, curHid).Result()
- }
- }
- //清除排行榜数据
- rankDataKeyTotal := form.Gid + ":" + form.Pf + ":" + config.Get("app.rank_table_key") + strconv.Itoa(RankTypeByEver) + ":*"
- keysTotal, _ := global.App.Redis.Keys(context.Background(), rankDataKeyTotal).Result()
- for _, val := range keysTotal {
- lastindex := strings.LastIndex(val, ":")
- if lastindex != -1 {
- //清除排行榜数据
- global.App.Redis.ZRem(context.Background(), val, form.Code)
- }
- }
- //加入黑名单队列
- blackListKey := config.Get("app.black_list_table")
- err = global.App.Redis.ZAdd(context.Background(), blackListKey, &redis.Z{Member: userKey}).Err()
- if err != nil {
- common.RetJson(1003, "加入黑名单失败", "参数错误2"+err.Error(), c)
- return
- }
- common.RetJson(0, "加入黑名单成功", "", c)
- }
- // DeleteUserFormBlackList 将玩家从从黑名单中去掉
- func DeleteUserFormBlackList(c *gin.Context) {
- form := request.Check(c, &struct {
- Code string `json:"code" binding:"required"`
- Gid string `json:"gid" binding:"required"`
- Pf string `json:"pf" binding:"required"`
- }{})
- userKey := form.Gid + ":" + form.Pf + ":" + config.Get("app.user_table_key") + form.Code
- //存在用户 读取数据
- _, err := global.App.Redis.HGetAll(context.Background(), userKey).Result()
- if err != nil {
- common.RetJson(1003, "用户数据不存在", err.Error(), c)
- return
- }
- //从黑名单队列中删除
- blackListKey := config.Get("app.black_list_table")
- err = global.App.Redis.ZRem(context.Background(), blackListKey, userKey).Err()
- if err != nil {
- common.RetJson(1003, "从黑名单中移除失败", "参数错误2", c)
- return
- }
- common.RetJson(0, "从黑名单中移除成功", "", c)
- }
- // ReadBlackList 查看目前黑名单有哪些人
- func ReadBlackList(c *gin.Context) {
- blackListKey := config.Get("app.black_list_table")
- data, err := global.App.Redis.ZRange(context.Background(), blackListKey, 0, -1).Result()
- if err != nil {
- common.RetJson(1003, "读取黑名单列表失败", "参数错误2", c)
- return
- }
- var userData []interface{}
- for _, value := range data {
- user, err := global.App.Redis.HGetAll(context.Background(), value).Result()
- if err != nil {
- common.RetJson(1003, "GetUserData err", "", c)
- return
- }
- userData = append(userData, user)
- }
- common.RetJson(0, "读取黑名单列表成功", userData, c)
- }
|