74 lines
2.3 KiB
Go
74 lines
2.3 KiB
Go
package app
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"bindbox-game/internal/code"
|
|
"bindbox-game/internal/pkg/core"
|
|
"bindbox-game/internal/pkg/validation"
|
|
usersvc "bindbox-game/internal/service/user"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type bindInviterRequest struct {
|
|
InviteCode string `json:"invite_code" binding:"required"`
|
|
}
|
|
|
|
type bindInviterResponse struct {
|
|
InviterID int64 `json:"inviter_id"`
|
|
InviterNickname string `json:"inviter_nickname"`
|
|
}
|
|
|
|
// BindInviter 用户绑定邀请人
|
|
// @Summary 用户绑定邀请人
|
|
// @Description 未绑定过邀请人的用户可主动填写邀请码绑定邀请人(绑定后不可更改)
|
|
// @Tags APP端.用户
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body bindInviterRequest true "邀请码"
|
|
// @Success 200 {object} bindInviterResponse
|
|
// @Failure 400 {object} code.Failure
|
|
// @Router /api/app/users/inviter/bind [post]
|
|
// @Security LoginVerifyToken
|
|
func (h *handler) BindInviter() core.HandlerFunc {
|
|
return func(ctx core.Context) {
|
|
req := new(bindInviterRequest)
|
|
if err := ctx.ShouldBindJSON(req); err != nil {
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
|
|
return
|
|
}
|
|
|
|
userID := int64(ctx.SessionUserInfo().Id)
|
|
|
|
result, err := h.user.BindInviter(ctx.RequestContext(), userID, usersvc.BindInviterInput{
|
|
InviteCode: req.InviteCode,
|
|
})
|
|
if err != nil {
|
|
switch err {
|
|
case usersvc.ErrAlreadyBound:
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10100, "您已绑定过邀请人,无法重复绑定"))
|
|
case usersvc.ErrInvalidCode:
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10101, "邀请码不存在"))
|
|
case usersvc.ErrCannotInviteSelf:
|
|
ctx.AbortWithError(core.Error(http.StatusBadRequest, 10102, "不能邀请自己"))
|
|
default:
|
|
ctx.AbortWithError(core.Error(http.StatusInternalServerError, 10103, err.Error()))
|
|
}
|
|
return
|
|
}
|
|
|
|
// 触发任务中心的邀请成功逻辑(给邀请人发放奖励)
|
|
if result.InviterID > 0 {
|
|
if err := h.task.OnInviteSuccess(ctx.RequestContext(), result.InviterID, userID); err != nil {
|
|
h.logger.Error("触发邀请任务失败", zap.Error(err), zap.Int64("inviter_id", result.InviterID), zap.Int64("invitee_id", userID))
|
|
}
|
|
}
|
|
|
|
ctx.Payload(&bindInviterResponse{
|
|
InviterID: result.InviterID,
|
|
InviterNickname: result.InviterNickname,
|
|
})
|
|
}
|
|
}
|