This commit is contained in:
xiabin 2025-01-20 17:36:03 +08:00
parent e2bff25b45
commit 22ec38db77
20 changed files with 105 additions and 31 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea
data/
game_open_api/logs/

View File

@ -19,7 +19,7 @@ services:
- ./data/mysql/data:/var/lib/mysql
- ./data/mysql/logs:/var/log/mysql
##初始化的脚本初始化我们存放的init.sql文件
- ./sql:/docker-entrypoint-initdb.d/
- ./data/sql:/docker-entrypoint-initdb.d/
- ./data/mysql/conf:/etc/mysql/conf.d
ports:
- "3306:3306"

View File

@ -4,6 +4,7 @@ import (
"game.api"
"douyin.api"
"wechat.api"
"common.api"
)
type UserId {
@ -24,6 +25,6 @@ type SetAppUserRequest {
)
service game_open_api-api {
@handler appUserSetUser
post /set_app_account (SetAppUserRequest) returns (UserId)
post /set_app_account (SetAppUserRequest) returns (Base)
}

View File

@ -1,6 +0,0 @@
syntax = "v1"
type Auth {
Token string `json:"token"`
}

12
game_open_api/common.api Normal file
View File

@ -0,0 +1,12 @@
syntax = "v1"
type Auth {
Base
Token string `json:"token"`
}
type Base {
Code int `json:"code"`
Message string `json:"message,omitempty"`
}

View File

@ -1,6 +1,6 @@
syntax = "v1"
import "auth.api"
import "common.api"
type DouyinCode2UserIdRequest {
AppId string `form:"appId"`

View File

@ -9,4 +9,10 @@ Cache:
Auth:
AccessSecret: youtu123!
AccessExpire: 3600
AccessExpire: 86400
Log:
Level: debug
# Mode: file
Path: ./logs
MaxSize: 100

View File

@ -1,5 +1,7 @@
syntax = "v1"
import "common.api"
type SetUserGameScoreRequest {
Score uint64 `json:"score"`
}
@ -27,6 +29,6 @@ service game_open_api-api {
get /ranking/list returns ([]RankingData)
@handler rankingSetScore
post /ranking/set_score (SetUserGameScoreRequest)
post /ranking/set_score (SetUserGameScoreRequest) returns (Base)
}

View File

@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
"youtu_server/game_open_api/internal/config"
"youtu_server/game_open_api/internal/handler"
"youtu_server/game_open_api/internal/svc"

View File

@ -20,6 +20,7 @@ func AppUserSetUserHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := app_user.NewAppUserSetUserLogic(r.Context(), svcCtx)
resp, err := l.AppUserSetUser(&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 DouyinCode2UserIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
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

@ -18,11 +18,12 @@ func RankingSetScoreHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
}
l := game.NewRankingSetScoreLogic(r.Context(), svcCtx)
err := l.RankingSetScore(&req)
resp, err := l.RankingSetScore(&req)
if err != nil {
resp.Code = -1
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.Ok(w)
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}

View File

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

View File

@ -24,7 +24,8 @@ func NewAppUserSetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ap
}
}
func (l *AppUserSetUserLogic) AppUserSetUser(req *types.SetAppUserRequest) (resp *types.UserId, err error) {
func (l *AppUserSetUserLogic) AppUserSetUser(req *types.SetAppUserRequest) (resp *types.Base, err error) {
resp = &types.Base{}
at, err := svc.GetCtxToken(l.ctx)
if err != nil {
return

View File

@ -2,7 +2,6 @@ package douyin
import (
"context"
"errors"
"github.com/golang-jwt/jwt/v4"
"time"
"youtu_server/game_open_api/internal/app_api_helper"
@ -43,7 +42,7 @@ func (l *DouyinCode2UserIdLogic) DouyinCode2UserId(req *types.DouyinCode2UserIdR
}
if res.Errcode != 0 {
err = errors.New(res.Errmsg)
resp.Message = res.Errmsg
return
}

View File

@ -2,6 +2,8 @@ package game
import (
"context"
"errors"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"youtu_server/game_open_api/model"
"youtu_server/game_open_api/internal/svc"
@ -24,16 +26,31 @@ func NewRankingSetScoreLogic(ctx context.Context, svcCtx *svc.ServiceContext) *R
}
}
func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreRequest) error {
func (l *RankingSetScoreLogic) RankingSetScore(req *types.SetUserGameScoreRequest) (resp *types.Base, err error) {
resp = &types.Base{}
at, err := svc.GetCtxToken(l.ctx)
if err != nil {
return err
return
}
_, err = l.svcCtx.GameScore.Insert(l.ctx, &model.GameScore{
AppUserId: at.UserId,
AppAccount: at.AppId,
Score: req.Score,
})
return err
oldScore, err := l.svcCtx.GameScore.FindUserScore(l.ctx, at.AppId, at.UserId)
if err != nil && !errors.Is(err, sqlc.ErrNotFound) {
return
}
if oldScore != nil && req.Score <= oldScore.Score {
return
}
if oldScore != nil {
err = l.svcCtx.GameScore.UpdateScore(l.ctx, at.AppId, at.UserId, req.Score)
} else {
_, err = l.svcCtx.GameScore.Insert(l.ctx, &model.GameScore{
AppUserId: at.UserId,
AppAccount: at.AppId,
Score: req.Score,
})
}
return
}

View File

@ -4,9 +4,15 @@
package types
type Auth struct {
Base
Token string `json:"token"`
}
type Base struct {
Code int `json:"code"`
Message string `json:"message,omitempty"`
}
type DouyinCode2UserIdRequest struct {
AppId string `form:"appId"`
Code string `form:"code,optional"`

View File

@ -2,6 +2,7 @@ package model
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
@ -13,8 +14,9 @@ import (
var _ GameScoreModel = (*customGameScoreModel)(nil)
const (
cacheEcpmRankListPrefix = "cache:ecpm:game:rankList:appId:"
cacheEcpmUserRankPrefix = "cache:ecpm:game:userRank:userId:"
cacheEcpmRankListPrefix = "cache:ecpm:game:rankList:appId:"
cacheEcpmUserRankPrefix = "cache:ecpm:game:userRank:userId:"
cacheEcpmGameScoreAppUserIdAppAccountPrefix = "cache:ecpm:gameScore:"
)
type (
@ -24,6 +26,8 @@ type (
gameScoreModel
GetRankList(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)
UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error
}
customGameScoreModel struct {
@ -92,3 +96,32 @@ WHERE
return nil, err
}
}
func (m *defaultGameScoreModel) FindUserScore(ctx context.Context, appId uint64, userId uint64) (*GameScore, error) {
ecpmGameScoreAppUserIdKey := fmt.Sprintf("%sappId:%d:userId:%d", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appId, userId)
var (
resp GameScore
err error
)
err = m.QueryRowCtx(ctx, &resp, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `app_user_id` = ? and app_account = ? limit 1", gameScoreRows, m.table)
return conn.QueryRowCtx(ctx, v, query, userId, appId)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultGameScoreModel) UpdateScore(ctx context.Context, appId uint64, userId uint64, score uint64) error {
ecpmGameScoreAppUserIdAppAccountKey := fmt.Sprintf("%sappId:%d:userId:%d", cacheEcpmGameScoreAppUserIdAppAccountPrefix, appId, userId)
_, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set `score` = ? where `app_account` = ? and `app_user_id` = ?", m.table)
return conn.ExecCtx(ctx, query, score, appId, userId)
}, ecpmGameScoreAppUserIdAppAccountKey)
return err
}

View File

@ -23,8 +23,7 @@ var (
gameScoreRowsExpectAutoSet = strings.Join(stringx.Remove(gameScoreFieldNames, "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
gameScoreRowsWithPlaceHolder = strings.Join(stringx.Remove(gameScoreFieldNames, "`app_user_id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheEcpmGameScoreAppUserIdPrefix = "cache:ecpm:gameScore:appUserId:"
cacheEcpmGameScoreAppUserIdAppAccountPrefix = "cache:ecpm:gameScore:appUserId:appAccount:"
cacheEcpmGameScoreAppUserIdPrefix = "cache:ecpm:gameScore:appUserId:"
)
type (

View File

@ -1,6 +1,6 @@
syntax = "v1"
import "auth.api"
import "common.api"
type WechatCode2UserIdRequest {