feat: 新增CDK兑换冠名接口
This commit is contained in:
115
modules/player/controller/app/cdk.go
Normal file
115
modules/player/controller/app/cdk.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"blazing/cool"
|
||||
configservice "blazing/modules/config/service"
|
||||
playerservice "blazing/modules/player/service"
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
)
|
||||
|
||||
type CdkController struct {
|
||||
*cool.Controller
|
||||
}
|
||||
|
||||
func init() {
|
||||
controller := &CdkController{
|
||||
&cool.Controller{
|
||||
Prefix: "/seer/game/cdk",
|
||||
Api: []string{},
|
||||
Service: configservice.NewCdkService(),
|
||||
},
|
||||
}
|
||||
cool.RegisterController(controller)
|
||||
}
|
||||
|
||||
type DonationServerListReq struct {
|
||||
g.Meta `path:"/donation/serverIds" method:"GET"`
|
||||
UserID uint32 `json:"user_id" v:"required|min:1#用户ID不能为空|用户ID非法"`
|
||||
Session string `json:"session" v:"required#session不能为空"`
|
||||
}
|
||||
|
||||
type DonationRedeemReq struct {
|
||||
g.Meta `path:"/donation/redeem" method:"POST"`
|
||||
UserID uint32 `json:"user_id" v:"required|min:1#用户ID不能为空|用户ID非法"`
|
||||
Session string `json:"session" v:"required#session不能为空"`
|
||||
CDKCode string `json:"cdk_code" v:"required#CDK不能为空"`
|
||||
ServerID uint32 `json:"server_id" v:"required|min:1#服务器ID不能为空|服务器ID非法"`
|
||||
ServerName string `json:"server_name" v:"required#服务器名称不能为空"`
|
||||
}
|
||||
|
||||
type DonationRedeemRes struct {
|
||||
ServerID uint32 `json:"server_id"`
|
||||
ServerName string `json:"server_name"`
|
||||
OwnerID uint32 `json:"owner_id"`
|
||||
ExpireTime time.Time `json:"expire_time"`
|
||||
}
|
||||
|
||||
func (c *CdkController) DonationServerIDs(ctx context.Context, req *DonationServerListReq) (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
|
||||
}
|
||||
|
||||
return cool.Ok(g.Map{
|
||||
"server_ids": configservice.NewServerService().GetDonationAvailableServerIDs(),
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (c *CdkController) DonationRedeem(ctx context.Context, req *DonationRedeemReq) (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
|
||||
}
|
||||
|
||||
cdkCode := strings.TrimSpace(req.CDKCode)
|
||||
if cdkCode == "" {
|
||||
return cool.Fail("CDK不能为空"), nil
|
||||
}
|
||||
|
||||
serverName := strings.TrimSpace(req.ServerName)
|
||||
serverName = strings.Trim(cool.Filter.Replace(serverName, '*'), "*")
|
||||
if serverName == "" {
|
||||
return cool.Fail("服务器名称不能为空"), nil
|
||||
}
|
||||
|
||||
cdkService := configservice.NewCdkService()
|
||||
cdkInfo := cdkService.Get(cdkCode)
|
||||
if cdkInfo == nil {
|
||||
return cool.Fail("CDK不存在或已被使用"), nil
|
||||
}
|
||||
if cdkInfo.CDKType != configservice.CDKTypeServerNaming {
|
||||
return cool.Fail("CDK类型不匹配"), nil
|
||||
}
|
||||
if cdkInfo.BindUserId != 0 && cdkInfo.BindUserId != req.UserID {
|
||||
return cool.Fail("CDK已绑定其他用户"), nil
|
||||
}
|
||||
if !cdkInfo.ValidEndTime.IsZero() && cdkInfo.ValidEndTime.Before(time.Now()) {
|
||||
return cool.Fail("CDK已过期"), nil
|
||||
}
|
||||
|
||||
playerCdkService := playerservice.NewCdkService(req.UserID)
|
||||
if !playerCdkService.CanGet(uint32(cdkInfo.ID)) {
|
||||
return cool.Fail("CDK已领取"), nil
|
||||
}
|
||||
|
||||
serverInfo, useErr := cdkService.UseServerNamingCDK(ctx, cdkCode, req.UserID, req.ServerID, serverName)
|
||||
if useErr != nil {
|
||||
return cool.Fail(useErr.Error()), nil
|
||||
}
|
||||
|
||||
playerCdkService.Log(uint32(cdkInfo.ID))
|
||||
return cool.Ok(&DonationRedeemRes{
|
||||
ServerID: serverInfo.OnlineID,
|
||||
ServerName: serverInfo.Name,
|
||||
OwnerID: serverInfo.Owner,
|
||||
ExpireTime: serverInfo.ExpireTime,
|
||||
}), nil
|
||||
}
|
||||
Reference in New Issue
Block a user