Files
bl/modules/player/controller/app/sign.go

105 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"blazing/cool"
configservice "blazing/modules/config/service"
playerservice "blazing/modules/player/service"
"context"
"fmt"
"strings"
"github.com/deatil/go-cryptobin/cryptobin/crypto"
"github.com/gogf/gf/v2/frame/g"
)
type SignController struct {
*cool.Controller
}
func init() {
controller := &SignController{
&cool.Controller{
Prefix: "/seer/game/sign",
Api: []string{},
Service: configservice.NewSignInService(),
},
}
cool.RegisterController(controller)
}
type SignStateReq struct {
g.Meta `path:"/state" method:"GET"`
UserID uint32 `json:"user_id" v:"required|min:1#用户ID不能为空|用户ID非法"`
Session string `json:"session" v:"required#session不能为空"`
}
type SignClaimReq struct {
g.Meta `path:"/claim" method:"POST"`
UserID uint32 `json:"user_id" v:"required|min:1#用户ID不能为空|用户ID非法"`
Session string `json:"session" v:"required#session不能为空"`
}
func (c *SignController) State(ctx context.Context, req *SignStateReq) (res *cool.BaseRes, err error) {
if err = g.Validator().Data(req).Run(ctx); err != nil {
return cool.Fail(err.Error()), nil
}
if err = validateGameSession(req.UserID, req.Session); err != nil {
return cool.Fail(err.Error()), nil
}
state, err := playerservice.NewSignService(req.UserID).GetState()
if err != nil {
return cool.Fail(err.Error()), nil
}
return cool.Ok(state), nil
}
func (c *SignController) Claim(ctx context.Context, req *SignClaimReq) (res *cool.BaseRes, err error) {
if err = g.Validator().Data(req).Run(ctx); err != nil {
return cool.Fail(err.Error()), nil
}
if err = validateGameSession(req.UserID, req.Session); err != nil {
return cool.Fail(err.Error()), nil
}
result, err := playerservice.NewSignService(req.UserID).Claim()
if err != nil {
return cool.Fail(err.Error()), nil
}
return cool.Ok(result), nil
}
func validateGameSession(userID uint32, session string) error {
if userID == 0 {
return fmt.Errorf("user_id不能为空")
}
session = strings.TrimSpace(session)
if session == "" {
return fmt.Errorf("session不能为空")
}
cached, err := cool.CacheManager.Get(context.Background(), fmt.Sprintf("session:%d", userID))
if err != nil || cached.IsEmpty() {
return fmt.Errorf("session已过期请重新登录")
}
rawSession := session
decrypted := crypto.
FromBase64String(session).
SetKey("gfertf12dfertf12").
SetIv("gfertf12dfertf12").
Aes().
CBC().
PKCS7Padding().
Decrypt().
ToString()
if decrypted != "" {
rawSession = decrypted
}
if rawSession != cached.String() {
return fmt.Errorf("session无效请重新登录")
}
return nil
}