@@ -8,7 +8,7 @@ import ( | |||
"applet/app/utils" | |||
"code.fnuoos.com/EggPlanet/egg_models.git/src/implement" | |||
"code.fnuoos.com/EggPlanet/egg_models.git/src/model" | |||
enum "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" | |||
"code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" | |||
md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md" | |||
"encoding/json" | |||
"fmt" | |||
@@ -489,3 +489,126 @@ func IsCanGetRedPackage(c *gin.Context) { | |||
} | |||
e.OutSuc(c, resp, nil) | |||
} | |||
const HeroListRedisRankKey = "EggEnergy:HomePage:HeroList:Rank:%d" // 1.kind | |||
func HeroList(c *gin.Context) { | |||
kind := utils.StrToInt(c.DefaultQuery("kind", "1")) | |||
page := utils.StrToInt(c.DefaultQuery("page", "1")) | |||
limit := 10 | |||
start := (page - 1) * limit | |||
end := start + limit - 1 | |||
rankCacheKey := fmt.Sprintf(HeroListRedisRankKey, kind) | |||
// 1. 获取指定范围的排行 | |||
results, err := cache.ZRevRange(rankCacheKey, start, end, true) | |||
if err != nil { | |||
e.OutErr(c, e.ERR, err.Error()) | |||
return | |||
} | |||
fmt.Println(results) | |||
// 2. 查询用户数据 | |||
total := len(results) | |||
list := make([]md.HeroListNode, total/2) | |||
userIds := make([]string, total/2) | |||
for i, result := range results { | |||
if i%2 == 1 { | |||
// 奇数数据是分数 | |||
list[(i-1)/2].SumValue = string(result.([]uint8)) | |||
} else { | |||
// 偶数数据是 uid | |||
list[i/2] = md.HeroListNode{ | |||
AvatarUrl: "", | |||
SumValue: "", | |||
Uid: string(result.([]uint8)), | |||
Nickname: "", | |||
} | |||
userIds = append(userIds, string(result.([]uint8))) | |||
} | |||
} | |||
userDb := implement.NewUserDb(db.Db) | |||
users, err := userDb.UserFindByParams(map[string]interface{}{ | |||
"key": "id", | |||
"value": userIds, | |||
}) | |||
if err != nil { | |||
e.OutErr(c, e.ERR, err.Error()) | |||
return | |||
} | |||
userMap := make(map[int64]model.User, len(users)) | |||
for _, user := range users { | |||
userMap[user.Id] = user | |||
} | |||
for i, node := range list { | |||
val, ok := userMap[utils.StrToInt64(node.Uid)] | |||
if ok { | |||
list[i].Nickname = val.Nickname | |||
list[i].AvatarUrl = svc.GetOssUrl(val.Avatar) | |||
} | |||
} | |||
user := svc.GetUser(c) | |||
rank, err1 := cache.ZRevRangeByMember(rankCacheKey, utils.Int64ToStr(user.Id)) | |||
if err1 != nil { | |||
if err1.Error() != "redigo: nil returned" { | |||
e.OutErr(c, e.ERR, err1.Error()) | |||
return | |||
} | |||
} | |||
var rankNum int | |||
var scoreNum string | |||
var score []interface{} | |||
var err2 error | |||
if rank != nil { | |||
score, err2 = cache.ZScoreByMember(rankCacheKey, utils.Int64ToStr(user.Id)) | |||
if err2 != nil { | |||
if err2.Error() != "redigo: nil returned" { | |||
e.OutErr(c, e.ERR, err2.Error()) | |||
return | |||
} | |||
} | |||
rankNum = utils.StrToInt(string(rank[0].([]uint8))) + 1 | |||
scoreNum = string(score[0].([]uint8)) | |||
} else { | |||
aggregationDb := implement.NewUserVirtualCoinFlowAggregationDb(db.Db) | |||
flowAggregation, err := aggregationDb.UserVirtualCoinFlowAggregationGetOneByParams(map[string]interface{}{ | |||
"key": "uid", | |||
"value": user.Id, | |||
}) | |||
if err != nil { | |||
e.OutErr(c, e.ERR_DB_ORM, err.Error()) | |||
return | |||
} | |||
if flowAggregation != nil { | |||
switch kind { | |||
case 1: | |||
scoreNum = flowAggregation.TodayData | |||
case 2: | |||
scoreNum = flowAggregation.ThisWeekData | |||
case 3: | |||
scoreNum = flowAggregation.ThisMonthData | |||
} | |||
} else { | |||
scoreNum = "0" | |||
} | |||
} | |||
selfRank := md.SelfHeroListNode{ | |||
AvatarUrl: svc.GetOssUrl(user.Avatar), | |||
SumValue: scoreNum, | |||
Uid: user.Id, | |||
Nickname: user.Nickname, | |||
Rank: rankNum, | |||
} | |||
resp := md.HeroListResp{ | |||
Total: 100, | |||
Page: page, | |||
Limit: 10, | |||
SelfRank: selfRank, | |||
List: list, | |||
} | |||
e.OutSuc(c, resp, nil) | |||
} |
@@ -36,3 +36,26 @@ type IsCanSignInResp struct { | |||
type IsCanGetRedPackageResp struct { | |||
IsCan bool `json:"is_can_get_red_package"` // 能否获取红包 | |||
} | |||
type HeroListNode struct { | |||
AvatarUrl string `json:"avatar_url"` // 头像 | |||
SumValue string `json:"sum_value"` // 期内获得能量值 | |||
Uid string `json:"uid"` | |||
Nickname string `json:"nickname"` // 昵称 | |||
} | |||
type SelfHeroListNode struct { | |||
AvatarUrl string `json:"avatar_url"` // 头像 | |||
SumValue string `json:"sum_value"` // 期内获得能量值 | |||
Uid int64 `json:"uid"` | |||
Nickname string `json:"nickname"` // 昵称 | |||
Rank int `json:"rank"` // 排行(为0时显示为未上榜) | |||
} | |||
type HeroListResp struct { | |||
Total int `json:"total"` | |||
Page int `json:"page"` | |||
Limit int `json:"limit"` | |||
SelfRank SelfHeroListNode `json:"self_rank"` // 当前用户排行 | |||
List []HeroListNode `json:"list"` | |||
} |
@@ -108,6 +108,7 @@ func route(r *gin.RouterGroup) { | |||
rHomePage.GET("/realTimePrice", hdl.RealTimePrice) // 主页-实时数据 | |||
rHomePage.GET("/isCanSignIn", hdl.IsCanSignIn) // 主页-是否可以签到 | |||
rHomePage.GET("/isCanGetRedPackage", hdl.IsCanGetRedPackage) // 主页-是否可以获得红包 | |||
rHomePage.GET("/heroList", hdl.HeroList) // 主页-英雄榜 | |||
} | |||
rAddFriend := r.Group("/addFriend") | |||
@@ -208,6 +208,18 @@ func ZRange(key string, start int, end int, withScores bool) ([]interface{}, err | |||
} | |||
return redigo.Values(Do("ZRANGE", key, start, end)) | |||
} | |||
func ZRevRange(key string, start int, end int, withScores bool) ([]interface{}, error) { | |||
if withScores { | |||
return redigo.Values(Do("ZREVRANGE", key, start, end, "WITHSCORES")) | |||
} | |||
return redigo.Values(Do("ZREVRANGE", key, start, end)) | |||
} | |||
func ZRevRangeByMember(key string, member string) ([]interface{}, error) { | |||
return redigo.Values(Do("ZRANK", key, member)) | |||
} | |||
func ZScoreByMember(key string, member string) ([]interface{}, error) { | |||
return redigo.Values(Do("ZSCORE", key, member)) | |||
} | |||
func ZRemRangeByScore(key string, start int64, end int64) ([]interface{}, error) { | |||
return redigo.Values(Do("ZREMRANGEBYSCORE", key, start, end)) | |||
} | |||
@@ -33,7 +33,7 @@ require ( | |||
) | |||
require ( | |||
code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241224014721-42a6059f0dfa | |||
code.fnuoos.com/EggPlanet/egg_models.git v0.2.1-0.20241224090637-89a57f7fbb1e | |||
code.fnuoos.com/EggPlanet/egg_system_rules.git v0.0.4-0.20241223102933-953e3394de41 | |||
code.fnuoos.com/go_rely_warehouse/zyos_go_es.git v1.0.1-0.20241118083738-0f22da9ba0be | |||
code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5 | |||