feat: 添加在线设备统计功能并优化订阅相关逻辑

- 在DeviceManager中添加GetOnlineDeviceCount方法用于获取在线设备数
- 在统计接口中增加在线设备数返回
- 优化订阅查询逻辑,增加服务组关联节点数量计算
- 添加AnyTLS协议支持及相关URI生成功能
- 重构邀请佣金计算逻辑,支持首购/年付/非首购不同比例
- 修复用户基本信息更新中IsAdmin和Enable字段类型不匹配问题
- 更新数据库迁移脚本和配置文件中邀请相关配置项
This commit is contained in:
2025-08-12 07:46:45 -07:00
parent c8de30f78c
commit a52c7142ee
16 changed files with 224 additions and 27 deletions
+4 -3
View File
@@ -119,9 +119,10 @@ type File struct {
}
type InviteConfig struct {
ForcedInvite bool `yaml:"ForcedInvite" default:"false"`
ReferralPercentage int64 `yaml:"ReferralPercentage" default:"0"`
OnlyFirstPurchase bool `yaml:"OnlyFirstPurchase" default:"false"`
ForcedInvite bool `yaml:"ForcedInvite" default:"false"`
FirstPurchasePercentage int64 `yaml:"FirstPurchasePercentage" default:"20"`
FirstYearlyPurchasePercentage int64 `yaml:"FirstYearlyPurchasePercentage" default:"25"`
NonFirstPurchasePercentage int64 `yaml:"NonFirstPurchasePercentage" default:"10"`
}
type Telegram struct {
@@ -44,6 +44,9 @@ func (l *UpdateUserBasicInfoLogic) UpdateUserBasicInfo(req *types.UpdateUserBasi
userInfo.Balance = req.Balance
userInfo.GiftAmount = req.GiftAmount
userInfo.Commission = req.Commission
// 手动设置 IsAdmin 字段,因为类型不匹配(*bool vs bool
userInfo.IsAdmin = &req.IsAdmin
userInfo.Enable = &req.Enable
if req.Password != "" {
if userInfo.Id == 2 && isDemo {
+5 -4
View File
@@ -120,10 +120,11 @@ func (l *GetStatLogic) GetStat() (resp *types.GetStatResponse, err error) {
protocol = append(protocol, p)
}
resp = &types.GetStatResponse{
User: u,
Node: n,
Country: int64(len(country)),
Protocol: protocol,
User: u,
Node: n,
Country: int64(len(country)),
Protocol: protocol,
OnlineDevice: l.svcCtx.DeviceManager.GetOnlineDeviceCount(),
}
val, _ := json.Marshal(*resp)
_ = l.svcCtx.Redis.Set(l.ctx, config.CommonStatCacheKey, string(val), time.Duration(3600)*time.Second).Err()
@@ -43,10 +43,26 @@ func (l *GetSubscriptionLogic) GetSubscription() (resp *types.GetSubscriptionRes
tool.DeepCopy(&sub, item)
if item.Discount != "" {
var discount []types.SubscribeDiscount
_ = json.Unmarshal([]byte(item.Discount), &discount)
sub.Discount = discount
list[i] = sub
}
// 计算节点数量(通过服务组查询关联的实际节点数量)
if item.ServerGroup != "" {
// 获取服务组ID列表
groupIds := tool.StringToInt64Slice(item.ServerGroup)
// 通过服务组查询关联的节点数量
servers, err := l.svcCtx.ServerModel.FindServerListByGroupIds(l.ctx, groupIds)
if err != nil {
l.Errorw("[Site GetSubscription] FindServerListByGroupIds error", logger.Field("error", err.Error()))
sub.ServerCount = 0
} else {
sub.ServerCount = int64(len(servers))
}
}
list[i] = sub
}
resp.List = list
@@ -40,6 +40,7 @@ func (l *QuerySubscribeListLogic) QuerySubscribeList() (resp *types.QuerySubscri
list := make([]types.Subscribe, len(data))
for i, item := range data {
var sub types.Subscribe
tool.DeepCopy(&sub, item)
if item.Discount != "" {
var discount []types.SubscribeDiscount
@@ -48,6 +49,15 @@ func (l *QuerySubscribeListLogic) QuerySubscribeList() (resp *types.QuerySubscri
list[i] = sub
}
list[i] = sub
// 通过服务组查询关联的节点数量
servers, err := l.svcCtx.ServerModel.FindServerListByGroupIds(l.ctx, sub.ServerGroup)
if err != nil {
l.Errorw("[QuerySubscribeListLogic] FindServerListByGroupIds error", logger.Field("error", err.Error()))
sub.ServerCount = 0
} else {
sub.ServerCount = int64(len(servers))
}
list[i] = sub
}
resp.List = list
return
@@ -60,6 +60,25 @@ func (l *QueryUserSubscribeLogic) QueryUserSubscribe() (resp *types.QueryUserSub
}
}
// 计算节点数量(通过服务组关联的实际节点数量)
if item.Subscribe != nil {
// 获取服务组ID列表
groupIds := tool.StringToInt64Slice(item.Subscribe.ServerGroup)
// 通过服务组查询关联的节点数量
servers, err := l.svcCtx.ServerModel.FindServerListByGroupIds(l.ctx, groupIds)
if err != nil {
l.Errorw("[QueryUserSubscribeLogic] FindServerListByGroupIds error", logger.Field("error", err.Error()))
sub.Subscribe.ServerCount = 0
} else {
sub.Subscribe.ServerCount = int64(len(servers))
}
// 保留原始服务器ID列表用于其他用途
serverIds := tool.StringToInt64Slice(item.Subscribe.Server)
sub.Subscribe.Server = serverIds
}
sub.ResetTime = calculateNextResetTime(&sub)
resp.List = append(resp.List, sub)
}
@@ -118,10 +118,47 @@ func (l *SubscribeLogic) getServers(userSub *user.Subscribe) ([]*server.Server,
serverIds := tool.StringToInt64Slice(subDetails.Server)
groupIds := tool.StringToInt64Slice(subDetails.ServerGroup)
// 🔍 订阅ID 2的详细调试
if userSub.SubscribeId == 2 {
l.Infof("🔍 [DEBUG Subscribe 2] === 开始调试订阅ID 2 ===")
l.Infof("🔍 [DEBUG Subscribe 2] Subscribe详情: %+v", subDetails)
l.Infof("🔍 [DEBUG Subscribe 2] Server字段: %s", subDetails.Server)
l.Infof("🔍 [DEBUG Subscribe 2] ServerGroup字段: %s", subDetails.ServerGroup)
l.Infof("🔍 [DEBUG Subscribe 2] 解析后的serverIds: %v", serverIds)
l.Infof("🔍 [DEBUG Subscribe 2] 解析后的groupIds: %v", groupIds)
}
l.Debugf("[Generate Subscribe]serverIds: %v, groupIds: %v", serverIds, groupIds)
// 查询所有服务器用于调试
allServers, _ := l.svc.ServerModel.FindAllServer(l.ctx.Request.Context())
if userSub.SubscribeId == 2 {
l.Infof("🔍 [DEBUG Subscribe 2] 数据库中所有服务器:")
for _, srv := range allServers {
l.Infof("🔍 [DEBUG Subscribe 2] ID:%d Name:%s Protocol:%s Enable:%v GroupID:%d",
srv.Id, srv.Name, srv.Protocol, *srv.Enable, srv.GroupId)
}
}
servers, err := l.svc.ServerModel.FindServerDetailByGroupIdsAndIds(l.ctx.Request.Context(), groupIds, serverIds)
if userSub.SubscribeId == 2 {
l.Infof("🔍 [DEBUG Subscribe 2] 查询结果服务器数量: %d", len(servers))
for i, srv := range servers {
l.Infof("🔍 [DEBUG Subscribe 2] 结果服务器 %d: ID=%d Name=%s Protocol=%s Enable=%v",
i+1, srv.Id, srv.Name, srv.Protocol, *srv.Enable)
}
// 检查AnyTLS服务器
anytlsServers := []*server.Server{}
for _, srv := range servers {
if srv.Protocol == "anytls" {
anytlsServers = append(anytlsServers, srv)
}
}
l.Infof("🔍 [DEBUG Subscribe 2] AnyTLS服务器数量: %d", len(anytlsServers))
}
l.Debugf("[Query Subscribe]found servers: %v", len(servers))
if err != nil {
+1
View File
@@ -82,6 +82,7 @@ func (m *defaultUserModel) QueryUserSubscribe(ctx context.Context, userId int64,
// 订阅过期时间大于当前时间或者订阅结束时间大于当前时间
return conn.Where("`expire_time` > ? OR `finished_at` >= ? OR `expire_time` = ?", now, sevenDaysAgo, time.UnixMilli(0)).
Preload("Subscribe").
Order("created_at DESC").
Find(&list).Error
})
return list, err
+11 -7
View File
@@ -475,6 +475,7 @@ type CreateSubscribeRequest struct {
GroupId int64 `json:"group_id"`
ServerGroup []int64 `json:"server_group"`
Server []int64 `json:"server"`
ServerCount int64 `json:"server_count"`
Show *bool `json:"show"`
Sell *bool `json:"sell"`
DeductionRatio int64 `json:"deduction_ratio"`
@@ -861,10 +862,11 @@ type GetServerUserListResponse struct {
}
type GetStatResponse struct {
User int64 `json:"user"`
Node int64 `json:"node"`
Country int64 `json:"country"`
Protocol []string `json:"protocol"`
User int64 `json:"user"`
Node int64 `json:"node"`
Country int64 `json:"country"`
Protocol []string `json:"protocol"`
OnlineDevice int64 `json:"online_device"`
}
type GetSubscribeDetailsRequest struct {
@@ -1044,9 +1046,10 @@ type Hysteria2 struct {
}
type InviteConfig struct {
ForcedInvite bool `json:"forced_invite"`
ReferralPercentage int64 `json:"referral_percentage"`
OnlyFirstPurchase bool `json:"only_first_purchase"`
ForcedInvite bool `json:"forced_invite"`
FirstPurchasePercentage int64 `json:"first_purchase_percentage"`
FirstYearlyPurchasePercentage int64 `json:"first_yearly_purchase_percentage"`
NonFirstPurchasePercentage int64 `json:"non_first_purchase_percentage"`
}
type KickOfflineRequest struct {
@@ -1656,6 +1659,7 @@ type Subscribe struct {
GroupId int64 `json:"group_id"`
ServerGroup []int64 `json:"server_group"`
Server []int64 `json:"server"`
ServerCount int64 `json:"server_count"`
Show bool `json:"show"`
Sell bool `json:"sell"`
Sort int64 `json:"sort"`