mirror of
https://github.com/zyycn/codex-proxy-rs.git
synced 2026-11-20 08:01:46 +00:00
feat: add account egress and client key USD budgets
Import sub2api proxy bindings, enforce durable daily and seven-day budgets alongside existing key concurrency, and integrate matching admin controls and audited reconciliation.
This commit is contained in:
@@ -110,6 +110,7 @@ export interface AccountUsage {
|
||||
}
|
||||
|
||||
export interface Account {
|
||||
outboundProxyEndpoint: string | null
|
||||
id: string
|
||||
name: string
|
||||
provider: string
|
||||
@@ -288,6 +289,7 @@ interface AccountResetCreditConsumeParam extends AccountIdParam {
|
||||
}
|
||||
|
||||
interface AccountUpdateParam {
|
||||
outboundProxyUrl?: string
|
||||
accountId: string
|
||||
enabled: boolean
|
||||
concurrencyLimit: number | null
|
||||
@@ -296,6 +298,7 @@ interface AccountUpdateParam {
|
||||
}
|
||||
|
||||
interface AccountBatchUpdateParam {
|
||||
outboundProxyUrl?: string
|
||||
accountIds: string[]
|
||||
enabled: boolean
|
||||
concurrencyLimit: number | null
|
||||
@@ -314,6 +317,7 @@ interface AccountImportParam {
|
||||
}
|
||||
|
||||
interface AccountOAuthStartParam {
|
||||
outboundProxyUrl?: string
|
||||
provider: string
|
||||
name: string
|
||||
accountId?: string
|
||||
|
||||
@@ -11,6 +11,13 @@ export interface ApiKey {
|
||||
enabled: boolean
|
||||
maxConcurrency: number
|
||||
requestsPerMinute: number
|
||||
dailyLimitUsd: string
|
||||
weeklyLimitUsd: string
|
||||
dailyUsedUsd: string
|
||||
weeklyUsedUsd: string
|
||||
dailyResetsAt: string | null
|
||||
weeklyResetsAt: string | null
|
||||
unresolvedRequests: number
|
||||
createdAt: string
|
||||
updatedAt: string
|
||||
lastUsedAt: string | null
|
||||
@@ -40,6 +47,21 @@ export interface ApiKeyMutationResponse {
|
||||
id: string
|
||||
}
|
||||
|
||||
export interface UnresolvedClientCharge {
|
||||
requestId: string
|
||||
startedAt: string
|
||||
completedAt: string | null
|
||||
state: string
|
||||
}
|
||||
|
||||
export function getUnresolvedClientCharges(id: string) {
|
||||
return request<{ items: UnresolvedClientCharge[] }>({ url: '/api/admin/client-keys/unresolved-charges', method: 'GET', params: { id } })
|
||||
}
|
||||
|
||||
export function reconcileClientCharge(data: { id: string, requestId: string, amountUsd: string, reason: string }) {
|
||||
return request<{ reconciled: boolean }>({ url: '/api/admin/client-keys/reconcile-charge', method: 'POST', data })
|
||||
}
|
||||
|
||||
// 请求参数类型:仅定义 API 边界的形状,调用方不依赖显式声明。
|
||||
interface ApiKeyListParams {
|
||||
cursor?: string
|
||||
@@ -55,6 +77,8 @@ export interface ApiKeyWriteParam {
|
||||
groupIds: string[]
|
||||
maxConcurrency: number
|
||||
requestsPerMinute: number
|
||||
dailyLimitUsd: string
|
||||
weeklyLimitUsd: string
|
||||
}
|
||||
|
||||
interface ApiKeyUpdateParam extends ApiKeyWriteParam {
|
||||
|
||||
@@ -7,6 +7,7 @@ import BaseFormItem from '@/components/base/BaseForm/FormItem.vue'
|
||||
import BaseInput from '@/components/base/BaseInput.vue'
|
||||
import BaseModal from '@/components/base/BaseModal/index.vue'
|
||||
import BaseSwitch from '@/components/base/BaseSwitch.vue'
|
||||
import AccountProxyField from './AccountProxyField.vue'
|
||||
|
||||
defineProps<{
|
||||
selectedCount: number
|
||||
@@ -23,6 +24,8 @@ const open = defineModel<boolean>({ required: true })
|
||||
const enabled = defineModel<boolean>('enabled', { required: true })
|
||||
const concurrencyLimit = defineModel<string>('concurrencyLimit', { required: true })
|
||||
const weight = defineModel<string>('weight', { required: true })
|
||||
const proxyMode = defineModel<string>('proxyMode', { required: true })
|
||||
const proxyUrl = defineModel<string>('proxyUrl', { required: true })
|
||||
const selectedGroupIds = defineModel<string[]>('selectedGroupIds', { required: true })
|
||||
</script>
|
||||
|
||||
@@ -69,6 +72,7 @@ const selectedGroupIds = defineModel<string[]>('selectedGroupIds', { required: t
|
||||
</BaseFormItem>
|
||||
</div>
|
||||
|
||||
<AccountProxyField v-model:mode="proxyMode" v-model:url="proxyUrl" :disabled="saving" />
|
||||
<BaseFormItem label="所属分组">
|
||||
<AccountGroupCheckboxGrid
|
||||
v-model="selectedGroupIds"
|
||||
|
||||
@@ -16,6 +16,7 @@ import BaseScrollbar from '@/components/base/BaseScrollbar.vue'
|
||||
import BaseSegmented from '@/components/base/BaseSegmented.vue'
|
||||
import BaseTextarea from '@/components/base/BaseTextarea.vue'
|
||||
import { useCopyText } from '@/composables/useCopyText'
|
||||
import AccountProxyField from '../AccountProxyField.vue'
|
||||
import AccountProviderChooser from './AccountProviderChooser.vue'
|
||||
import { resolveAccountCreatePresentation } from './presenter'
|
||||
|
||||
@@ -148,6 +149,7 @@ async function copyText(value: string, successText: string) {
|
||||
/>
|
||||
|
||||
<div v-if="mode === 'oauth'" class="flex flex-col gap-4">
|
||||
<AccountProxyField v-if="!reauthorizing" v-model:mode="form.proxyMode" v-model:url="form.proxyUrl" :preserve="false" :disabled="saving || oauthLoading || Boolean(form.oauthFlowId)" />
|
||||
<div class="rounded-cp bg-cp-fill-quaternary px-4 py-3">
|
||||
<div class="flex items-start gap-3">
|
||||
<div
|
||||
|
||||
@@ -6,6 +6,8 @@ export interface AccountCreateForm {
|
||||
oauthFlowId: string
|
||||
oauthAuthUrl: string
|
||||
oauthCallback: string
|
||||
proxyMode: string
|
||||
proxyUrl: string
|
||||
}
|
||||
|
||||
export function emptyAccountCreateForm(): AccountCreateForm {
|
||||
@@ -17,5 +19,7 @@ export function emptyAccountCreateForm(): AccountCreateForm {
|
||||
oauthFlowId: '',
|
||||
oauthAuthUrl: '',
|
||||
oauthCallback: '',
|
||||
proxyMode: 'direct',
|
||||
proxyUrl: '',
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import BaseSwitch from '@/components/base/BaseSwitch.vue'
|
||||
import ProviderIconGroup from '@/components/ProviderIconGroup.vue'
|
||||
import AccountIdentityCell from './AccountIdentityCell.vue'
|
||||
import AccountPlanBadge from './AccountPlanBadge.vue'
|
||||
import AccountProxyField from './AccountProxyField.vue'
|
||||
|
||||
defineProps<{
|
||||
account: AccountRow | null
|
||||
@@ -27,6 +28,8 @@ const open = defineModel<boolean>({ required: true })
|
||||
const enabled = defineModel<boolean>('enabled', { required: true })
|
||||
const concurrencyLimit = defineModel<string>('concurrencyLimit', { required: true })
|
||||
const weight = defineModel<string>('weight', { required: true })
|
||||
const proxyMode = defineModel<string>('proxyMode', { required: true })
|
||||
const proxyUrl = defineModel<string>('proxyUrl', { required: true })
|
||||
const selectedGroupIds = defineModel<string[]>('selectedGroupIds', { required: true })
|
||||
</script>
|
||||
|
||||
@@ -98,6 +101,7 @@ const selectedGroupIds = defineModel<string[]>('selectedGroupIds', { required: t
|
||||
:disabled="saving"
|
||||
/>
|
||||
</BaseFormItem>
|
||||
<AccountProxyField v-model:mode="proxyMode" v-model:url="proxyUrl" :endpoint="account.outboundProxyEndpoint" :disabled="saving" />
|
||||
</div>
|
||||
|
||||
<template #footer>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import BaseFormItem from '@/components/base/BaseForm/FormItem.vue'
|
||||
import BaseInput from '@/components/base/BaseInput.vue'
|
||||
import BaseSelect from '@/components/base/BaseSelect.vue'
|
||||
|
||||
const props = withDefaults(defineProps<{ endpoint?: string | null, disabled?: boolean, preserve?: boolean }>(), { preserve: true })
|
||||
const mode = defineModel<string>('mode', { required: true })
|
||||
const url = defineModel<string>('url', { required: true })
|
||||
const options = computed(() => [
|
||||
...(props.preserve ? [{ label: '保持当前', value: 'preserve' }] : []),
|
||||
{ label: '直连', value: 'direct' },
|
||||
{ label: '指定代理', value: 'proxy' },
|
||||
])
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="grid gap-3">
|
||||
<BaseFormItem label="出站隧道">
|
||||
<BaseSelect v-model="mode" :options="options" :disabled="disabled" aria-label="出站隧道" />
|
||||
</BaseFormItem>
|
||||
<p v-if="endpoint && mode === 'preserve'" class="m-0 font-mono text-xs break-all text-cp-text-tertiary">
|
||||
{{ endpoint }}
|
||||
</p>
|
||||
<BaseFormItem v-if="mode === 'proxy'" label="代理 URL" required>
|
||||
<BaseInput v-model="url" type="password" autocomplete="new-password" aria-label="代理 URL" :disabled="disabled" />
|
||||
</BaseFormItem>
|
||||
</div>
|
||||
</template>
|
||||
@@ -20,6 +20,8 @@ export function useAccountBatchEditor(options: {
|
||||
const schedulingEnabled = shallowRef(true)
|
||||
const concurrencyLimit = shallowRef('')
|
||||
const weight = shallowRef('1')
|
||||
const proxyMode = shallowRef('preserve')
|
||||
const proxyUrl = shallowRef('')
|
||||
const selectedGroupIds = ref<string[]>([])
|
||||
const saveAction = useAsyncAction()
|
||||
const saving = saveAction.loading
|
||||
@@ -30,6 +32,8 @@ export function useAccountBatchEditor(options: {
|
||||
return
|
||||
|
||||
schedulingEnabled.value = accounts.every(account => account.enabled)
|
||||
proxyMode.value = 'preserve'
|
||||
proxyUrl.value = ''
|
||||
concurrencyLimit.value = sharedConcurrencyLimit(accounts)
|
||||
weight.value = sharedWeight(accounts)
|
||||
selectedGroupIds.value = sharedGroupIds(accounts)
|
||||
@@ -40,6 +44,10 @@ export function useAccountBatchEditor(options: {
|
||||
if (saving.value || options.selectedIds.value.size === 0)
|
||||
return
|
||||
const scheduling = parseAccountSchedulingForm(concurrencyLimit.value, weight.value)
|
||||
if (proxyMode.value === 'proxy' && !proxyUrl.value.trim()) {
|
||||
toast.warning('请输入代理 URL')
|
||||
return
|
||||
}
|
||||
if (!scheduling.valid) {
|
||||
toast.warning(scheduling.message)
|
||||
return
|
||||
@@ -49,6 +57,7 @@ export function useAccountBatchEditor(options: {
|
||||
const accountIds = selectedAccounts().map(account => account.id)
|
||||
await batchUpdateAccounts({
|
||||
accountIds,
|
||||
outboundProxyUrl: proxyMode.value === 'preserve' ? undefined : proxyMode.value === 'direct' ? '' : proxyUrl.value.trim(),
|
||||
enabled: schedulingEnabled.value,
|
||||
concurrencyLimit: scheduling.values.concurrencyLimit,
|
||||
weight: scheduling.values.weight,
|
||||
@@ -89,6 +98,8 @@ export function useAccountBatchEditor(options: {
|
||||
if (open || isSaving)
|
||||
return
|
||||
schedulingEnabled.value = true
|
||||
proxyMode.value = 'preserve'
|
||||
proxyUrl.value = ''
|
||||
concurrencyLimit.value = ''
|
||||
weight.value = '1'
|
||||
selectedGroupIds.value = []
|
||||
@@ -99,6 +110,8 @@ export function useAccountBatchEditor(options: {
|
||||
schedulingEnabled,
|
||||
concurrencyLimit,
|
||||
weight,
|
||||
proxyMode,
|
||||
proxyUrl,
|
||||
selectedGroupIds,
|
||||
saving,
|
||||
open,
|
||||
|
||||
@@ -19,6 +19,8 @@ export function useAccountEditor(options: {
|
||||
const schedulingEnabled = shallowRef(true)
|
||||
const concurrencyLimit = shallowRef('')
|
||||
const weight = shallowRef('1')
|
||||
const proxyMode = shallowRef('preserve')
|
||||
const proxyUrl = shallowRef('')
|
||||
const selectedGroupIds = ref<string[]>([])
|
||||
const saveAction = useAsyncAction()
|
||||
const saving = saveAction.loading
|
||||
@@ -31,6 +33,8 @@ export function useAccountEditor(options: {
|
||||
|
||||
function open(account: AccountRow) {
|
||||
editingAccountId.value = account.id
|
||||
proxyMode.value = 'preserve'
|
||||
proxyUrl.value = ''
|
||||
schedulingEnabled.value = account.enabled
|
||||
concurrencyLimit.value = concurrencyLimitInput(account.concurrencyLimit)
|
||||
weight.value = String(account.weight)
|
||||
@@ -43,6 +47,10 @@ export function useAccountEditor(options: {
|
||||
if (!accountId || saving.value)
|
||||
return
|
||||
const scheduling = parseAccountSchedulingForm(concurrencyLimit.value, weight.value)
|
||||
if (proxyMode.value === 'proxy' && !proxyUrl.value.trim()) {
|
||||
toast.warning('请输入代理 URL')
|
||||
return
|
||||
}
|
||||
if (!scheduling.valid) {
|
||||
toast.warning(scheduling.message)
|
||||
return
|
||||
@@ -51,6 +59,7 @@ export function useAccountEditor(options: {
|
||||
await saveAction.run(async () => {
|
||||
await updateAccount({
|
||||
accountId,
|
||||
outboundProxyUrl: proxyMode.value === 'preserve' ? undefined : proxyMode.value === 'direct' ? '' : proxyUrl.value.trim(),
|
||||
enabled: schedulingEnabled.value,
|
||||
concurrencyLimit: scheduling.values.concurrencyLimit,
|
||||
weight: scheduling.values.weight,
|
||||
@@ -66,6 +75,8 @@ export function useAccountEditor(options: {
|
||||
if (open || isSaving)
|
||||
return
|
||||
editingAccountId.value = null
|
||||
proxyMode.value = 'preserve'
|
||||
proxyUrl.value = ''
|
||||
schedulingEnabled.value = true
|
||||
concurrencyLimit.value = ''
|
||||
weight.value = '1'
|
||||
@@ -78,6 +89,8 @@ export function useAccountEditor(options: {
|
||||
schedulingEnabled,
|
||||
concurrencyLimit,
|
||||
weight,
|
||||
proxyMode,
|
||||
proxyUrl,
|
||||
selectedGroupIds,
|
||||
saving,
|
||||
open,
|
||||
|
||||
@@ -74,8 +74,11 @@ export function useAccountOnboarding(options: {
|
||||
async () => {
|
||||
const input = await newAccountInput()
|
||||
const account = reauthorizingAccount.value
|
||||
if (!account && createForm.value.proxyMode === 'proxy' && !createForm.value.proxyUrl.trim())
|
||||
throw new Error('请输入代理 URL')
|
||||
const result = await startAccountOAuth({
|
||||
...input,
|
||||
outboundProxyUrl: !account && createForm.value.proxyMode === 'proxy' ? createForm.value.proxyUrl.trim() : undefined,
|
||||
...(account
|
||||
? {
|
||||
accountId: account.id,
|
||||
|
||||
@@ -128,6 +128,8 @@ const {
|
||||
schedulingEnabled: batchSchedulingEnabled,
|
||||
concurrencyLimit: batchConcurrencyLimit,
|
||||
weight: batchWeight,
|
||||
proxyMode: batchProxyMode,
|
||||
proxyUrl: batchProxyUrl,
|
||||
selectedGroupIds: batchGroupIds,
|
||||
saving: savingBatchEdit,
|
||||
open: openBatchEdit,
|
||||
@@ -145,6 +147,8 @@ const {
|
||||
schedulingEnabled,
|
||||
concurrencyLimit: editingConcurrencyLimit,
|
||||
weight: editingWeight,
|
||||
proxyMode: editingProxyMode,
|
||||
proxyUrl: editingProxyUrl,
|
||||
selectedGroupIds: editingGroupIds,
|
||||
saving: savingAccountEdit,
|
||||
open: openAccountEdit,
|
||||
@@ -343,6 +347,8 @@ const {
|
||||
v-model:enabled="schedulingEnabled"
|
||||
v-model:concurrency-limit="editingConcurrencyLimit"
|
||||
v-model:weight="editingWeight"
|
||||
v-model:proxy-mode="editingProxyMode"
|
||||
v-model:proxy-url="editingProxyUrl"
|
||||
v-model:selected-group-ids="editingGroupIds"
|
||||
:account="editingAccount"
|
||||
:groups="groups"
|
||||
@@ -356,6 +362,8 @@ const {
|
||||
v-model:enabled="batchSchedulingEnabled"
|
||||
v-model:concurrency-limit="batchConcurrencyLimit"
|
||||
v-model:weight="batchWeight"
|
||||
v-model:proxy-mode="batchProxyMode"
|
||||
v-model:proxy-url="batchProxyUrl"
|
||||
v-model:selected-group-ids="batchGroupIds"
|
||||
:selected-count="selectedIds.size"
|
||||
:groups="groups"
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<script setup lang="ts">
|
||||
import type { ApiKey } from '@/api'
|
||||
import { ClipboardCheck } from '@lucide/vue'
|
||||
import { computed } from 'vue'
|
||||
import BaseIconButton from '@/components/base/BaseIconButton.vue'
|
||||
|
||||
const props = defineProps<{ apiKey: ApiKey }>()
|
||||
const emit = defineEmits<{ reconcile: [apiKey: ApiKey] }>()
|
||||
const windows = computed(() => [
|
||||
{ label: '日', used: props.apiKey.dailyUsedUsd, limit: props.apiKey.dailyLimitUsd, reset: props.apiKey.dailyResetsAt },
|
||||
{ label: '周', used: props.apiKey.weeklyUsedUsd, limit: props.apiKey.weeklyLimitUsd, reset: props.apiKey.weeklyResetsAt },
|
||||
])
|
||||
function resetLabel(value: string | null) {
|
||||
return value ? `重置:${new Date(value).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false })}(北京时间)` : ''
|
||||
}
|
||||
function amount(value: string) {
|
||||
return Number(value).toLocaleString('en-US', { maximumFractionDigits: 10 })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="grid gap-1 text-xs tabular-nums">
|
||||
<div v-for="window in windows" :key="window.label" :title="`$${window.used} / ${Number(window.limit) === 0 ? '不限' : `$${window.limit}`} ${resetLabel(window.reset)}`" class="flex min-w-0 items-center gap-2">
|
||||
<span class="shrink-0 text-cp-text-tertiary">{{ window.label }}</span>
|
||||
<span class="truncate" :class="Number(window.limit) > 0 && Number(window.used) >= Number(window.limit) ? 'text-cp-error' : 'text-cp-text'">
|
||||
${{ amount(window.used) }} / {{ Number(window.limit) === 0 ? '不限' : `$${amount(window.limit)}` }}
|
||||
</span>
|
||||
</div>
|
||||
<div v-if="apiKey.unresolvedRequests" class="flex items-center gap-1 text-cp-warning">
|
||||
<span>待核账 {{ apiKey.unresolvedRequests }} 笔</span>
|
||||
<BaseIconButton variant="ghost" size="sm" label="费用核账" @click.stop="emit('reconcile', apiKey)">
|
||||
<ClipboardCheck class="size-3.5" />
|
||||
</BaseIconButton>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
@@ -34,7 +34,6 @@ const title = computed(() => props.editing ? '编辑 API Key' : '创建 API Key'
|
||||
<BaseModal
|
||||
v-model="open"
|
||||
:title="title"
|
||||
description="设置调用方可用的账号分组、并发和每分钟请求上限"
|
||||
tone="info"
|
||||
size="lg"
|
||||
:dismissible="!saving"
|
||||
@@ -67,12 +66,23 @@ const title = computed(() => props.editing ? '编辑 API Key' : '创建 API Key'
|
||||
/>
|
||||
</BaseFormItem>
|
||||
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<BaseFormItem label="日限额(USD)">
|
||||
<BaseInput v-model="form.dailyLimitUsd" type="number" min="0" step="any" aria-label="日限额(USD)" :disabled="saving" />
|
||||
</BaseFormItem>
|
||||
<BaseFormItem label="周限额(USD)">
|
||||
<BaseInput v-model="form.weeklyLimitUsd" type="number" min="0" step="any" aria-label="周限额(USD)" :disabled="saving" />
|
||||
</BaseFormItem>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<BaseFormItem label="最大并发" description="0 表示不限制">
|
||||
<BaseInput
|
||||
v-model="form.maxConcurrency"
|
||||
type="number"
|
||||
aria-label="最大并发"
|
||||
min="0"
|
||||
step="1"
|
||||
:disabled="saving"
|
||||
/>
|
||||
</BaseFormItem>
|
||||
@@ -81,6 +91,8 @@ const title = computed(() => props.editing ? '编辑 API Key' : '创建 API Key'
|
||||
v-model="form.requestsPerMinute"
|
||||
type="number"
|
||||
aria-label="每分钟请求数"
|
||||
min="0"
|
||||
step="1"
|
||||
:disabled="saving"
|
||||
/>
|
||||
</BaseFormItem>
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
<script setup lang="ts">
|
||||
import type { ApiKey, UnresolvedClientCharge } from '@/api'
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { getUnresolvedClientCharges, reconcileClientCharge } from '@/api'
|
||||
import BaseButton from '@/components/base/BaseButton.vue'
|
||||
import BaseFormItem from '@/components/base/BaseForm/FormItem.vue'
|
||||
import BaseInput from '@/components/base/BaseInput.vue'
|
||||
import BaseModal from '@/components/base/BaseModal/index.vue'
|
||||
import BaseSelect from '@/components/base/BaseSelect.vue'
|
||||
import { toast } from '@/components/base/BaseToast'
|
||||
import { errorMessage } from '@/utils/async'
|
||||
|
||||
const props = defineProps<{ apiKey: ApiKey | null }>()
|
||||
const emit = defineEmits<{ reconciled: [] }>()
|
||||
const open = defineModel<boolean>({ required: true })
|
||||
const events = ref<UnresolvedClientCharge[]>([])
|
||||
const requestId = ref('')
|
||||
const amount = ref('')
|
||||
const reason = ref('')
|
||||
const loading = ref(false)
|
||||
const saving = ref(false)
|
||||
const error = ref('')
|
||||
let generation = 0
|
||||
const options = computed(() => events.value.map(event => ({ value: event.requestId, label: event.requestId })))
|
||||
const selected = computed(() => events.value.find(event => event.requestId === requestId.value))
|
||||
const valid = computed(() => /^\d{1,10}(?:\.\d{1,10})?$/.test(amount.value.trim()) && reason.value.trim().length > 0 && reason.value.length <= 1024)
|
||||
|
||||
async function load() {
|
||||
const id = props.apiKey?.id
|
||||
const version = ++generation
|
||||
if (!id || !open.value)
|
||||
return
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
events.value = []
|
||||
try {
|
||||
const result = await getUnresolvedClientCharges(id)
|
||||
if (version !== generation)
|
||||
return
|
||||
events.value = result.items
|
||||
requestId.value = result.items[0]?.requestId ?? ''
|
||||
}
|
||||
catch (failure) {
|
||||
if (version === generation)
|
||||
error.value = errorMessage(failure, '待核账请求读取失败')
|
||||
}
|
||||
finally {
|
||||
if (version === generation)
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
const id = props.apiKey?.id
|
||||
if (!id || !requestId.value || !valid.value || saving.value)
|
||||
return
|
||||
saving.value = true
|
||||
try {
|
||||
await reconcileClientCharge({ id, requestId: requestId.value, amountUsd: amount.value.trim(), reason: reason.value.trim() })
|
||||
toast.success('费用已核账')
|
||||
emit('reconciled')
|
||||
await load()
|
||||
if (events.value.length === 0 && !error.value)
|
||||
open.value = false
|
||||
}
|
||||
catch (failure) {
|
||||
toast.error(errorMessage(failure, '核账失败'))
|
||||
}
|
||||
finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
watch([open, () => props.apiKey?.id], () => {
|
||||
amount.value = ''
|
||||
reason.value = ''
|
||||
void load()
|
||||
})
|
||||
watch(requestId, () => {
|
||||
amount.value = ''
|
||||
reason.value = ''
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseModal v-model="open" title="费用核账" :description="apiKey?.name" size="md" :dismissible="!saving">
|
||||
<p v-if="loading" class="text-cp-text-tertiary">
|
||||
读取中…
|
||||
</p>
|
||||
<div v-else-if="error" class="grid gap-3">
|
||||
<p class="text-cp-error">
|
||||
{{ error }}
|
||||
</p>
|
||||
<BaseButton variant="secondary" @click="load">
|
||||
重试
|
||||
</BaseButton>
|
||||
</div>
|
||||
<div v-else-if="events.length" class="grid gap-4">
|
||||
<BaseFormItem label="待核账请求">
|
||||
<BaseSelect v-model="requestId" :options="options" :disabled="saving" />
|
||||
</BaseFormItem>
|
||||
<p v-if="selected" class="m-0 text-xs text-cp-text-tertiary">
|
||||
{{ new Date(selected.startedAt).toLocaleString('zh-CN') }}
|
||||
</p>
|
||||
<BaseFormItem label="确认费用(USD)" required>
|
||||
<BaseInput v-model="amount" aria-label="确认费用(USD)" type="number" min="0" step="any" :disabled="saving" />
|
||||
</BaseFormItem>
|
||||
<BaseFormItem label="核账原因" required>
|
||||
<BaseInput v-model="reason" aria-label="核账原因" :maxlength="1024" :disabled="saving" />
|
||||
</BaseFormItem>
|
||||
</div>
|
||||
<p v-else class="text-cp-text-tertiary">
|
||||
暂无待核账请求
|
||||
</p>
|
||||
<template #footer>
|
||||
<BaseButton variant="ghost" :disabled="saving" @click="open = false">
|
||||
取消
|
||||
</BaseButton>
|
||||
<BaseButton variant="primary" :loading="saving" :disabled="!valid || !requestId || loading || Boolean(error)" @click="save">
|
||||
确认核账
|
||||
</BaseButton>
|
||||
</template>
|
||||
</BaseModal>
|
||||
</template>
|
||||
@@ -23,6 +23,8 @@ export interface ApiKeyFormValue {
|
||||
groupIds: string[]
|
||||
maxConcurrency: string
|
||||
requestsPerMinute: string
|
||||
dailyLimitUsd: string
|
||||
weeklyLimitUsd: string
|
||||
}
|
||||
|
||||
export function useApiKeyMutations(options: {
|
||||
@@ -65,6 +67,8 @@ export function useApiKeyMutations(options: {
|
||||
groupIds: key.groups.map(group => group.id),
|
||||
maxConcurrency: String(key.maxConcurrency),
|
||||
requestsPerMinute: String(key.requestsPerMinute),
|
||||
dailyLimitUsd: key.dailyLimitUsd,
|
||||
weeklyLimitUsd: key.weeklyLimitUsd,
|
||||
}
|
||||
showFormModal.value = true
|
||||
}
|
||||
@@ -96,6 +100,8 @@ export function useApiKeyMutations(options: {
|
||||
groupIds: [...new Set(form.value.groupIds)],
|
||||
maxConcurrency: parseLimit(form.value.maxConcurrency),
|
||||
requestsPerMinute: parseLimit(form.value.requestsPerMinute),
|
||||
dailyLimitUsd: form.value.dailyLimitUsd.trim(),
|
||||
weeklyLimitUsd: form.value.weeklyLimitUsd.trim(),
|
||||
}
|
||||
const current = editingKey.value
|
||||
if (current) {
|
||||
@@ -124,6 +130,12 @@ export function useApiKeyMutations(options: {
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
for (const [label, value] of [['日限额', form.value.dailyLimitUsd], ['周限额', form.value.weeklyLimitUsd]]) {
|
||||
if (!value || !/^\d{1,10}(?:\.\d{1,10})?$/.test(value.trim())) {
|
||||
toast.warning(`${label}必须是非负金额,最多 10 位小数`)
|
||||
return false
|
||||
}
|
||||
}
|
||||
if (!form.value.name.trim()) {
|
||||
toast.warning('请输入 API Key 名称')
|
||||
return false
|
||||
@@ -274,6 +286,8 @@ function emptyForm(): ApiKeyFormValue {
|
||||
groupIds: [],
|
||||
maxConcurrency: '0',
|
||||
requestsPerMinute: '0',
|
||||
dailyLimitUsd: '0',
|
||||
weeklyLimitUsd: '0',
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@ export const apiKeyColumns = defineTableColumns<ApiKeyRow>([
|
||||
{ key: 'identity', label: '名称', kind: 'identity', size: 'xl', sortable: 'name' },
|
||||
{ key: 'prefix', label: '密钥前缀', kind: 'mono', size: '2xl' },
|
||||
{ key: 'enabled', label: '状态', kind: 'status', sortable: true },
|
||||
{ key: 'scope', label: '分组', kind: 'status' },
|
||||
{ key: 'scope', label: '分组', kind: 'status', size: 'lg' },
|
||||
{ key: 'budget', label: '已用 / 限额 USD', kind: 'text', size: 'xl' },
|
||||
{ key: 'limits', label: '并发 / RPM', kind: 'text' },
|
||||
{ key: 'createdAtDisplay', label: '创建时间', kind: 'datetime', sortable: 'createdAt' },
|
||||
{
|
||||
key: 'lastUsedAt',
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
import type { ApiKey } from '@/api'
|
||||
import { ref, watch } from 'vue'
|
||||
|
||||
import BaseCard from '@/components/base/BaseCard.vue'
|
||||
@@ -11,10 +12,12 @@ import LastUsedAtCell from '@/components/LastUsedAtCell.vue'
|
||||
import { useAccountGroupCatalog } from '@/composables/useAccountGroupCatalog'
|
||||
import { usePageSelection } from '@/composables/usePageSelection'
|
||||
import ApiKeyActions from './components/ApiKeyActions.vue'
|
||||
import ApiKeyBudgetCell from './components/ApiKeyBudgetCell.vue'
|
||||
import ApiKeyCreateModal from './components/ApiKeyCreateModal.vue'
|
||||
import ApiKeyFilters from './components/ApiKeyFilters.vue'
|
||||
import ApiKeyIdentityCell from './components/ApiKeyIdentityCell.vue'
|
||||
import ApiKeyPrefixCell from './components/ApiKeyPrefixCell.vue'
|
||||
import ApiKeyReconcileModal from './components/ApiKeyReconcileModal.vue'
|
||||
import ApiKeyScopeCell from './components/ApiKeyScopeCell.vue'
|
||||
import ApiKeyStatusBadge from './components/ApiKeyStatusBadge.vue'
|
||||
import ApiKeyUseModal from './components/ApiKeyUseModal.vue'
|
||||
@@ -24,6 +27,13 @@ import { useApiKeyUse } from './composables/useApiKeyUse'
|
||||
import { apiKeyColumns } from './constants'
|
||||
|
||||
const selectedIds = ref<Set<string>>(new Set())
|
||||
const reconcileKey = ref<ApiKey | null>(null)
|
||||
const showReconcile = ref(false)
|
||||
|
||||
function openReconcile(key: ApiKey) {
|
||||
reconcileKey.value = key
|
||||
showReconcile.value = true
|
||||
}
|
||||
|
||||
const {
|
||||
loading,
|
||||
@@ -157,6 +167,12 @@ watch(
|
||||
<template #scope="{ row }">
|
||||
<ApiKeyScopeCell :api-key="row" />
|
||||
</template>
|
||||
<template #budget="{ row }">
|
||||
<ApiKeyBudgetCell :api-key="row" @reconcile="openReconcile" />
|
||||
</template>
|
||||
<template #limits="{ row }">
|
||||
<span class="text-xs tabular-nums">{{ row.maxConcurrency || '不限' }} / {{ row.requestsPerMinute || '不限' }}</span>
|
||||
</template>
|
||||
<template #enabled="{ row }">
|
||||
<ApiKeyStatusBadge :api-key="row" />
|
||||
</template>
|
||||
@@ -200,6 +216,7 @@ watch(
|
||||
@save="requestSave"
|
||||
@import-ccs="importCreatedKeyToCcs"
|
||||
/>
|
||||
<ApiKeyReconcileModal v-model="showReconcile" :api-key="reconcileKey" @reconciled="loadApiKeys" />
|
||||
|
||||
<ApiKeyUseModal
|
||||
v-model="showUseKeyModal"
|
||||
|
||||
Reference in New Issue
Block a user