- 新增 EnableTrialEmailWhitelist + TrialEmailDomainWhitelist 配置 - 邮箱注册/登录:加白名单域名判断,域名匹配才赠送 trial - 设备登录/手机注册:移除 activeTrial,不再自动赠送 - 绑定邮箱(bindEmailWithVerification):绑定成功后检查白名单+防重复赠送 - 新增 IsEmailDomainWhitelisted 导出函数供跨包调用 - 清理 deviceLoginLogic/telephoneUserRegisterLogic 中的 activeTrial 死代码 Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
@@ -8,12 +8,14 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/perfect-panel/server/internal/config"
|
||||
"github.com/perfect-panel/server/internal/logic/auth"
|
||||
"github.com/perfect-panel/server/internal/model/user"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
"github.com/perfect-panel/server/pkg/constant"
|
||||
"github.com/perfect-panel/server/pkg/jwt"
|
||||
"github.com/perfect-panel/server/pkg/logger"
|
||||
"github.com/perfect-panel/server/pkg/tool"
|
||||
"github.com/perfect-panel/server/pkg/uuidx"
|
||||
"github.com/perfect-panel/server/pkg/xerr"
|
||||
"github.com/pkg/errors"
|
||||
@@ -127,6 +129,8 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Grant trial subscription if email domain is whitelisted
|
||||
l.tryGrantTrialOnEmailBind(emailUser.Id, req.Email)
|
||||
return &types.BindEmailWithVerificationResponse{
|
||||
Success: true,
|
||||
Message: "email user created and joined family",
|
||||
@@ -154,6 +158,9 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Grant trial subscription if email domain is whitelisted
|
||||
l.tryGrantTrialOnEmailBind(existingMethod.UserId, req.Email)
|
||||
|
||||
return &types.BindEmailWithVerificationResponse{
|
||||
Success: true,
|
||||
Message: "joined family successfully",
|
||||
@@ -200,3 +207,74 @@ func (l *BindEmailWithVerificationLogic) refreshBindSessionToken(userId int64) (
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
// tryGrantTrialOnEmailBind grants trial subscription to the email user (family owner)
|
||||
// if EnableTrialEmailWhitelist is on and the email domain matches.
|
||||
func (l *BindEmailWithVerificationLogic) tryGrantTrialOnEmailBind(ownerUserId int64, email string) {
|
||||
rc := l.svcCtx.Config.Register
|
||||
if !rc.EnableTrial || !rc.EnableTrialEmailWhitelist {
|
||||
return
|
||||
}
|
||||
if !auth.IsEmailDomainWhitelisted(email, rc.TrialEmailDomainWhitelist) {
|
||||
l.Infow("email domain not in trial whitelist, skip",
|
||||
logger.Field("email", email),
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Anti-duplicate: check if owner already has trial subscription
|
||||
var count int64
|
||||
if err := l.svcCtx.DB.WithContext(l.ctx).
|
||||
Model(&user.Subscribe{}).
|
||||
Where("user_id = ? AND subscribe_id = ?", ownerUserId, rc.TrialSubscribe).
|
||||
Count(&count).Error; err != nil {
|
||||
l.Errorw("failed to check existing trial", logger.Field("error", err.Error()))
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
l.Infow("trial already granted, skip",
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
sub, err := l.svcCtx.SubscribeModel.FindOne(l.ctx, rc.TrialSubscribe)
|
||||
if err != nil {
|
||||
l.Errorw("failed to find trial subscribe template", logger.Field("error", err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
userSub := &user.Subscribe{
|
||||
UserId: ownerUserId,
|
||||
OrderId: 0,
|
||||
SubscribeId: sub.Id,
|
||||
StartTime: time.Now(),
|
||||
ExpireTime: tool.AddTime(rc.TrialTimeUnit, rc.TrialTime, time.Now()),
|
||||
Traffic: sub.Traffic,
|
||||
Download: 0,
|
||||
Upload: 0,
|
||||
Token: uuidx.NewUUID().String(),
|
||||
UUID: uuidx.NewUUID().String(),
|
||||
Status: 1,
|
||||
}
|
||||
if err = l.svcCtx.UserModel.InsertSubscribe(l.ctx, userSub); err != nil {
|
||||
l.Errorw("failed to insert trial subscribe",
|
||||
logger.Field("error", err.Error()),
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// InsertSubscribe auto-clears user subscribe cache via execSubscribeMutation.
|
||||
// Clear server cache so nodes pick up the new subscription.
|
||||
if err = l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); err != nil {
|
||||
l.Errorw("ClearServerAllCache error", logger.Field("error", err.Error()))
|
||||
}
|
||||
|
||||
l.Infow("trial granted on email bind",
|
||||
logger.Field("owner_user_id", ownerUserId),
|
||||
logger.Field("email", email),
|
||||
logger.Field("subscribe_id", sub.Id),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user