2026-04-08 18:12:02 +08:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 19:31:44 +08:00
|
|
|
// init 注册用户态CDK相关Web接口。
|
2026-04-08 18:12:02 +08:00
|
|
|
func init() {
|
|
|
|
|
controller := &CdkController{
|
|
|
|
|
&cool.Controller{
|
|
|
|
|
Prefix: "/seer/game/cdk",
|
|
|
|
|
Api: []string{},
|
|
|
|
|
Service: configservice.NewCdkService(),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
cool.RegisterController(controller)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DonationServerListReq struct {
|
2026-04-08 19:31:44 +08:00
|
|
|
g.Meta `path:"/donation/serverIds" method:"GET"`
|
2026-04-08 18:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-10 19:36:59 +08:00
|
|
|
type DonationServerInfoReq struct {
|
|
|
|
|
g.Meta `path:"/donation/serverInfo" method:"GET"`
|
|
|
|
|
ServerID uint32 `json:"server_id" v:"required|min:1#服务器ID不能为空|服务器ID非法"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DonationServerInfoRes struct {
|
|
|
|
|
ServerID uint32 `json:"server_id"`
|
|
|
|
|
ServerName string `json:"server_name"`
|
|
|
|
|
Remark string `json:"remark"`
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 18:12:02 +08:00
|
|
|
type DonationRedeemReq struct {
|
|
|
|
|
g.Meta `path:"/donation/redeem" method:"POST"`
|
|
|
|
|
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 {
|
2026-04-08 19:31:44 +08:00
|
|
|
ServerID uint32 `json:"server_id"`
|
|
|
|
|
ServerName string `json:"server_name"`
|
|
|
|
|
OwnerID uint32 `json:"owner_id"`
|
|
|
|
|
ServerExpireTime time.Time `json:"server_expire_time"`
|
2026-04-08 18:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-08 19:31:44 +08:00
|
|
|
// DonationServerIDs 查询当前可用于服务器绑定CDK的服务器ID列表。
|
2026-04-08 18:12:02 +08:00
|
|
|
func (c *CdkController) DonationServerIDs(ctx context.Context, req *DonationServerListReq) (res *cool.BaseRes, err error) {
|
2026-04-08 19:31:44 +08:00
|
|
|
admin := cool.GetAdmin(ctx)
|
|
|
|
|
if admin == nil || admin.UserId == 0 {
|
|
|
|
|
return cool.Fail("未登录或登录已失效"), nil
|
2026-04-08 18:12:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cool.Ok(g.Map{
|
|
|
|
|
"server_ids": configservice.NewServerService().GetDonationAvailableServerIDs(),
|
|
|
|
|
}), nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-10 19:36:59 +08:00
|
|
|
// DonationServerInfo 查询冠名兑换前展示的服务器名称与备注。
|
|
|
|
|
func (c *CdkController) DonationServerInfo(ctx context.Context, req *DonationServerInfoReq) (res *cool.BaseRes, err error) {
|
|
|
|
|
if err = g.Validator().Data(req).Run(ctx); err != nil {
|
|
|
|
|
return cool.Fail(err.Error()), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
admin := cool.GetAdmin(ctx)
|
|
|
|
|
if admin == nil || admin.UserId == 0 {
|
|
|
|
|
return cool.Fail("未登录或登录已失效"), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
server := configservice.NewServerService().GetServerID(req.ServerID)
|
|
|
|
|
if server.OnlineID == 0 {
|
|
|
|
|
return cool.Fail("服务器不存在"), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cool.Ok(&DonationServerInfoRes{
|
|
|
|
|
ServerID: server.OnlineID,
|
|
|
|
|
ServerName: server.Name,
|
|
|
|
|
Remark: server.Desc,
|
|
|
|
|
}), nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 19:31:44 +08:00
|
|
|
// DonationRedeem 兑换服务器绑定类型CDK并完成冠名写入。
|
2026-04-08 18:12:02 +08:00
|
|
|
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
|
|
|
|
|
}
|
2026-04-08 19:31:44 +08:00
|
|
|
|
|
|
|
|
admin := cool.GetAdmin(ctx)
|
|
|
|
|
if admin == nil || admin.UserId == 0 {
|
|
|
|
|
return cool.Fail("未登录或登录已失效"), nil
|
2026-04-08 18:12:02 +08:00
|
|
|
}
|
2026-04-08 19:31:44 +08:00
|
|
|
ownerID := uint32(admin.UserId)
|
2026-04-08 18:12:02 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
2026-04-08 19:31:44 +08:00
|
|
|
if cdkInfo.BindUserId != 0 && cdkInfo.BindUserId != ownerID {
|
2026-04-08 18:12:02 +08:00
|
|
|
return cool.Fail("CDK已绑定其他用户"), nil
|
|
|
|
|
}
|
|
|
|
|
if !cdkInfo.ValidEndTime.IsZero() && cdkInfo.ValidEndTime.Before(time.Now()) {
|
|
|
|
|
return cool.Fail("CDK已过期"), nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 19:31:44 +08:00
|
|
|
playerCdkService := playerservice.NewCdkService(ownerID)
|
2026-04-08 18:12:02 +08:00
|
|
|
if !playerCdkService.CanGet(uint32(cdkInfo.ID)) {
|
|
|
|
|
return cool.Fail("CDK已领取"), nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-08 19:31:44 +08:00
|
|
|
serverInfo, useErr := cdkService.UseServerNamingCDK(ctx, cdkCode, ownerID, req.ServerID, serverName)
|
2026-04-08 18:12:02 +08:00
|
|
|
if useErr != nil {
|
|
|
|
|
return cool.Fail(useErr.Error()), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
playerCdkService.Log(uint32(cdkInfo.ID))
|
|
|
|
|
return cool.Ok(&DonationRedeemRes{
|
2026-04-08 19:31:44 +08:00
|
|
|
ServerID: serverInfo.ServerID,
|
|
|
|
|
ServerName: serverInfo.ServerName,
|
|
|
|
|
OwnerID: serverInfo.OwnerID,
|
|
|
|
|
ServerExpireTime: serverInfo.ServerExpireTime,
|
2026-04-08 18:12:02 +08:00
|
|
|
}), nil
|
|
|
|
|
}
|