mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-05-05 05:30:44 +08:00
fix: address audit findings across websearch, notify, and channel pricing
Backend fixes: - Fix balance notify ignoring percentage threshold type (was treating percentage value as fixed USD amount) - Remove dead code parseJSONStringArray - Add ImageOutputTokens to tryModelFilePricing calculation - Unify zero-value check: cost == 0 → cost <= 0 in calculateTokenStatsCost - Use MarshalNotifyEmails instead of json.Marshal for consistency - Rename quotaDim.oldUsed → currentUsed for clarity - Extract HTML email templates to const variables (function ≤30 lines) Test fixes: - Rewrite account_websearch_test.go for GetWebSearchEmulationMode tri-state - Add 6 tryModelFilePricing test cases Frontend fixes: - Replace hardcoded '未命名' with i18n key - Extract getBillingModeLabel/getBillingModeBadgeClass to shared utils - Replace inline type with imported NotifyEmailEntry - Pass platform to AccountStats pricing rules via inferRulePlatform() - Add billing mode constants (BILLING_MODE_TOKEN/PER_REQUEST/IMAGE)
This commit is contained in:
@@ -87,7 +87,7 @@
|
||||
|
||||
<template #cell-billing_mode="{ row }">
|
||||
<span class="inline-flex items-center rounded px-2 py-0.5 text-xs font-medium" :class="getBillingModeBadgeClass(row.billing_mode)">
|
||||
{{ getBillingModeLabel(row.billing_mode) }}
|
||||
{{ getBillingModeLabel(row.billing_mode, t) }}
|
||||
</span>
|
||||
</template>
|
||||
|
||||
@@ -346,6 +346,7 @@ import { formatCacheTokens, formatMultiplier } from '@/utils/formatters'
|
||||
import { formatTokenPricePerMillion } from '@/utils/usagePricing'
|
||||
import { getUsageServiceTierLabel } from '@/utils/usageServiceTier'
|
||||
import { resolveUsageRequestType } from '@/utils/usageRequestType'
|
||||
import { getBillingModeLabel, getBillingModeBadgeClass } from '@/utils/billingMode'
|
||||
import DataTable from '@/components/common/DataTable.vue'
|
||||
import EmptyState from '@/components/common/EmptyState.vue'
|
||||
import Icon from '@/components/icons/Icon.vue'
|
||||
@@ -399,17 +400,6 @@ const getRequestTypeBadgeClass = (row: AdminUsageLog): string => {
|
||||
return 'bg-amber-100 text-amber-800 dark:bg-amber-900 dark:text-amber-200'
|
||||
}
|
||||
|
||||
const getBillingModeLabel = (mode: string | null | undefined): string => {
|
||||
if (mode === 'per_request') return t('admin.usage.billingModePerRequest')
|
||||
if (mode === 'image') return t('admin.usage.billingModeImage')
|
||||
return t('admin.usage.billingModeToken')
|
||||
}
|
||||
|
||||
const getBillingModeBadgeClass = (mode: string | null | undefined): string => {
|
||||
if (mode === 'per_request') return 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200'
|
||||
if (mode === 'image') return 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200'
|
||||
return 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200'
|
||||
}
|
||||
|
||||
|
||||
const formatUserAgent = (ua: string): string => {
|
||||
|
||||
@@ -1889,7 +1889,8 @@ export default {
|
||||
searchAccountPlaceholder: 'Search accounts...',
|
||||
ruleAccountsHint: 'Leave empty to match all accounts',
|
||||
ruleModelPricing: 'Model Pricing',
|
||||
noGroupsInChannel: 'No groups selected in platform tabs above'
|
||||
noGroupsInChannel: 'No groups selected in platform tabs above',
|
||||
unnamed: 'Unnamed'
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -1968,7 +1968,8 @@ export default {
|
||||
searchAccountPlaceholder: '搜索账号...',
|
||||
ruleAccountsHint: '留空表示匹配所有账号',
|
||||
ruleModelPricing: '模型定价',
|
||||
noGroupsInChannel: '上方平台标签页中未选择分组'
|
||||
noGroupsInChannel: '上方平台标签页中未选择分组',
|
||||
unnamed: '未命名'
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
19
frontend/src/utils/billingMode.ts
Normal file
19
frontend/src/utils/billingMode.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export const BILLING_MODE_TOKEN = 'token'
|
||||
export const BILLING_MODE_PER_REQUEST = 'per_request'
|
||||
export const BILLING_MODE_IMAGE = 'image'
|
||||
|
||||
export function getBillingModeLabel(mode: string | null | undefined, t: (key: string) => string): string {
|
||||
switch (mode) {
|
||||
case BILLING_MODE_PER_REQUEST: return t('admin.usage.billingModePerRequest')
|
||||
case BILLING_MODE_IMAGE: return t('admin.usage.billingModeImage')
|
||||
default: return t('admin.usage.billingModeToken')
|
||||
}
|
||||
}
|
||||
|
||||
export function getBillingModeBadgeClass(mode: string | null | undefined): string {
|
||||
switch (mode) {
|
||||
case BILLING_MODE_PER_REQUEST: return 'bg-purple-100 text-purple-700 dark:bg-purple-900/30 dark:text-purple-300'
|
||||
case BILLING_MODE_IMAGE: return 'bg-pink-100 text-pink-700 dark:bg-pink-900/30 dark:text-pink-300'
|
||||
default: return 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-300'
|
||||
}
|
||||
}
|
||||
@@ -980,26 +980,38 @@ function clearAllRuleAccountSearchState() {
|
||||
showRuleAccountDropdown.value = {}
|
||||
}
|
||||
|
||||
function inferRulePlatform(groupIds: number[]): string {
|
||||
const platforms = new Set<string>()
|
||||
for (const gid of groupIds) {
|
||||
const group = allGroups.value.find(g => g.id === gid)
|
||||
if (group) platforms.add(group.platform)
|
||||
}
|
||||
return platforms.size === 1 ? [...platforms][0] : ''
|
||||
}
|
||||
|
||||
function accountStatsRulesToAPI(): AccountStatsPricingRule[] {
|
||||
return form.account_stats_pricing_rules.map(rule => ({
|
||||
name: rule.name,
|
||||
group_ids: rule.group_ids,
|
||||
account_ids: rule.account_ids,
|
||||
pricing: rule.pricing
|
||||
.filter(p => p.models.length > 0)
|
||||
.map(p => ({
|
||||
platform: '',
|
||||
models: p.models,
|
||||
billing_mode: p.billing_mode,
|
||||
input_price: mTokToPerToken(p.input_price),
|
||||
output_price: mTokToPerToken(p.output_price),
|
||||
cache_write_price: mTokToPerToken(p.cache_write_price),
|
||||
cache_read_price: mTokToPerToken(p.cache_read_price),
|
||||
image_output_price: mTokToPerToken(p.image_output_price),
|
||||
per_request_price: p.per_request_price != null && p.per_request_price !== '' ? Number(p.per_request_price) : null,
|
||||
intervals: formIntervalsToAPI(p.intervals || [])
|
||||
}))
|
||||
}))
|
||||
return form.account_stats_pricing_rules.map(rule => {
|
||||
const platform = inferRulePlatform(rule.group_ids)
|
||||
return {
|
||||
name: rule.name,
|
||||
group_ids: rule.group_ids,
|
||||
account_ids: rule.account_ids,
|
||||
pricing: rule.pricing
|
||||
.filter(p => p.models.length > 0)
|
||||
.map(p => ({
|
||||
platform,
|
||||
models: p.models,
|
||||
billing_mode: p.billing_mode,
|
||||
input_price: mTokToPerToken(p.input_price),
|
||||
output_price: mTokToPerToken(p.output_price),
|
||||
cache_write_price: mTokToPerToken(p.cache_write_price),
|
||||
cache_read_price: mTokToPerToken(p.cache_read_price),
|
||||
image_output_price: mTokToPerToken(p.image_output_price),
|
||||
per_request_price: p.per_request_price != null && p.per_request_price !== '' ? Number(p.per_request_price) : null,
|
||||
intervals: formIntervalsToAPI(p.intervals || [])
|
||||
}))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// ── Form ↔ API conversion ──
|
||||
@@ -1329,7 +1341,7 @@ async function handleSubmit() {
|
||||
const intervalErr = validateIntervals(entry.intervals)
|
||||
if (intervalErr) {
|
||||
const platformLabel = t('admin.groups.platforms.' + section.platform, section.platform)
|
||||
const modelLabel = entry.models.join(', ') || '未命名'
|
||||
const modelLabel = entry.models.join(', ') || t('admin.channels.form.unnamed')
|
||||
appStore.showError(`${platformLabel} - ${modelLabel}: ${intervalErr}`)
|
||||
activeTab.value = section.platform
|
||||
return
|
||||
|
||||
@@ -2804,7 +2804,7 @@ import type {
|
||||
WebSearchProviderConfig,
|
||||
WebSearchTestResult,
|
||||
} from '@/api/admin/settings'
|
||||
import type { AdminGroup, Proxy } from '@/types'
|
||||
import type { AdminGroup, Proxy, NotifyEmailEntry } from '@/types'
|
||||
import type { ProviderInstance } from '@/types/payment'
|
||||
import AppLayout from '@/components/layout/AppLayout.vue'
|
||||
import Icon from '@/components/icons/Icon.vue'
|
||||
@@ -3028,7 +3028,7 @@ const form = reactive<SettingsForm>({
|
||||
balance_low_notify_enabled: false,
|
||||
balance_low_notify_threshold: 0,
|
||||
account_quota_notify_enabled: false,
|
||||
account_quota_notify_emails: [] as { email: string; disabled: boolean; verified: boolean }[]
|
||||
account_quota_notify_emails: [] as NotifyEmailEntry[]
|
||||
})
|
||||
|
||||
// Proxies for web search emulation ProxySelector
|
||||
|
||||
@@ -192,7 +192,7 @@
|
||||
<template #cell-billing_mode="{ row }">
|
||||
<span class="inline-flex items-center rounded px-1.5 py-0.5 text-xs font-medium"
|
||||
:class="getBillingModeBadgeClass(row.billing_mode)">
|
||||
{{ getBillingModeLabel(row.billing_mode) }}
|
||||
{{ getBillingModeLabel(row.billing_mode, t) }}
|
||||
</span>
|
||||
</template>
|
||||
|
||||
@@ -524,6 +524,7 @@ import { formatCacheTokens, formatMultiplier } from '@/utils/formatters'
|
||||
import { formatTokenPricePerMillion } from '@/utils/usagePricing'
|
||||
import { getUsageServiceTierLabel } from '@/utils/usageServiceTier'
|
||||
import { resolveUsageRequestType } from '@/utils/usageRequestType'
|
||||
import { getBillingModeLabel, getBillingModeBadgeClass } from '@/utils/billingMode'
|
||||
|
||||
const { t } = useI18n()
|
||||
const appStore = useAppStore()
|
||||
@@ -644,17 +645,6 @@ const getRequestTypeBadgeClass = (log: UsageLog): string => {
|
||||
return 'bg-amber-100 text-amber-800 dark:bg-amber-900 dark:text-amber-200'
|
||||
}
|
||||
|
||||
const getBillingModeLabel = (mode: string | null | undefined): string => {
|
||||
if (mode === 'per_request') return t('admin.usage.billingModePerRequest')
|
||||
if (mode === 'image') return t('admin.usage.billingModeImage')
|
||||
return t('admin.usage.billingModeToken')
|
||||
}
|
||||
|
||||
const getBillingModeBadgeClass = (mode: string | null | undefined): string => {
|
||||
if (mode === 'per_request') return 'bg-blue-100 text-blue-800 dark:bg-blue-900/30 dark:text-blue-200'
|
||||
if (mode === 'image') return 'bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-200'
|
||||
return 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300'
|
||||
}
|
||||
|
||||
const getRequestTypeExportText = (log: UsageLog): string => {
|
||||
const requestType = resolveUsageRequestType(log)
|
||||
@@ -866,7 +856,7 @@ const exportToCSV = async () => {
|
||||
formatReasoningEffort(log.reasoning_effort),
|
||||
log.inbound_endpoint || '',
|
||||
getRequestTypeExportText(log),
|
||||
getBillingModeLabel(log.billing_mode),
|
||||
getBillingModeLabel(log.billing_mode, t),
|
||||
log.input_tokens,
|
||||
log.output_tokens,
|
||||
log.cache_read_tokens,
|
||||
|
||||
Reference in New Issue
Block a user