initialize(); $options = getopt('', ['admin:', 'only:', 'external', 'json:']); $admin = Admin::where('id', (int) ($options['admin'] ?? 0))->findOrEmpty(); if ($admin->isEmpty()) { fwrite(STDERR, "请用 --admin=<后台账号ID> 指定执行身份\n"); exit(1); } $identity = new Identity(['id' => 0, 'expire_time' => time() + 3600], $admin); $only = (string) ($options['only'] ?? ''); $results = []; foreach (Catalog::all() as $key => $resource) { if ($only !== '' && !str_starts_with($key, $only)) { continue; } if ($resource['kind'] === 'write' || $resource['http'] === 'POST' || !empty($resource['no_login']) || $resource['status'] === Catalog::EXCLUDED) { continue; } if (!isset($options['external']) && !empty($resource['external'])) { continue; } $params = match ($resource['kind']) { 'list' => ['page_no' => 1, 'page_size' => 3, 'page_type' => 1], 'detail' => [(string) ($resource['guard']['param'] ?? $resource['id_param'] ?? 'id') => 1], default => [], }; $started = microtime(true); $envelope = Dispatcher::call($identity, $resource, array_merge($params, (array) ($resource['force'] ?? []))); $ms = (int) round((microtime(true) - $started) * 1000); $msg = $envelope['msg']; $outcome = $envelope['code'] === 1 ? 'ok' : (str_contains($msg, '只读保护') ? 'writes' : (str_contains($msg, '查询失败') ? 'error' : 'fail')); $rows = is_array($envelope['data']['lists'] ?? null) ? count($envelope['data']['lists']) : null; $results[$key] = ['status' => $resource['status'], 'kind' => $resource['kind'], 'outcome' => $outcome, 'msg' => mb_substr($msg, 0, 120), 'rows' => $rows, 'ms' => $ms]; printf("%-8s %-8s %-7s %5dms %s %s\n", $outcome, $resource['status'], $resource['kind'], $ms, $key, $outcome === 'ok' ? '' : mb_substr($msg, 0, 80)); } $summary = array_count_values(array_column($results, 'outcome')); ksort($summary); echo PHP_EOL . json_encode($summary, JSON_UNESCAPED_UNICODE) . PHP_EOL; if (!empty($options['json'])) { file_put_contents($root . $options['json'], json_encode($results, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)); }