diff --git a/game_open_api/game.api b/game_open_api/game.api index 187d95d..285b515 100644 --- a/game_open_api/game.api +++ b/game_open_api/game.api @@ -15,6 +15,11 @@ type RankingData { Self bool `json:"self" db:"-"` // 是否是自己 } +type RankingResponse { + Base + RankingData []RankingData `json:"data"` +} + @server( group: game diff --git a/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go b/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go deleted file mode 100644 index d1597c8..0000000 --- a/game_open_api/internal/handler/douyin/douyinCode2UserIdHandler.go +++ /dev/null @@ -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) - } - } -} diff --git a/game_open_api/internal/handler/douyin/douyinCode2tokenHandler.go b/game_open_api/internal/handler/douyin/douyinCode2tokenHandler.go index 04ac5df..abac0a5 100644 --- a/game_open_api/internal/handler/douyin/douyinCode2tokenHandler.go +++ b/game_open_api/internal/handler/douyin/douyinCode2tokenHandler.go @@ -20,6 +20,7 @@ func DouyinCode2tokenHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { l := douyin.NewDouyinCode2tokenLogic(r.Context(), svcCtx) resp, err := l.DouyinCode2token(&req) if err != nil { + resp.Code = -1 httpx.ErrorCtx(r.Context(), w, err) } else { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go b/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go deleted file mode 100644 index c361295..0000000 --- a/game_open_api/internal/logic/douyin/douyinCode2UserIdLogic.go +++ /dev/null @@ -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 -} diff --git a/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go b/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go index b835d1c..b746269 100644 --- a/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go +++ b/game_open_api/internal/logic/douyin/douyinCode2tokenLogic.go @@ -2,6 +2,10 @@ 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" @@ -16,6 +20,7 @@ type DouyinCode2tokenLogic struct { } func NewDouyinCode2tokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DouyinCode2tokenLogic { + app_api_helper.Init(ctx, svcCtx.AppAccount) return &DouyinCode2tokenLogic{ Logger: logx.WithContext(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) { - // 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 } diff --git a/game_open_api/internal/logic/game/rankingListLogic.go b/game_open_api/internal/logic/game/rankingListLogic.go index 1c1aa8d..bb9166a 100644 --- a/game_open_api/internal/logic/game/rankingListLogic.go +++ b/game_open_api/internal/logic/game/rankingListLogic.go @@ -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) if err != nil { 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 { return nil, err } var flag bool - for i := range resp { - if resp[i].UserId == at.UserId { - resp[i].Self = true + for i := range resp.RankingData { + if resp.RankingData[i].UserId == at.UserId { + resp.RankingData[i].Self = true + resp.RankingData = append(resp.RankingData, resp.RankingData[i]) flag = true break } @@ -44,12 +46,11 @@ func (l *RankingListLogic) RankingList() (resp []types.RankingData, err error) { if !flag { userRank, err := l.svcCtx.GameScore.GetUserRank(l.ctx, at.AppId, at.UserId) - //_, _ = userRank, err if err != nil { return nil, err } userRank.Self = true - resp = append(resp, *userRank) + resp.RankingData = append(resp.RankingData, userRank) } return diff --git a/game_open_api/internal/types/types.go b/game_open_api/internal/types/types.go index 0bac83f..2753018 100644 --- a/game_open_api/internal/types/types.go +++ b/game_open_api/internal/types/types.go @@ -28,6 +28,11 @@ type RankingData struct { Self bool `json:"self" db:"-"` // 是否是自己 } +type RankingResponse struct { + Base + RankingData []RankingData `json:"data"` +} + type SetAppUserRequest struct { Nickname string `json:"nickname"` Avatar string `json:"avatar"` diff --git a/game_open_api/model/gamescoremodel.go b/game_open_api/model/gamescoremodel.go index 27a9481..4ad2694 100644 --- a/game_open_api/model/gamescoremodel.go +++ b/game_open_api/model/gamescoremodel.go @@ -25,7 +25,7 @@ type ( GameScoreModel interface { gameScoreModel 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) 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) var res []types.RankingData err = m.QueryRowCtx(ctx, &res, ecpmGameScoreAppUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error { @@ -87,13 +87,13 @@ WHERE switch { case err == nil: if len(res) == 1 { - resp = &res[0] + resp = res[0] } return resp, nil case errors.Is(err, sqlc.ErrNotFound): - return nil, ErrNotFound + return resp, ErrNotFound default: - return nil, err + return resp, err } }