fix(btpanel): 批量勾选提交真实 server id

v-data-table 勾选值为 id 数组,修复误传 undefined 导致 422。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
r
2026-06-21 09:37:04 +08:00
parent b4436143a6
commit 770469c4aa
2 changed files with 28 additions and 10 deletions
@@ -22,11 +22,11 @@
color="primary"
variant="tonal"
prepend-icon="mdi-playlist-check"
:disabled="!selected.length"
:disabled="!selectedIds.length"
:loading="batchLoading"
@click="openBatchConfirm('selected')"
>
选中获取 API{{ selected.length }}
选中获取 API{{ selectedIds.length }}
</v-btn>
<v-text-field
v-model="search"
@@ -40,7 +40,7 @@
</div>
<v-data-table
v-model="selected"
v-model="selectedIds"
:headers="headers"
:items="filteredServers"
:loading="loading"
@@ -135,7 +135,8 @@ const batchMode = ref<'all' | 'selected'>('all')
const loginLoadingId = ref<number | null>(null)
const search = ref('')
const servers = ref<BtPanelServerRow[]>([])
const selected = ref<BtPanelServerRow[]>([])
/** v-data-table 勾选值为 item-valueid)数组,不是行对象 */
const selectedIds = ref<number[]>([])
const headers = [
{ title: '名称', key: 'name', minWidth: 120 },
@@ -157,8 +158,19 @@ const bootstrapStateLabels: Record<string, string> = {
const unconfiguredCount = computed(() => servers.value.filter(s => !s.configured).length)
function serverById(id: number) {
return servers.value.find(s => s.id === id)
}
function selectedUnconfiguredIds(): number[] {
return selectedIds.value.filter((id) => {
const row = serverById(id)
return Boolean(row && !row.configured)
})
}
const batchTargetCount = computed(() =>
batchMode.value === 'all' ? unconfiguredCount.value : selected.value.filter(s => !s.configured).length,
batchMode.value === 'all' ? unconfiguredCount.value : selectedUnconfiguredIds().length,
)
function bootstrapLabel(state: string) {
@@ -186,8 +198,7 @@ const filteredServers = computed(() => {
function openBatchConfirm(mode: 'all' | 'selected') {
batchMode.value = mode
if (mode === 'selected') {
const targets = selected.value.filter(s => !s.configured)
if (!targets.length) {
if (!selectedUnconfiguredIds().length) {
window.$snackbar?.('所选服务器均已配置 API', 'info')
return
}
@@ -202,7 +213,7 @@ async function loadServers() {
loading.value = true
try {
servers.value = await listBtServers()
selected.value = selected.value.filter(s => servers.value.some(row => row.id === s.id))
selectedIds.value = selectedIds.value.filter(id => servers.value.some(row => row.id === id))
} catch (e: unknown) {
window.$snackbar?.(e instanceof Error ? e.message : '加载服务器列表失败', 'error')
} finally {
@@ -234,7 +245,7 @@ async function confirmBatchBootstrap() {
if (batchMode.value === 'all') {
job = await batchBootstrapBtServers([], { allUnconfigured: true })
} else {
const ids = selected.value.filter(s => !s.configured).map(s => s.id)
const ids = selectedUnconfiguredIds()
job = await batchBootstrapBtServers(ids)
}
batchConfirmOpen.value = false
@@ -340,7 +340,14 @@ class BtPanelService:
statuses = await self.list_server_statuses()
ids = [int(s["id"]) for s in statuses if not s.get("configured")]
else:
ids = [int(x) for x in server_ids if x]
ids = []
for x in server_ids:
try:
n = int(x)
except (TypeError, ValueError):
continue
if n > 0:
ids.append(n)
if not ids:
raise ValueError("没有可批量初始化的服务器(均已配置或未录入)")
return await start_batch_job(