Browse Source

fix: update hero list failed to get self rank

herolist
shenjiachi 2 weeks ago
parent
commit
f3100aa341
6 changed files with 209 additions and 42 deletions
  1. +49
    -37
      app/hdl/hdl_home_page.go
  2. +1
    -1
      app/md/md_home_page.go
  3. +4
    -4
      app/utils/cache/redis.go
  4. +58
    -0
      docs/docs.go
  5. +58
    -0
      docs/swagger.json
  6. +39
    -0
      docs/swagger.yaml

+ 49
- 37
app/hdl/hdl_home_page.go View File

@@ -491,6 +491,18 @@ func IsCanGetRedPackage(c *gin.Context) {
}

const HeroListRedisRankKey = "EggEnergy:HomePage:HeroList:Rank:%d" // 1.kind
// HeroList
// @Summary 蛋蛋星球-主页-英雄榜(获取)
// @Tags 主页
// @Description 英雄榜(获取)
// @Accept json
// @Produce json
// @param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param kind query string true "时间类型:1.天 2.周 3.月"
// @Param page query string true "页数"
// @Success 200 {object} md.IsCanGetRedPackageResp "具体数据"
// @Failure 400 {object} md.Response "具体错误"
// @Router /api/v1/homePage/heroList [get]
func HeroList(c *gin.Context) {
kind := utils.StrToInt(c.DefaultQuery("kind", "1"))
page := utils.StrToInt(c.DefaultQuery("page", "1"))
@@ -505,9 +517,8 @@ func HeroList(c *gin.Context) {
e.OutErr(c, e.ERR, err.Error())
return
}
fmt.Println(results)

// 2. 查询用户数据
// 2. 查询排行内用户头像和昵称
total := len(results)
list := make([]md.HeroListNode, total/2)
userIds := make([]string, total/2)
@@ -548,59 +559,60 @@ func HeroList(c *gin.Context) {
}
}

// 3. 查询当前用户排名信息
var rank int64
var score string
var err1 error
user := svc.GetUser(c)
rank, err1 := cache.ZRevRangeByMember(rankCacheKey, utils.Int64ToStr(user.Id))
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
} else {
// 3.1 当前用户不在排行榜上
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:
score = flowAggregation.TodayData
case 2:
score = flowAggregation.ThisWeekData
case 3:
score = flowAggregation.ThisMonthData
}
} else {
score = "0"
}
}
}
var rankNum int
var scoreNum string
var score []interface{}
var err2 error
if rank != nil {
score, err2 = cache.ZScoreByMember(rankCacheKey, utils.Int64ToStr(user.Id))
} else {
// 3.2 该用户在排行榜上
scoreNum, 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"
}

// redis 排名从 0 开始
rank++
score = utils.Float64ToStr(scoreNum)
}

selfRank := md.SelfHeroListNode{
AvatarUrl: svc.GetOssUrl(user.Avatar),
SumValue: scoreNum,
SumValue: score,
Uid: user.Id,
Nickname: user.Nickname,
Rank: rankNum,
Rank: rank,
}

resp := md.HeroListResp{


+ 1
- 1
app/md/md_home_page.go View File

@@ -49,7 +49,7 @@ type SelfHeroListNode struct {
SumValue string `json:"sum_value"` // 期内获得能量值
Uid int64 `json:"uid"`
Nickname string `json:"nickname"` // 昵称
Rank int `json:"rank"` // 排行(为0时显示为未上榜)
Rank int64 `json:"rank"` // 排行(为0时显示为未上榜)
}

type HeroListResp struct {


+ 4
- 4
app/utils/cache/redis.go View File

@@ -214,11 +214,11 @@ func ZRevRange(key string, start int, end int, withScores bool) ([]interface{},
}
return redigo.Values(Do("ZREVRANGE", key, start, end))
}
func ZRevRangeByMember(key string, member string) ([]interface{}, error) {
return redigo.Values(Do("ZRANK", key, member))
func ZRevRangeByMember(key string, member string) (int64, error) {
return redigo.Int64(Do("ZREVRANK", key, member))
}
func ZScoreByMember(key string, member string) ([]interface{}, error) {
return redigo.Values(Do("ZSCORE", key, member))
func ZScoreByMember(key string, member string) (float64, error) {
return redigo.Float64(Do("ZSCORE", key, member))
}
func ZRemRangeByScore(key string, start int64, end int64) ([]interface{}, error) {
return redigo.Values(Do("ZREMRANGEBYSCORE", key, start, end))


+ 58
- 0
docs/docs.go View File

@@ -1994,6 +1994,58 @@ const docTemplate = `{
}
}
},
"/api/v1/homePage/heroList": {
"get": {
"description": "英雄榜(获取)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"主页"
],
"summary": "蛋蛋星球-主页-英雄榜(获取)",
"parameters": [
{
"type": "string",
"description": "验证参数Bearer和token空格拼接",
"name": "Authorization",
"in": "header",
"required": true
},
{
"type": "string",
"description": "时间类型:1.天 2.周 3.月",
"name": "kind",
"in": "query",
"required": true
},
{
"type": "string",
"description": "页数",
"name": "page",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "具体数据",
"schema": {
"$ref": "#/definitions/md.IsCanGetRedPackageResp"
}
},
"400": {
"description": "具体错误",
"schema": {
"$ref": "#/definitions/md.Response"
}
}
}
}
},
"/api/v1/homePage/index": {
"get": {
"description": "基础信息(获取)",
@@ -6683,6 +6735,9 @@ const docTemplate = `{
"content": {
"type": "string"
},
"down_type": {
"type": "string"
},
"img": {
"type": "string"
},
@@ -6692,6 +6747,9 @@ const docTemplate = `{
"name": {
"type": "string"
},
"out_url": {
"type": "string"
},
"type": {
"type": "string"
},


+ 58
- 0
docs/swagger.json View File

@@ -1988,6 +1988,58 @@
}
}
},
"/api/v1/homePage/heroList": {
"get": {
"description": "英雄榜(获取)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"主页"
],
"summary": "蛋蛋星球-主页-英雄榜(获取)",
"parameters": [
{
"type": "string",
"description": "验证参数Bearer和token空格拼接",
"name": "Authorization",
"in": "header",
"required": true
},
{
"type": "string",
"description": "时间类型:1.天 2.周 3.月",
"name": "kind",
"in": "query",
"required": true
},
{
"type": "string",
"description": "页数",
"name": "page",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "具体数据",
"schema": {
"$ref": "#/definitions/md.IsCanGetRedPackageResp"
}
},
"400": {
"description": "具体错误",
"schema": {
"$ref": "#/definitions/md.Response"
}
}
}
}
},
"/api/v1/homePage/index": {
"get": {
"description": "基础信息(获取)",
@@ -6677,6 +6729,9 @@
"content": {
"type": "string"
},
"down_type": {
"type": "string"
},
"img": {
"type": "string"
},
@@ -6686,6 +6741,9 @@
"name": {
"type": "string"
},
"out_url": {
"type": "string"
},
"type": {
"type": "string"
},


+ 39
- 0
docs/swagger.yaml View File

@@ -1639,12 +1639,16 @@ definitions:
properties:
content:
type: string
down_type:
type: string
img:
type: string
is_must:
type: string
name:
type: string
out_url:
type: string
type:
type: string
url:
@@ -3143,6 +3147,41 @@ paths:
summary: 蛋蛋星球-主页-视频奖励规则(获取)
tags:
- 主页
/api/v1/homePage/heroList:
get:
consumes:
- application/json
description: 英雄榜(获取)
parameters:
- description: 验证参数Bearer和token空格拼接
in: header
name: Authorization
required: true
type: string
- description: 时间类型:1.天 2.周 3.月
in: query
name: kind
required: true
type: string
- description: 页数
in: query
name: page
required: true
type: string
produces:
- application/json
responses:
"200":
description: 具体数据
schema:
$ref: '#/definitions/md.IsCanGetRedPackageResp'
"400":
description: 具体错误
schema:
$ref: '#/definitions/md.Response'
summary: 蛋蛋星球-主页-英雄榜(获取)
tags:
- 主页
/api/v1/homePage/index:
get:
consumes:


Loading…
Cancel
Save