@@ -378,11 +378,53 @@ func (h *Handler) connectionTestModel(ctx context.Context) string {
378378 return "gpt-5.4"
379379}
380380
381- // BatchTest 批量测试所有账号连接
381+ type batchTestRequest struct {
382+ IDs * []int64 `json:"ids"`
383+ }
384+
385+ func resolveBatchTestAccounts (store * auth.Store , ids * []int64 ) ([]* auth.Account , int ) {
386+ if store == nil {
387+ return nil , 0
388+ }
389+ if ids == nil {
390+ return store .Accounts (), 0
391+ }
392+
393+ accounts := make ([]* auth.Account , 0 , len (* ids ))
394+ missing := 0
395+ seen := make (map [int64 ]struct {}, len (* ids ))
396+ for _ , id := range * ids {
397+ if _ , ok := seen [id ]; ok {
398+ continue
399+ }
400+ seen [id ] = struct {}{}
401+ acc := store .FindByID (id )
402+ if acc == nil {
403+ missing ++
404+ continue
405+ }
406+ accounts = append (accounts , acc )
407+ }
408+ return accounts , missing
409+ }
410+
411+ // BatchTest 批量测试账号连接;未传 ids 时测试所有账号,传 ids 时仅测试指定账号。
382412// POST /api/admin/accounts/batch-test
383413func (h * Handler ) BatchTest (c * gin.Context ) {
384- accounts := h .store .Accounts ()
385- if len (accounts ) == 0 {
414+ var req batchTestRequest
415+ if c .Request .Body != nil && c .Request .ContentLength != 0 {
416+ if err := c .ShouldBindJSON (& req ); err != nil {
417+ writeError (c , http .StatusBadRequest , "请求格式错误" )
418+ return
419+ }
420+ }
421+ if req .IDs != nil && len (* req .IDs ) == 0 {
422+ writeError (c , http .StatusBadRequest , "请提供要测试的账号 ID 列表" )
423+ return
424+ }
425+
426+ accounts , missingCount := resolveBatchTestAccounts (h .store , req .IDs )
427+ if len (accounts ) == 0 && missingCount == 0 {
386428 c .JSON (http .StatusOK , gin.H {"total" : 0 , "success" : 0 , "failed" : 0 , "banned" : 0 , "rate_limited" : 0 })
387429 return
388430 }
@@ -393,7 +435,7 @@ func (h *Handler) BatchTest(c *gin.Context) {
393435
394436 var (
395437 successCount int64
396- failedCount int64
438+ failedCount = int64 ( missingCount )
397439 bannedCount int64
398440 rateLimitCount int64
399441 wg sync.WaitGroup
@@ -457,7 +499,7 @@ func (h *Handler) BatchTest(c *gin.Context) {
457499 wg .Wait ()
458500
459501 c .JSON (http .StatusOK , gin.H {
460- "total" : len (accounts ),
502+ "total" : len (accounts ) + missingCount ,
461503 "success" : successCount ,
462504 "failed" : failedCount ,
463505 "banned" : bannedCount ,
0 commit comments