64 lines
1.9 KiB
Go
64 lines
1.9 KiB
Go
package system
|
||
|
||
import (
|
||
"time"
|
||
|
||
"git.echol.cn/loser/Go-Web-Template/server/model/common/response"
|
||
"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
|
||
// @Tags InviteCode
|
||
// @Summary 生成邀请码(同一时间仅允许一个未使用码)
|
||
// @Security ApiKeyAuth
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=system.InviteCodeGenerated,msg=string} "生成成功"
|
||
// @Router /user/invite-code/generate [post]
|
||
func (a *InviteCodeApi) GenerateInviteCode(c *gin.Context) {
|
||
uid := utils.GetUserID(c)
|
||
expireHours := inviteCodeService.InviteExpireHours()
|
||
res, err := inviteCodeService.Generate(uid, expireHours)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
response.OkWithData(res, c)
|
||
}
|
||
|
||
// CurrentInviteCode
|
||
// @Tags InviteCode
|
||
// @Summary 获取当前未使用邀请码(不返回明文)
|
||
// @Security ApiKeyAuth
|
||
// @Produce application/json
|
||
// @Success 200 {object} response.Response{data=inviteCodeCurrentResponse,msg=string} "获取成功"
|
||
// @Router /user/invite-code/current [get]
|
||
func (a *InviteCodeApi) CurrentInviteCode(c *gin.Context) {
|
||
uid := utils.GetUserID(c)
|
||
rec, err := inviteCodeService.GetCurrentUnused(uid)
|
||
if err != nil {
|
||
response.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
if rec == nil {
|
||
response.OkWithData(inviteCodeCurrentResponse{HasUnused: false}, c)
|
||
return
|
||
}
|
||
// 仅返回必要字段(不返回 hash)
|
||
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)
|
||
}
|