This commit is contained in:
@@ -15,6 +15,7 @@ type CdkController struct {
|
||||
*cool.Controller
|
||||
}
|
||||
|
||||
// init 注册用户态CDK相关Web接口。
|
||||
func init() {
|
||||
controller := &CdkController{
|
||||
&cool.Controller{
|
||||
@@ -27,33 +28,29 @@ func init() {
|
||||
}
|
||||
|
||||
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不能为空"`
|
||||
g.Meta `path:"/donation/serverIds" method:"GET"`
|
||||
}
|
||||
|
||||
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"`
|
||||
ServerID uint32 `json:"server_id"`
|
||||
ServerName string `json:"server_name"`
|
||||
OwnerID uint32 `json:"owner_id"`
|
||||
ServerExpireTime time.Time `json:"server_expire_time"`
|
||||
CDKExpireTime time.Time `json:"cdk_expire_time"`
|
||||
}
|
||||
|
||||
// DonationServerIDs 查询当前可用于服务器绑定CDK的服务器ID列表。
|
||||
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
|
||||
admin := cool.GetAdmin(ctx)
|
||||
if admin == nil || admin.UserId == 0 {
|
||||
return cool.Fail("未登录或登录已失效"), nil
|
||||
}
|
||||
|
||||
return cool.Ok(g.Map{
|
||||
@@ -61,13 +58,17 @@ func (c *CdkController) DonationServerIDs(ctx context.Context, req *DonationServ
|
||||
}), nil
|
||||
}
|
||||
|
||||
// DonationRedeem 兑换服务器绑定类型CDK并完成冠名写入。
|
||||
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
|
||||
|
||||
admin := cool.GetAdmin(ctx)
|
||||
if admin == nil || admin.UserId == 0 {
|
||||
return cool.Fail("未登录或登录已失效"), nil
|
||||
}
|
||||
ownerID := uint32(admin.UserId)
|
||||
|
||||
cdkCode := strings.TrimSpace(req.CDKCode)
|
||||
if cdkCode == "" {
|
||||
@@ -88,28 +89,29 @@ func (c *CdkController) DonationRedeem(ctx context.Context, req *DonationRedeemR
|
||||
if cdkInfo.CDKType != configservice.CDKTypeServerNaming {
|
||||
return cool.Fail("CDK类型不匹配"), nil
|
||||
}
|
||||
if cdkInfo.BindUserId != 0 && cdkInfo.BindUserId != req.UserID {
|
||||
if cdkInfo.BindUserId != 0 && cdkInfo.BindUserId != ownerID {
|
||||
return cool.Fail("CDK已绑定其他用户"), nil
|
||||
}
|
||||
if !cdkInfo.ValidEndTime.IsZero() && cdkInfo.ValidEndTime.Before(time.Now()) {
|
||||
return cool.Fail("CDK已过期"), nil
|
||||
}
|
||||
|
||||
playerCdkService := playerservice.NewCdkService(req.UserID)
|
||||
playerCdkService := playerservice.NewCdkService(ownerID)
|
||||
if !playerCdkService.CanGet(uint32(cdkInfo.ID)) {
|
||||
return cool.Fail("CDK已领取"), nil
|
||||
}
|
||||
|
||||
serverInfo, useErr := cdkService.UseServerNamingCDK(ctx, cdkCode, req.UserID, req.ServerID, serverName)
|
||||
serverInfo, useErr := cdkService.UseServerNamingCDK(ctx, cdkCode, ownerID, 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,
|
||||
ServerID: serverInfo.ServerID,
|
||||
ServerName: serverInfo.ServerName,
|
||||
OwnerID: serverInfo.OwnerID,
|
||||
ServerExpireTime: serverInfo.ServerExpireTime,
|
||||
CDKExpireTime: serverInfo.CDKExpireTime,
|
||||
}), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user