This commit is contained in:
2026-05-08 06:19:59 -07:00
parent 7d2f98b7c9
commit f946504cb8
12 changed files with 1493 additions and 140 deletions
-25
View File
@@ -2,13 +2,11 @@ package auth
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/perfect-panel/server/internal/config"
"github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/internal/model/log"
"github.com/perfect-panel/server/internal/model/user"
"github.com/perfect-panel/server/internal/svc"
@@ -47,29 +45,6 @@ func (l *EmailLoginLogic) EmailLogin(req *types.EmailLoginRequest) (resp *types.
req.Code = strings.TrimSpace(req.Code)
// Verify Code
scenes := []string{constant.Security.String(), constant.Register.String(), "unknown"}
var verified bool
var cacheKeyUsed string
var payload common.CacheKeyPayload
for _, scene := range scenes {
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, scene, req.Email)
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
if err != nil || value == "" {
continue
}
if err := json.Unmarshal([]byte(value), &payload); err != nil {
continue
}
if payload.Code == req.Code && time.Now().Unix()-payload.LastAt <= l.svcCtx.Config.VerifyCode.VerifyCodeExpireTime {
verified = true
cacheKeyUsed = cacheKey
break
}
}
if !verified {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "verification code error or expired")
}
l.svcCtx.Redis.Del(l.ctx, cacheKeyUsed)
// Check User
userInfo, err = l.svcCtx.UserModel.FindOneByEmail(l.ctx, req.Email)
@@ -43,7 +43,7 @@ func (l *GetInviteSalesLogic) GetInviteSales(req *types.GetInviteSalesRequest) (
db := l.svcCtx.DB.WithContext(l.ctx).
Table("`order` o").
Joins("JOIN user u ON o.user_id = u.id").
Where("u.referer_id = ? AND o.status = ?", userId, 5)
Where("u.referer_id = ? AND o.status IN ?", userId, []int{2, 5})
if req.StartTime > 0 {
db = db.Where("o.updated_at >= FROM_UNIXTIME(?)", req.StartTime)
@@ -88,7 +88,7 @@ func (l *GetInviteSalesLogic) GetInviteSales(req *types.GetInviteSalesRequest) (
Select("o.amount, CAST(UNIX_TIMESTAMP(o.updated_at) * 1000 AS SIGNED) as updated_at, u.id as user_id, s.name as product_name, o.quantity").
Joins("JOIN user u ON o.user_id = u.id").
Joins("LEFT JOIN subscribe s ON o.subscribe_id = s.id").
Where("u.referer_id = ? AND o.status = ?", userId, 5) // status 5: Finished
Where("u.referer_id = ? AND o.status IN ?", userId, []int{2, 5}) // status 2: Active, 5: Finished
if req.StartTime > 0 {
query = query.Where("o.updated_at >= FROM_UNIXTIME(?)", req.StartTime)
@@ -58,7 +58,10 @@ func (l *QueryUserAffiliateLogic) QueryUserAffiliate() (resp *types.QueryUserAff
l.Errorf("[QueryUserAffiliate] unmarshal comission log failed: %v", err.Error())
continue
}
sum += content.Amount
// 只统计佣金收入(购买331、续费332),不含提现(336)、退款(333)、调整(335)等
if content.Type == log.CommissionTypePurchase || content.Type == log.CommissionTypeRenewal {
sum += content.Amount
}
}
return &types.QueryUserAffiliateCountResponse{
@@ -2,13 +2,12 @@ package user
import (
"context"
"encoding/json"
"fmt"
"sort"
"strings"
logicCommon "github.com/perfect-panel/server/internal/logic/common"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/kutt"
"github.com/perfect-panel/server/pkg/uuidx"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
@@ -99,8 +98,8 @@ func (l *QueryUserInfoLogic) QueryUserInfo() (resp *types.User, err error) {
resp.AuthMethods = userMethods
// 生成邀请短链接
if l.svcCtx.Config.Kutt.Enable && resp.ReferCode != "" {
shortLink := l.generateInviteShortLink(resp.ReferCode)
if resp.ReferCode != "" {
shortLink := logicCommon.NewInviteLinkResolver(l.ctx, l.svcCtx).ResolveInviteLink(resp.ReferCode)
if shortLink != "" {
resp.ShareLink = shortLink
}
@@ -212,112 +211,6 @@ func getFamilyStatusName(status uint8) string {
return "disabled"
}
// customData 用于解析 SiteConfig.CustomData JSON 字段
// 包含从自定义数据中提取所需的配置项
type customData struct {
ShareUrl string `json:"shareUrl"` // 分享链接前缀 URL(目标落地页)
Domain string `json:"domain"` // 短链接域名
}
// getShareUrl 从 SiteConfig.CustomData 中获取 shareUrl
//
// 返回:
// - string: 分享链接前缀 URL,如果获取失败则返回 Kutt.TargetURL 作为 fallback
func (l *QueryUserInfoLogic) getShareUrl() string {
siteConfig := l.svcCtx.Config.Site
if siteConfig.CustomData != "" {
var data customData
if err := json.Unmarshal([]byte(siteConfig.CustomData), &data); err == nil {
if data.ShareUrl != "" {
return data.ShareUrl
}
}
}
// fallback 到 Kutt.TargetURL
return l.svcCtx.Config.Kutt.TargetURL
}
// getDomain 从 SiteConfig.CustomData 中获取短链接域名
//
// 返回:
// - string: 短链接域名,如果获取失败则返回 Kutt.Domain 作为 fallback
func (l *QueryUserInfoLogic) getDomain() string {
siteConfig := l.svcCtx.Config.Site
if siteConfig.CustomData != "" {
var data customData
if err := json.Unmarshal([]byte(siteConfig.CustomData), &data); err == nil {
if data.Domain != "" {
return data.Domain
}
}
}
// fallback 到 Kutt.Domain
return l.svcCtx.Config.Kutt.Domain
}
// generateInviteShortLink 生成邀请短链接(带 Redis 缓存)
//
// 参数:
// - inviteCode: 邀请码
//
// 返回:
// - string: 短链接 URL,失败时返回空字符串
func (l *QueryUserInfoLogic) generateInviteShortLink(inviteCode string) string {
cfg := l.svcCtx.Config.Kutt
shareUrl := l.getShareUrl()
domain := l.getDomain()
// 检查必要配置
if cfg.ApiURL == "" || cfg.ApiKey == "" {
l.Sloww("Kutt config incomplete",
logger.Field("api_url", cfg.ApiURL != ""),
logger.Field("api_key", cfg.ApiKey != ""))
return ""
}
if shareUrl == "" {
l.Sloww("ShareUrl not configured in CustomData or Kutt.TargetURL")
return ""
}
// Redis 缓存 key
cacheKey := "cache:invite:short_link:" + inviteCode
// 1. 尝试从 Redis 缓存读取
cachedLink, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
if err == nil && cachedLink != "" {
l.Debugw("Hit cache for invite short link",
logger.Field("invite_code", inviteCode),
logger.Field("short_link", cachedLink))
return cachedLink
}
// 2. 缓存未命中,调用 Kutt API 创建短链接
client := kutt.NewClient(cfg.ApiURL, cfg.ApiKey)
shortLink, err := client.CreateInviteShortLink(l.ctx, shareUrl, inviteCode, domain)
if err != nil {
l.Errorw("Failed to create short link",
logger.Field("error", err.Error()),
logger.Field("invite_code", inviteCode),
logger.Field("share_url", shareUrl))
return ""
}
// 3. 写入 Redis 缓存(永不过期,因为邀请码不变短链接也不会变)
if err := l.svcCtx.Redis.Set(l.ctx, cacheKey, shortLink, 0).Err(); err != nil {
l.Errorw("Failed to cache short link",
logger.Field("error", err.Error()),
logger.Field("invite_code", inviteCode))
// 缓存失败不影响返回
}
l.Infow("Created and cached invite short link",
logger.Field("invite_code", inviteCode),
logger.Field("short_link", shortLink),
logger.Field("share_url", shareUrl))
return shortLink
}
// getAuthTypePriority 获取认证类型的排序优先级
// email: 1 (第一位)
// mobile: 2 (第二位)