Files
Go-Web-Template/server/api/v1/app/invite_code.go
2026-04-23 15:29:07 +08:00

51 lines
1.4 KiB
Go

package app
import (
"time"
"git.echol.cn/loser/Go-Web-Template/server/model/common/response"
appSvc "git.echol.cn/loser/Go-Web-Template/server/service/app"
"git.echol.cn/loser/Go-Web-Template/server/utils"
"github.com/gin-gonic/gin"
)
type InviteCodeApi struct{}
type inviteCodeCurrentResponse struct {
HasUnused bool `json:"hasUnused"`
Record interface{} `json:"record,omitempty"`
}
// GenerateInviteCode 玩家生成邀请码(返回明文)
func (a *InviteCodeApi) GenerateInviteCode(c *gin.Context) {
uid := utils.GetUserID(c)
res, err := appSvc.AppInviteCodeServiceApp.Generate(uid)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
response.OkWithData(res, c)
}
// CurrentInviteCode 获取当前未使用邀请码(不返回明文)
func (a *InviteCodeApi) CurrentInviteCode(c *gin.Context) {
uid := utils.GetUserID(c)
rec, err := appSvc.AppInviteCodeServiceApp.GetCurrentUnused(uid)
if err != nil {
response.FailWithMessage(err.Error(), c)
return
}
if rec == nil {
response.OkWithData(inviteCodeCurrentResponse{HasUnused: false}, c)
return
}
payload := map[string]interface{}{
"id": rec.ID,
"codeLast4": rec.CodeLast4,
"status": rec.Status,
"expiresAt": rec.ExpiresAt,
"createdAt": rec.CreatedAt.Format(time.RFC3339),
}
response.OkWithData(inviteCodeCurrentResponse{HasUnused: true, Record: payload}, c)
}