mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
feat(simple-mode): allow basic account grouping
This commit is contained in:
1 parent
ab99d56e96
commit
a3675552bd
11 files changed
+165
-46
No files matched your search
@@ -192,7 +192,7 @@ func initializeApplication(buildInfo handler.BuildInfo) (*Application, error) {
|
||||
adminService := service.NewAdminService(userRepository, adminGroupRepository, adminAccountRepository, proxyRepository, apiKeyRepository, redeemCodeRepository, userGroupRateRepository, userRPMCache, billingCacheService, proxyExitInfoProber, proxyLatencyCache, apiKeyAuthCacheInvalidator, client, settingService, subscriptionService, userSubscriptionRepository, privacyClientFactory, openAIGatewayService, affiliateService, compositeModelRouteRepository, compositeRouteResolver, channelService)
|
||||
adminUserHandler := admin.NewUserHandler(adminService, concurrencyService, serviceUserPlatformQuotaRepository, billingCache, totpService, userService, settingService)
|
||||
groupCapacityService := service.NewGroupCapacityService(accountRepository, groupRepository, concurrencyService, sessionLimitCache, rpmCache)
|
||||
groupHandler := admin.NewGroupHandler(adminService, dashboardService, groupCapacityService)
|
||||
groupHandler := admin.NewGroupHandlerWithConfig(adminService, dashboardService, groupCapacityService, configConfig)
|
||||
claudeUsageFetcher := repository.NewClaudeUsageFetcher(httpUpstream)
|
||||
antigravityQuotaFetcher := service.NewAntigravityQuotaFetcher(proxyRepository, configConfig)
|
||||
grokQuotaFetcher := service.NewGrokQuotaFetcher()
|
||||
|
||||
@@ -24,6 +24,8 @@ type stubAdminService struct {
|
||||
boundAuthIdentity *service.AdminBindAuthIdentityInput
|
||||
boundAuthIdentityFor int64
|
||||
createdAccounts []*service.CreateAccountInput
|
||||
createdGroups []*service.CreateGroupInput
|
||||
updatedGroups []*service.UpdateGroupInput
|
||||
createdProxies []*service.CreateProxyInput
|
||||
updatedProxyIDs []int64
|
||||
updatedProxies []*service.UpdateProxyInput
|
||||
@@ -353,6 +355,7 @@ func (s *stubAdminService) PreviewCompositeRoute(ctx context.Context, groupID in
|
||||
}
|
||||
|
||||
func (s *stubAdminService) CreateGroup(ctx context.Context, input *service.CreateGroupInput) (*service.Group, error) {
|
||||
s.createdGroups = append(s.createdGroups, input)
|
||||
group := service.Group{ID: 200, Name: input.Name, Status: service.StatusActive}
|
||||
return &group, nil
|
||||
}
|
||||
@@ -367,6 +370,7 @@ func (s *stubAdminService) RecoverDuplicateGroup(ctx context.Context, id int64,
|
||||
}
|
||||
|
||||
func (s *stubAdminService) UpdateGroup(ctx context.Context, id int64, input *service.UpdateGroupInput) (*service.Group, error) {
|
||||
s.updatedGroups = append(s.updatedGroups, input)
|
||||
group := service.Group{ID: id, Name: input.Name, Status: service.StatusActive}
|
||||
return &group, nil
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/Wei-Shaw/sub2api/internal/handler/dto"
|
||||
infraerrors "github.com/Wei-Shaw/sub2api/internal/pkg/errors"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
||||
@@ -24,6 +25,7 @@ type GroupHandler struct {
|
||||
adminService service.AdminService
|
||||
dashboardService *service.DashboardService
|
||||
groupCapacityService *service.GroupCapacityService
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// GetLiveCapability 返回当前服务端是否具备生成 Live attestation 的运行环境。
|
||||
@@ -87,13 +89,70 @@ func (f optionalLimitField) ToServiceInput() *float64 {
|
||||
|
||||
// NewGroupHandler creates a new admin group handler
|
||||
func NewGroupHandler(adminService service.AdminService, dashboardService *service.DashboardService, groupCapacityService *service.GroupCapacityService) *GroupHandler {
|
||||
return NewGroupHandlerWithConfig(adminService, dashboardService, groupCapacityService, nil)
|
||||
}
|
||||
|
||||
func NewGroupHandlerWithConfig(adminService service.AdminService, dashboardService *service.DashboardService, groupCapacityService *service.GroupCapacityService, cfg *config.Config) *GroupHandler {
|
||||
return &GroupHandler{
|
||||
adminService: adminService,
|
||||
dashboardService: dashboardService,
|
||||
groupCapacityService: groupCapacityService,
|
||||
cfg: cfg,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *GroupHandler) isSimpleMode() bool {
|
||||
return h != nil && h.cfg != nil && h.cfg.RunMode == config.RunModeSimple
|
||||
}
|
||||
|
||||
func sanitizeCreateGroupRequestForSimpleMode(req *CreateGroupRequest) {
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
req.RateMultiplier = 1
|
||||
req.IsExclusive = false
|
||||
req.SubscriptionType = service.SubscriptionTypeStandard
|
||||
req.DailyLimitUSD, req.WeeklyLimitUSD, req.MonthlyLimitUSD = optionalLimitField{}, optionalLimitField{}, optionalLimitField{}
|
||||
req.AllowImageGeneration, req.AllowBatchImageGeneration = false, false
|
||||
req.ImageRateIndependent, req.ImageRateMultiplier = false, nil
|
||||
req.ImagePrice1K, req.ImagePrice2K, req.ImagePrice4K = nil, nil, nil
|
||||
req.VideoRateIndependent, req.VideoRateMultiplier = false, nil
|
||||
req.PeakRateEnabled, req.PeakStart, req.PeakEnd, req.PeakRateMultiplier = false, "", "", nil
|
||||
req.ProfitControlEnabled, req.ProfitMinMargin, req.ProfitSafetyBuffer = false, nil, nil
|
||||
req.ClaudeCodeOnly, req.FallbackGroupID, req.FallbackGroupIDOnInvalidRequest = false, nil, nil
|
||||
req.ModelRouting, req.ModelRoutingEnabled, req.MCPXMLInject = nil, false, nil
|
||||
req.SupportedModelScopes, req.AllowMessagesDispatch, req.AllowLive = nil, false, false
|
||||
req.RequireOAuthOnly, req.RequirePrivacySet, req.DefaultMappedModel = false, false, ""
|
||||
req.MessagesDispatchModelConfig, req.ModelsListConfig = service.OpenAIMessagesDispatchModelConfig{}, service.GroupModelsListConfig{}
|
||||
req.RPMLimit, req.MaxReasoningEffort, req.ReasoningEffortMappings = 0, "", nil
|
||||
req.CopyAccountsFromGroupIDs = nil
|
||||
}
|
||||
|
||||
func sanitizeUpdateGroupRequestForSimpleMode(req *UpdateGroupRequest) {
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
rate, exclusive, disabled, image, messages, live, oauth, privacy := 1.0, false, false, false, false, false, false, false
|
||||
rpm := 0
|
||||
req.RateMultiplier, req.IsExclusive = &rate, &exclusive
|
||||
req.Status, req.SubscriptionType = "", service.SubscriptionTypeStandard
|
||||
req.DailyLimitUSD, req.WeeklyLimitUSD, req.MonthlyLimitUSD = optionalLimitField{}, optionalLimitField{}, optionalLimitField{}
|
||||
req.AllowImageGeneration, req.AllowBatchImageGeneration = &image, &image
|
||||
req.ImageRateIndependent, req.ImageRateMultiplier = &disabled, nil
|
||||
req.ImagePrice1K, req.ImagePrice2K, req.ImagePrice4K = nil, nil, nil
|
||||
req.VideoRateIndependent, req.VideoRateMultiplier = &disabled, nil
|
||||
req.PeakRateEnabled, req.PeakStart, req.PeakEnd, req.PeakRateMultiplier = &disabled, nil, nil, nil
|
||||
req.ProfitControlEnabled, req.ProfitMinMargin, req.ProfitSafetyBuffer = &disabled, nil, nil
|
||||
req.ClaudeCodeOnly, req.FallbackGroupID, req.FallbackGroupIDOnInvalidRequest = &disabled, nil, nil
|
||||
req.ModelRouting, req.ModelRoutingEnabled, req.MCPXMLInject = nil, &disabled, nil
|
||||
req.SupportedModelScopes = nil
|
||||
req.AllowMessagesDispatch, req.AllowLive = &messages, &live
|
||||
req.RequireOAuthOnly, req.RequirePrivacySet = &oauth, &privacy
|
||||
req.DefaultMappedModel, req.MessagesDispatchModelConfig, req.ModelsListConfig = nil, nil, nil
|
||||
req.RPMLimit, req.MaxReasoningEffort, req.ReasoningEffortMappings = &rpm, nil, nil
|
||||
req.CopyAccountsFromGroupIDs = nil
|
||||
}
|
||||
|
||||
// CreateGroupRequest represents create group request
|
||||
type CreateGroupRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
@@ -501,6 +560,9 @@ func (h *GroupHandler) Create(c *gin.Context) {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
if h.isSimpleMode() {
|
||||
sanitizeCreateGroupRequestForSimpleMode(&req)
|
||||
}
|
||||
|
||||
if err := service.ValidatePeakRateConfig(req.SubscriptionType, req.PeakRateEnabled, req.PeakStart, req.PeakEnd, float64ValueOrDefault(req.PeakRateMultiplier, 1.0)); err != nil {
|
||||
response.BadRequest(c, err.Error())
|
||||
@@ -645,6 +707,9 @@ func (h *GroupHandler) Update(c *gin.Context) {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
if h.isSimpleMode() {
|
||||
sanitizeUpdateGroupRequestForSimpleMode(&req)
|
||||
}
|
||||
|
||||
group, err := h.adminService.UpdateGroup(c.Request.Context(), groupID, &service.UpdateGroupInput{
|
||||
Name: req.Name,
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/config"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGroupHandlerSimpleModeSanitizesCommercialFields(t *testing.T) {
|
||||
gin.SetMode(gin.TestMode)
|
||||
svc := newStubAdminService()
|
||||
h := NewGroupHandlerWithConfig(svc, nil, nil, &config.Config{RunMode: config.RunModeSimple})
|
||||
r := gin.New()
|
||||
r.POST("/groups", h.Create)
|
||||
r.PUT("/groups/:id", h.Update)
|
||||
|
||||
create := `{"name":"simple","platform":"anthropic","rate_multiplier":7,"is_exclusive":true,"subscription_type":"subscription","daily_limit_usd":10,"allow_image_generation":true,"rpm_limit":99}`
|
||||
req := httptest.NewRequest(http.MethodPost, "/groups", bytes.NewBufferString(create))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res := httptest.NewRecorder()
|
||||
r.ServeHTTP(res, req)
|
||||
require.Equal(t, http.StatusOK, res.Code)
|
||||
require.Len(t, svc.createdGroups, 1)
|
||||
created := svc.createdGroups[0]
|
||||
require.Equal(t, 1.0, created.RateMultiplier)
|
||||
require.False(t, created.IsExclusive)
|
||||
require.Equal(t, service.SubscriptionTypeStandard, created.SubscriptionType)
|
||||
require.Nil(t, created.DailyLimitUSD)
|
||||
require.False(t, created.AllowImageGeneration)
|
||||
require.Zero(t, created.RPMLimit)
|
||||
|
||||
update := `{"name":"renamed","rate_multiplier":9,"is_exclusive":true,"subscription_type":"subscription","daily_limit_usd":12,"allow_image_generation":true,"status":"inactive","rpm_limit":123}`
|
||||
req = httptest.NewRequest(http.MethodPut, "/groups/2", bytes.NewBufferString(update))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res = httptest.NewRecorder()
|
||||
r.ServeHTTP(res, req)
|
||||
require.Equal(t, http.StatusOK, res.Code)
|
||||
require.Len(t, svc.updatedGroups, 1)
|
||||
updated := svc.updatedGroups[0]
|
||||
require.NotNil(t, updated.RateMultiplier)
|
||||
require.Equal(t, 1.0, *updated.RateMultiplier)
|
||||
require.NotNil(t, updated.IsExclusive)
|
||||
require.False(t, *updated.IsExclusive)
|
||||
require.Equal(t, service.SubscriptionTypeStandard, updated.SubscriptionType)
|
||||
require.Nil(t, updated.DailyLimitUSD)
|
||||
require.NotNil(t, updated.AllowImageGeneration)
|
||||
require.False(t, *updated.AllowImageGeneration)
|
||||
require.Empty(t, updated.Status)
|
||||
require.NotNil(t, updated.RPMLimit)
|
||||
require.Zero(t, *updated.RPMLimit)
|
||||
}
|
||||
@@ -3432,7 +3432,6 @@
|
||||
|
||||
<!-- Group Selection - 仅标准模式显示 -->
|
||||
<GroupSelector
|
||||
v-if="!authStore.isSimpleMode"
|
||||
v-model="form.group_ids"
|
||||
:groups="groups"
|
||||
:platform="form.platform"
|
||||
@@ -3807,7 +3806,6 @@ import {
|
||||
fetchAntigravityDefaultMappings,
|
||||
isValidWildcardPattern
|
||||
} from '@/composables/useModelWhitelist'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import { useQuotaNotifyState } from '@/composables/useQuotaNotifyState'
|
||||
import {
|
||||
|
||||
@@ -2842,7 +2842,6 @@
|
||||
|
||||
<!-- Group Selection - 仅标准模式显示 -->
|
||||
<GroupSelector
|
||||
v-if="!authStore.isSimpleMode"
|
||||
v-model="form.group_ids"
|
||||
:groups="groups"
|
||||
:platform="account?.platform"
|
||||
@@ -2907,7 +2906,6 @@
|
||||
import { ref, reactive, computed, watch, nextTick } from 'vue'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { useAppStore } from '@/stores/app'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { adminAPI } from '@/api/admin'
|
||||
import { useQuotaNotifyState } from '@/composables/useQuotaNotifyState'
|
||||
import type {
|
||||
|
||||
@@ -764,7 +764,7 @@ const adminNavItems = computed((): NavItem[] => {
|
||||
{ path: '/admin/dashboard', label: t('nav.dashboard'), icon: DashboardIcon },
|
||||
{ path: '/admin/ops', label: t('nav.ops'), icon: ChartIcon, featureFlag: flagOpsMonitoring },
|
||||
{ path: '/admin/users', label: t('nav.users'), icon: UsersIcon, hideInSimpleMode: true },
|
||||
{ path: '/admin/groups', label: t('nav.groups'), icon: FolderIcon, hideInSimpleMode: true },
|
||||
{ path: '/admin/groups', label: t('nav.groups'), icon: FolderIcon },
|
||||
{
|
||||
path: '/admin/channels',
|
||||
label: t('nav.channelManagement'),
|
||||
|
||||
@@ -938,7 +938,6 @@ router.beforeEach(async (to, _from, next) => {
|
||||
// 简易模式下限制访问某些页面
|
||||
if (authStore.isSimpleMode) {
|
||||
const restrictedPaths = [
|
||||
'/admin/groups',
|
||||
'/admin/subscriptions',
|
||||
'/admin/redeem',
|
||||
'/subscriptions',
|
||||
|
||||
@@ -510,7 +510,7 @@
|
||||
<p class="input-hint">{{ t("admin.groups.platformHint") }}</p>
|
||||
</div>
|
||||
<!-- 从分组复制账号 -->
|
||||
<div v-if="copyAccountsGroupOptions.length > 0">
|
||||
<div v-if="!authStore.isSimpleMode && copyAccountsGroupOptions.length > 0">
|
||||
<div class="mb-1.5 flex items-center gap-1">
|
||||
<label class="text-sm font-medium text-gray-700 dark:text-gray-300">
|
||||
{{ t("admin.groups.copyAccounts.title") }}
|
||||
@@ -598,7 +598,7 @@
|
||||
</select>
|
||||
<p class="input-hint">{{ t("admin.groups.copyAccounts.hint") }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<div v-if="!authStore.isSimpleMode">
|
||||
<label class="input-label">{{
|
||||
t("admin.groups.form.rateMultiplier")
|
||||
}}</label>
|
||||
@@ -2307,6 +2307,7 @@
|
||||
/>
|
||||
<p class="input-hint">{{ t("admin.groups.platformNotEditable") }}</p>
|
||||
</div>
|
||||
<template v-if="!authStore.isSimpleMode">
|
||||
<!-- 从分组复制账号(编辑时) -->
|
||||
<div v-if="copyAccountsGroupOptionsForEdit.length > 0">
|
||||
<div class="mb-1.5 flex items-center gap-1">
|
||||
@@ -4022,6 +4023,7 @@
|
||||
{{ t("admin.groups.modelRouting.addRule") }}
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
</form>
|
||||
|
||||
<template #footer>
|
||||
@@ -4568,6 +4570,7 @@
|
||||
import { ref, reactive, computed, onMounted, onUnmounted, watch } from "vue";
|
||||
import { useI18n } from "vue-i18n";
|
||||
import { useAppStore } from "@/stores/app";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import { useOnboardingStore } from "@/stores/onboarding";
|
||||
import { adminAPI } from "@/api/admin";
|
||||
import type {
|
||||
@@ -4738,6 +4741,7 @@ const groupPricingToAPI = (
|
||||
|
||||
const { t } = useI18n();
|
||||
const appStore = useAppStore();
|
||||
const authStore = useAuthStore();
|
||||
const onboardingStore = useOnboardingStore();
|
||||
|
||||
const ALWAYS_VISIBLE_COLUMNS = new Set(["name", "actions"]);
|
||||
@@ -4751,43 +4755,27 @@ const VERSION_NEW_HIDDEN_COLUMNS: Record<number, string[]> = {
|
||||
2: ["id"],
|
||||
};
|
||||
|
||||
const allColumns = computed<Column[]>(() => [
|
||||
{ key: "name", label: t("admin.groups.columns.name"), sortable: true },
|
||||
{ key: "id", label: t("admin.groups.columns.id"), sortable: true },
|
||||
{
|
||||
key: "platform",
|
||||
label: t("admin.groups.columns.platform"),
|
||||
sortable: true,
|
||||
},
|
||||
{
|
||||
key: "billing_type",
|
||||
label: t("admin.groups.columns.billingType"),
|
||||
sortable: true,
|
||||
},
|
||||
{
|
||||
key: "rate_multiplier",
|
||||
label: t("admin.groups.columns.rateMultiplier"),
|
||||
sortable: true,
|
||||
},
|
||||
{
|
||||
key: "is_exclusive",
|
||||
label: t("admin.groups.columns.type"),
|
||||
sortable: true,
|
||||
},
|
||||
{
|
||||
key: "account_count",
|
||||
label: t("admin.groups.columns.accounts"),
|
||||
sortable: true,
|
||||
},
|
||||
{
|
||||
key: "capacity",
|
||||
label: t("admin.groups.columns.capacity"),
|
||||
sortable: false,
|
||||
},
|
||||
{ key: "usage", label: t("admin.groups.columns.usage"), sortable: false },
|
||||
{ key: "status", label: t("admin.groups.columns.status"), sortable: true },
|
||||
{ key: "actions", label: t("admin.groups.columns.actions"), sortable: false },
|
||||
]);
|
||||
const allColumns = computed<Column[]>(() => {
|
||||
const basic: Column[] = [
|
||||
{ key: "name", label: t("admin.groups.columns.name"), sortable: true },
|
||||
{ key: "id", label: t("admin.groups.columns.id"), sortable: true },
|
||||
{ key: "platform", label: t("admin.groups.columns.platform"), sortable: true },
|
||||
{ key: "account_count", label: t("admin.groups.columns.accounts"), sortable: true },
|
||||
{ key: "status", label: t("admin.groups.columns.status"), sortable: true },
|
||||
{ key: "actions", label: t("admin.groups.columns.actions"), sortable: false },
|
||||
];
|
||||
if (authStore.isSimpleMode) return basic;
|
||||
return [
|
||||
...basic.slice(0, 3),
|
||||
{ key: "billing_type", label: t("admin.groups.columns.billingType"), sortable: true },
|
||||
{ key: "rate_multiplier", label: t("admin.groups.columns.rateMultiplier"), sortable: true },
|
||||
{ key: "is_exclusive", label: t("admin.groups.columns.type"), sortable: true },
|
||||
basic[3],
|
||||
{ key: "capacity", label: t("admin.groups.columns.capacity"), sortable: false },
|
||||
{ key: "usage", label: t("admin.groups.columns.usage"), sortable: false },
|
||||
...basic.slice(4),
|
||||
];
|
||||
});
|
||||
|
||||
const toggleableColumns = computed(() =>
|
||||
allColumns.value.filter((col) => !ALWAYS_VISIBLE_COLUMNS.has(col.key)),
|
||||
|
||||
@@ -75,6 +75,12 @@ vi.mock('@/stores/app', () => ({
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/auth', () => ({
|
||||
useAuthStore: () => ({
|
||||
isSimpleMode: false,
|
||||
}),
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/onboarding', () => ({
|
||||
useOnboardingStore: () => ({
|
||||
isCurrentStep,
|
||||
|
||||
@@ -53,6 +53,10 @@ vi.mock('@/stores/app', () => ({
|
||||
useAppStore: () => ({ showSuccess, showError })
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/auth', () => ({
|
||||
useAuthStore: () => ({ isSimpleMode: false })
|
||||
}))
|
||||
|
||||
vi.mock('@/stores/onboarding', () => ({
|
||||
useOnboardingStore: () => ({
|
||||
isCurrentStep: vi.fn(() => false),
|
||||
|
||||
Reference in new issue
Block a user