update:ranking/list

This commit is contained in:
xiabin 2025-01-21 10:02:04 +08:00
parent 38c3b17952
commit 1fba083683
8 changed files with 74 additions and 121 deletions

View File

@ -15,6 +15,11 @@ type RankingData {
Self bool `json:"self" db:"-"` // 是否是自己 Self bool `json:"self" db:"-"` // 是否是自己
} }
type RankingResponse {
Base
RankingData []RankingData `json:"data"`
}
@server( @server(
group: game group: game

View File

@ -1,29 +0,0 @@
package douyin
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"youtu_server/game_open_api/internal/logic/douyin"
"youtu_server/game_open_api/internal/svc"
"youtu_server/game_open_api/internal/types"
)
func DouyinCode2UserIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DouyinCode2TokenRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := douyin.NewDouyinCode2UserIdLogic(r.Context(), svcCtx)
resp, err := l.DouyinCode2UserId(&req)
if err != nil {
resp.Code = -1
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

@ -20,6 +20,7 @@ func DouyinCode2tokenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := douyin.NewDouyinCode2tokenLogic(r.Context(), svcCtx) l := douyin.NewDouyinCode2tokenLogic(r.Context(), svcCtx)
resp, err := l.DouyinCode2token(&req) resp, err := l.DouyinCode2token(&req)
if err != nil { if err != nil {
resp.Code = -1
httpx.ErrorCtx(r.Context(), w, err) httpx.ErrorCtx(r.Context(), w, err)
} else { } else {
httpx.OkJsonCtx(r.Context(), w, resp) httpx.OkJsonCtx(r.Context(), w, resp)

View File

@ -1,79 +0,0 @@
package douyin
import (
"context"
"github.com/golang-jwt/jwt/v4"
"time"
"youtu_server/game_open_api/internal/app_api_helper"
"youtu_server/game_open_api/model"
"youtu_server/game_open_api/internal/svc"
"youtu_server/game_open_api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DouyinCode2UserIdLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDouyinCode2UserIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DouyinCode2UserIdLogic {
app_api_helper.Init(ctx, svcCtx.AppAccount)
return &DouyinCode2UserIdLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DouyinCode2UserIdLogic) DouyinCode2UserId(req *types.DouyinCode2TokenRequest) (resp *types.Auth, err error) {
resp = new(types.Auth)
douyinCli, err := app_api_helper.DouyinCli.GetDouYinOpenApi(req.AppId)
if err != nil {
return
}
res, err := douyinCli.Api.Code2Session(req.Code, req.AnonymousCode)
if err != nil {
return
}
if res.Errcode != 0 {
resp.Message = res.Errmsg
return
}
accountId, err := l.svcCtx.AppAccount.FindIdByAppId(l.ctx, req.AppId)
if err != nil {
return
}
aui := &model.AppUser{
AppAccountId: accountId,
Openid: res.Openid,
Unionid: res.Unionid,
AnonymousOpenid: res.AnonymousOpenid,
}
err = l.svcCtx.AppUser.FindOrCreate(l.ctx, aui)
if err != nil {
return
}
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Unix() + l.svcCtx.Config.Auth.AccessExpire
claims["iat"] = l.svcCtx.Config.Auth.AccessExpire
claims["payload"] = &svc.AccessToken{
AppId: accountId,
UserId: aui.Id,
AppIdStr: req.AppId,
OpenId: res.Openid,
}
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
resp.Token, err = token.SignedString([]byte(l.svcCtx.Config.Auth.AccessSecret))
return
}

View File

@ -2,6 +2,10 @@ package douyin
import ( import (
"context" "context"
"github.com/golang-jwt/jwt/v4"
"time"
"youtu_server/game_open_api/internal/app_api_helper"
"youtu_server/game_open_api/model"
"youtu_server/game_open_api/internal/svc" "youtu_server/game_open_api/internal/svc"
"youtu_server/game_open_api/internal/types" "youtu_server/game_open_api/internal/types"
@ -16,6 +20,7 @@ type DouyinCode2tokenLogic struct {
} }
func NewDouyinCode2tokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DouyinCode2tokenLogic { func NewDouyinCode2tokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DouyinCode2tokenLogic {
app_api_helper.Init(ctx, svcCtx.AppAccount)
return &DouyinCode2tokenLogic{ return &DouyinCode2tokenLogic{
Logger: logx.WithContext(ctx), Logger: logx.WithContext(ctx),
ctx: ctx, ctx: ctx,
@ -24,7 +29,51 @@ func NewDouyinCode2tokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
} }
func (l *DouyinCode2tokenLogic) DouyinCode2token(req *types.DouyinCode2TokenRequest) (resp *types.Auth, err error) { func (l *DouyinCode2tokenLogic) DouyinCode2token(req *types.DouyinCode2TokenRequest) (resp *types.Auth, err error) {
// todo: add your logic here and delete this line resp = new(types.Auth)
douyinCli, err := app_api_helper.DouyinCli.GetDouYinOpenApi(req.AppId)
if err != nil {
return
}
res, err := douyinCli.Api.Code2Session(req.Code, req.AnonymousCode)
if err != nil {
return
}
if res.Errcode != 0 {
resp.Message = res.Errmsg
return
}
accountId, err := l.svcCtx.AppAccount.FindIdByAppId(l.ctx, req.AppId)
if err != nil {
return
}
aui := &model.AppUser{
AppAccountId: accountId,
Openid: res.Openid,
Unionid: res.Unionid,
AnonymousOpenid: res.AnonymousOpenid,
}
err = l.svcCtx.AppUser.FindOrCreate(l.ctx, aui)
if err != nil {
return
}
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Unix() + l.svcCtx.Config.Auth.AccessExpire
claims["iat"] = l.svcCtx.Config.Auth.AccessExpire
claims["payload"] = &svc.AccessToken{
AppId: accountId,
UserId: aui.Id,
AppIdStr: req.AppId,
OpenId: res.Openid,
}
token := jwt.New(jwt.SigningMethodHS256)
token.Claims = claims
resp.Token, err = token.SignedString([]byte(l.svcCtx.Config.Auth.AccessSecret))
return return
} }

View File

@ -22,21 +22,23 @@ func NewRankingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ranki
} }
} }
func (l *RankingListLogic) RankingList() (resp []types.RankingData, err error) { func (l *RankingListLogic) RankingList() (resp *types.RankingResponse, err error) {
resp = new(types.RankingResponse)
at, err := svc.GetCtxToken(l.ctx) at, err := svc.GetCtxToken(l.ctx)
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp, err = l.svcCtx.GameScore.GetRankList(l.ctx, at.AppId, at.UserId) resp.RankingData, err = l.svcCtx.GameScore.GetRankList(l.ctx, at.AppId, at.UserId)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var flag bool var flag bool
for i := range resp { for i := range resp.RankingData {
if resp[i].UserId == at.UserId { if resp.RankingData[i].UserId == at.UserId {
resp[i].Self = true resp.RankingData[i].Self = true
resp.RankingData = append(resp.RankingData, resp.RankingData[i])
flag = true flag = true
break break
} }
@ -44,12 +46,11 @@ func (l *RankingListLogic) RankingList() (resp []types.RankingData, err error) {
if !flag { if !flag {
userRank, err := l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId) userRank, err := l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId)
//_, _ = userRank, err
if err != nil { if err != nil {
return nil, err return nil, err
} }
userRank.Self = true userRank.Self = true
resp = append(resp, *userRank) resp.RankingData = append(resp.RankingData, userRank)
} }
return return

View File

@ -28,6 +28,11 @@ type RankingData struct {
Self bool `json:"self" db:"-"` // 是否是自己 Self bool `json:"self" db:"-"` // 是否是自己
} }
type RankingResponse struct {
Base
RankingData []RankingData `json:"data"`
}
type SetAppUserRequest struct { type SetAppUserRequest struct {
Nickname string `json:"nickname"` Nickname string `json:"nickname"`
Avatar string `json:"avatar"` Avatar string `json:"avatar"`

View File

@ -25,7 +25,7 @@ type (
GameScoreModel interface { GameScoreModel interface {
gameScoreModel gameScoreModel
GetRankList(ctx context.Context, appId uint64, userId uint64) ([]types.RankingData, error) GetRankList(ctx context.Context, appId uint64, userId uint64) ([]types.RankingData, error)
GetUserRank(ctx context.Context, appId uint64, userId uint64) (*types.RankingData, error) GetUserRank(ctx context.Context, appId uint64, userId uint64) (types.RankingData, error)
FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error) FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error)
UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error
} }
@ -58,7 +58,7 @@ func (m *defaultGameScoreModel) GetRankList(ctx context.Context, appId uint64, u
} }
} }
func (m *defaultGameScoreModel) GetUserRank(ctx context.Context, appId uint64, userId uint64) (resp *types.RankingData, err error) { func (m *defaultGameScoreModel) GetUserRank(ctx context.Context, appId uint64, userId uint64) (resp types.RankingData, err error) {
ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmUserRankPrefix, userId) ecpmGameScoreAppUserIdKey := fmt.Sprintf("%s%v", cacheEcpmUserRankPrefix, userId)
var res []types.RankingData var res []types.RankingData
err = m.QueryRowCtx(ctx, &res, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { err = m.QueryRowCtx(ctx, &res, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
@ -87,13 +87,13 @@ WHERE
switch { switch {
case err == nil: case err == nil:
if len(res) == 1 { if len(res) == 1 {
resp = &res[0] resp = res[0]
} }
return resp, nil return resp, nil
case errors.Is(err, sqlc.ErrNotFound): case errors.Is(err, sqlc.ErrNotFound):
return nil, ErrNotFound return resp, ErrNotFound
default: default:
return nil, err return resp, err
} }
} }