mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-03 06:52:13 +08:00
注册邮箱域名白名单策略上线,后台大数据场景性能大幅优化。 - 注册邮箱域名白名单:支持管理员配置允许注册的邮箱域名策略 - Keys 页面表单筛选:用户 /keys 页面支持按条件筛选 API Key - Settings 页面分 Tab 拆分:管理后台设置页面按功能模块分 Tab 展示 - 后台大数据场景加载性能优化:仪表盘/用户/账号/Ops 页面大数据集加载显著提速 - Usage 大表分页优化:默认避免全量 COUNT(*),大幅降低分页查询耗时 - 消除重复的 normalizeAccountIDList,补充新增组件的单元测试 - 清理无用文件和过时文档,精简项目结构 - EmailVerifyView 硬编码英文字符串替换为 i18n 调用 - 修复 Anthropic 平台无限流重置时间的 429 误标记账号限流问题 - 修复自定义菜单页面管理员视角菜单不生效问题 - 修复 Ops 错误详情弹窗未展示真实上游 payload 的问题 - 修复充值/订阅菜单 icon 显示问题 # Conflicts: # .gitignore # backend/cmd/server/VERSION # backend/ent/group.go # backend/ent/runtime/runtime.go # backend/ent/schema/group.go # backend/go.sum # backend/internal/handler/admin/account_handler.go # backend/internal/handler/admin/dashboard_handler.go # backend/internal/pkg/usagestats/usage_log_types.go # backend/internal/repository/group_repo.go # backend/internal/repository/usage_log_repo.go # backend/internal/server/middleware/security_headers.go # backend/internal/server/router.go # backend/internal/service/account_usage_service.go # backend/internal/service/admin_service_bulk_update_test.go # backend/internal/service/dashboard_service.go # backend/internal/service/gateway_service.go # frontend/src/api/admin/dashboard.ts # frontend/src/components/account/BulkEditAccountModal.vue # frontend/src/components/charts/GroupDistributionChart.vue # frontend/src/components/layout/AppSidebar.vue # frontend/src/i18n/locales/en.ts # frontend/src/i18n/locales/zh.ts # frontend/src/views/admin/GroupsView.vue # frontend/src/views/admin/SettingsView.vue # frontend/src/views/admin/UsageView.vue # frontend/src/views/user/PurchaseSubscriptionView.vue
176 lines
4.3 KiB
Go
176 lines
4.3 KiB
Go
package service
|
||
|
||
import (
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type Group struct {
|
||
ID int64
|
||
Name string
|
||
Description string
|
||
Platform string
|
||
RateMultiplier float64
|
||
IsExclusive bool
|
||
Status string
|
||
Hydrated bool // indicates the group was loaded from a trusted repository source
|
||
|
||
SubscriptionType string
|
||
DailyLimitUSD *float64
|
||
WeeklyLimitUSD *float64
|
||
MonthlyLimitUSD *float64
|
||
DefaultValidityDays int
|
||
|
||
// 图片生成计费配置(antigravity 和 gemini 平台使用)
|
||
ImagePrice1K *float64
|
||
ImagePrice2K *float64
|
||
ImagePrice4K *float64
|
||
|
||
// Sora 按次计费配置(阶段 1)
|
||
SoraImagePrice360 *float64
|
||
SoraImagePrice540 *float64
|
||
SoraVideoPricePerRequest *float64
|
||
SoraVideoPricePerRequestHD *float64
|
||
|
||
// Sora 存储配额
|
||
SoraStorageQuotaBytes int64
|
||
|
||
// Claude Code 客户端限制
|
||
ClaudeCodeOnly bool
|
||
FallbackGroupID *int64
|
||
// 无效请求兜底分组(仅 anthropic 平台使用)
|
||
FallbackGroupIDOnInvalidRequest *int64
|
||
|
||
// 模型路由配置
|
||
// key: 模型匹配模式(支持 * 通配符,如 "claude-opus-*")
|
||
// value: 优先账号 ID 列表
|
||
ModelRouting map[string][]int64
|
||
ModelRoutingEnabled bool
|
||
|
||
// MCP XML 协议注入开关(仅 antigravity 平台使用)
|
||
MCPXMLInject bool
|
||
|
||
// Claude usage 模拟开关:将无写缓存 usage 模拟为 claude-max 风格
|
||
SimulateClaudeMaxEnabled bool
|
||
|
||
// 支持的模型系列(仅 antigravity 平台使用)
|
||
// 可选值: claude, gemini_text, gemini_image
|
||
SupportedModelScopes []string
|
||
|
||
// 分组排序
|
||
SortOrder int
|
||
|
||
CreatedAt time.Time
|
||
UpdatedAt time.Time
|
||
|
||
AccountGroups []AccountGroup
|
||
AccountCount int64
|
||
}
|
||
|
||
func (g *Group) IsActive() bool {
|
||
return g.Status == StatusActive
|
||
}
|
||
|
||
func (g *Group) IsSubscriptionType() bool {
|
||
return g.SubscriptionType == SubscriptionTypeSubscription
|
||
}
|
||
|
||
func (g *Group) IsFreeSubscription() bool {
|
||
return g.IsSubscriptionType() && g.RateMultiplier == 0
|
||
}
|
||
|
||
func (g *Group) HasDailyLimit() bool {
|
||
return g.DailyLimitUSD != nil && *g.DailyLimitUSD > 0
|
||
}
|
||
|
||
func (g *Group) HasWeeklyLimit() bool {
|
||
return g.WeeklyLimitUSD != nil && *g.WeeklyLimitUSD > 0
|
||
}
|
||
|
||
func (g *Group) HasMonthlyLimit() bool {
|
||
return g.MonthlyLimitUSD != nil && *g.MonthlyLimitUSD > 0
|
||
}
|
||
|
||
// GetImagePrice 根据 image_size 返回对应的图片生成价格
|
||
// 如果分组未配置价格,返回 nil(调用方应使用默认值)
|
||
func (g *Group) GetImagePrice(imageSize string) *float64 {
|
||
switch imageSize {
|
||
case "1K":
|
||
return g.ImagePrice1K
|
||
case "2K":
|
||
return g.ImagePrice2K
|
||
case "4K":
|
||
return g.ImagePrice4K
|
||
default:
|
||
// 未知尺寸默认按 2K 计费
|
||
return g.ImagePrice2K
|
||
}
|
||
}
|
||
|
||
// GetSoraImagePrice 根据 Sora 图片尺寸返回价格(360/540)
|
||
func (g *Group) GetSoraImagePrice(imageSize string) *float64 {
|
||
switch imageSize {
|
||
case "360":
|
||
return g.SoraImagePrice360
|
||
case "540":
|
||
return g.SoraImagePrice540
|
||
default:
|
||
return g.SoraImagePrice360
|
||
}
|
||
}
|
||
|
||
// IsGroupContextValid reports whether a group from context has the fields required for routing decisions.
|
||
func IsGroupContextValid(group *Group) bool {
|
||
if group == nil {
|
||
return false
|
||
}
|
||
if group.ID <= 0 {
|
||
return false
|
||
}
|
||
if !group.Hydrated {
|
||
return false
|
||
}
|
||
if group.Platform == "" || group.Status == "" {
|
||
return false
|
||
}
|
||
return true
|
||
}
|
||
|
||
// GetRoutingAccountIDs 根据请求模型获取路由账号 ID 列表
|
||
// 返回匹配的优先账号 ID 列表,如果没有匹配规则则返回 nil
|
||
func (g *Group) GetRoutingAccountIDs(requestedModel string) []int64 {
|
||
if !g.ModelRoutingEnabled || len(g.ModelRouting) == 0 || requestedModel == "" {
|
||
return nil
|
||
}
|
||
|
||
// 1. 精确匹配优先
|
||
if accountIDs, ok := g.ModelRouting[requestedModel]; ok && len(accountIDs) > 0 {
|
||
return accountIDs
|
||
}
|
||
|
||
// 2. 通配符匹配(前缀匹配)
|
||
for pattern, accountIDs := range g.ModelRouting {
|
||
if matchModelPattern(pattern, requestedModel) && len(accountIDs) > 0 {
|
||
return accountIDs
|
||
}
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// matchModelPattern 检查模型是否匹配模式
|
||
// 支持 * 通配符,如 "claude-opus-*" 匹配 "claude-opus-4-20250514"
|
||
func matchModelPattern(pattern, model string) bool {
|
||
if pattern == model {
|
||
return true
|
||
}
|
||
|
||
// 处理 * 通配符(仅支持末尾通配符)
|
||
if strings.HasSuffix(pattern, "*") {
|
||
prefix := strings.TrimSuffix(pattern, "*")
|
||
return strings.HasPrefix(model, prefix)
|
||
}
|
||
|
||
return false
|
||
}
|