api-catalog.blade.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <x-filament::page>
  2. <div class="space-y-6">
  3. {{-- 试卷 JSON 输出案例 --}}
  4. <x-filament::section>
  5. <div class="text-base font-semibold text-slate-800">试卷 JSON 输出案例</div>
  6. <div class="mt-2 text-xs text-slate-500">输入卷子 ID,返回与智能出卷 API 中 `exam_content` 完全一致的 JSON,可预览或导出。</div>
  7. <div class="mt-4 space-y-3">
  8. <div class="flex flex-wrap items-center gap-3">
  9. <input
  10. id="paper-json-id"
  11. type="text"
  12. class="w-80 rounded-lg border border-slate-200 px-3 py-2 text-sm"
  13. value="paper_1765788931_ce02f6a3"
  14. placeholder="请输入 paper_id"
  15. />
  16. <button
  17. type="button"
  18. class="rounded-lg bg-primary-600 px-3 py-2 text-sm font-semibold text-white"
  19. onclick="loadPaperJson()"
  20. >
  21. 获取 JSON
  22. </button>
  23. <button
  24. type="button"
  25. class="rounded-lg border border-slate-200 px-3 py-2 text-sm font-semibold text-slate-700"
  26. onclick="downloadPaperJson()"
  27. >
  28. 导出 JSON
  29. </button>
  30. </div>
  31. <pre id="paper-json-output" class="max-h-96 overflow-auto rounded-lg border border-slate-200 bg-slate-50 p-3 text-xs text-slate-700">等待输入 paper_id ...</pre>
  32. </div>
  33. </x-filament::section>
  34. {{-- API 文档列表 --}}
  35. @foreach ($apiGroups as $group)
  36. <x-filament::section>
  37. <div class="flex items-center justify-between">
  38. <div class="text-base font-semibold text-slate-800">{{ $group['name'] }}</div>
  39. <div class="text-xs text-slate-400">{{ count($group['items']) }} 个接口</div>
  40. </div>
  41. <div class="mt-2 text-xs text-slate-500">基于 Laravel 路由自动生成,配合详细文档说明</div>
  42. <div class="mt-4 space-y-4">
  43. @foreach ($group['items'] as $item)
  44. <details class="rounded-lg border border-slate-200 bg-white">
  45. <summary class="flex cursor-pointer list-none flex-wrap items-center gap-3 p-4 hover:bg-slate-50">
  46. <span class="rounded-md px-2 py-1 text-xs font-bold uppercase {{ $this->getMethodColor($item['method']) }}">
  47. {{ $item['method'] }}
  48. </span>
  49. <span class="font-mono text-sm text-slate-800">{{ $item['path'] }}</span>
  50. @if (!empty($item['tag']))
  51. <span class="rounded-full border border-amber-200 bg-amber-50 px-2 py-0.5 text-xs font-semibold text-amber-700">
  52. {{ $item['tag'] }}
  53. </span>
  54. @endif
  55. <span class="ml-auto text-xs text-slate-400">点击展开详情</span>
  56. </summary>
  57. <div class="border-t border-slate-200 p-4 space-y-4">
  58. {{-- 摘要和描述 --}}
  59. @if (!empty($item['details']['summary']))
  60. <div>
  61. <div class="text-sm font-semibold text-slate-700">{{ $item['details']['summary'] }}</div>
  62. @if (!empty($item['details']['description']))
  63. <div class="mt-1 text-sm text-slate-600">{{ $item['details']['description'] }}</div>
  64. @endif
  65. </div>
  66. @endif
  67. {{-- 路由信息 --}}
  68. <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-sm">
  69. @if (!empty($item['details']['route_name']))
  70. <div>
  71. <span class="font-semibold text-slate-700">路由名:</span>
  72. <span class="font-mono text-xs text-slate-600">{{ $item['details']['route_name'] }}</span>
  73. </div>
  74. @endif
  75. @if (!empty($item['details']['action']))
  76. <div>
  77. <span class="font-semibold text-slate-700">Action:</span>
  78. <span class="font-mono text-xs text-slate-600">{{ $item['details']['action'] }}</span>
  79. </div>
  80. @endif
  81. </div>
  82. {{-- 参数说明 --}}
  83. @if (!empty($item['details']['param_details']))
  84. <div>
  85. <div class="mb-2 text-sm font-semibold text-slate-700">参数说明</div>
  86. <div class="space-y-3">
  87. @foreach ($item['details']['param_details'] as $paramGroup)
  88. <div>
  89. @if (!empty($paramGroup[0]) && is_string($paramGroup[0]))
  90. <div class="mb-1 text-xs font-semibold text-slate-600 uppercase">{{ $paramGroup[0] }}</div>
  91. @php $params = array_slice($paramGroup, 1) @endphp
  92. @else
  93. @php $params = $paramGroup @endphp
  94. @endif
  95. <div class="overflow-x-auto">
  96. <table class="min-w-full text-xs">
  97. <thead>
  98. <tr class="bg-slate-50">
  99. <th class="px-3 py-2 text-left font-semibold text-slate-600">名称</th>
  100. <th class="px-3 py-2 text-left font-semibold text-slate-600">类型</th>
  101. <th class="px-3 py-2 text-left font-semibold text-slate-600">必填</th>
  102. <th class="px-3 py-2 text-left font-semibold text-slate-600">默认值</th>
  103. <th class="px-3 py-2 text-left font-semibold text-slate-600">说明</th>
  104. </tr>
  105. </thead>
  106. <tbody class="divide-y divide-slate-200">
  107. @foreach ($params as $param)
  108. <tr class="hover:bg-slate-50">
  109. <td class="px-3 py-2 font-mono text-slate-700">{{ $param['name'] }}</td>
  110. <td class="px-3 py-2 text-slate-600">{{ $param['type'] }}</td>
  111. <td class="px-3 py-2">
  112. @if ($param['required'])
  113. <span class="rounded bg-red-100 px-2 py-0.5 text-red-700">是</span>
  114. @else
  115. <span class="rounded bg-gray-100 px-2 py-0.5 text-gray-600">否</span>
  116. @endif
  117. </td>
  118. <td class="px-3 py-2 text-slate-600">{{ $param['default'] ?? '-' }}</td>
  119. <td class="px-3 py-2 text-slate-600">{{ $param['description'] }}</td>
  120. </tr>
  121. @endforeach
  122. </tbody>
  123. </table>
  124. </div>
  125. </div>
  126. @endforeach
  127. </div>
  128. </div>
  129. @endif
  130. {{-- 响应示例 --}}
  131. @if (!empty($item['details']['response_examples']))
  132. <div>
  133. <div class="mb-2 text-sm font-semibold text-slate-700">响应示例</div>
  134. <div class="space-y-3">
  135. @foreach ($item['details']['response_examples'] as $responseType => $responseExample)
  136. <div>
  137. <div class="mb-1 text-xs font-semibold text-slate-600 uppercase">{{ $responseType }}</div>
  138. <pre class="rounded-lg bg-slate-900 p-3 text-xs text-slate-100 overflow-x-auto">{{ json_encode($responseExample, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
  139. </div>
  140. @endforeach
  141. </div>
  142. </div>
  143. @endif
  144. {{-- 使用示例 --}}
  145. @if (!empty($item['details']['examples']))
  146. <div>
  147. <div class="mb-2 text-sm font-semibold text-slate-700">使用示例</div>
  148. <div class="space-y-1">
  149. @foreach ($item['details']['examples'] as $example)
  150. <div class="rounded bg-slate-50 px-3 py-2 font-mono text-xs text-slate-700">{{ $example }}</div>
  151. @endforeach
  152. </div>
  153. </div>
  154. @endif
  155. {{-- 在线测试按钮 --}}
  156. @if ($item['doc'])
  157. <div class="pt-3 border-t border-slate-200">
  158. <button
  159. type="button"
  160. class="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700"
  161. onclick="toggleApiTest('{{ $item['uri'] }}', '{{ $item['method'] }}')"
  162. >
  163. 在线测试
  164. </button>
  165. </div>
  166. {{-- 测试表单 --}}
  167. <div id="test-form-{{ $item['uri'] }}-{{ $item['method'] }}" class="mt-4 hidden rounded-lg border border-slate-200 bg-slate-50 p-4">
  168. <div class="mb-3 text-sm font-semibold text-slate-700">API 测试</div>
  169. <form onsubmit="testApi(event, '{{ $item['uri'] }}', '{{ $item['method'] }}')">
  170. @if (!empty($item['details']['param_details']))
  171. @foreach ($item['details']['param_details'] as $paramGroup)
  172. @if (!empty($paramGroup[0]) && is_string($paramGroup[0]))
  173. @php $params = array_slice($paramGroup, 1) @endphp
  174. @else
  175. @php $params = $paramGroup @endphp
  176. @endif
  177. @foreach ($params as $param)
  178. @if ($paramGroup === $params && $paramGroup[0] === 'query' || (is_array($paramGroup[0]) && $paramGroup[0] === $params))
  179. <div class="mb-3">
  180. <label class="mb-1 block text-xs font-semibold text-slate-600">{{ $param['name'] }} ({{ $param['type'] }})</label>
  181. @if ($param['required'])
  182. <input
  183. type="text"
  184. name="query_{{ $param['name'] }}"
  185. class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"
  186. placeholder="{{ $param['description'] }}"
  187. />
  188. @else
  189. <input
  190. type="text"
  191. name="query_{{ $param['name'] }}"
  192. class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm"
  193. placeholder="{{ $param['description'] }} (可选)"
  194. />
  195. @endif
  196. </div>
  197. @endif
  198. @endforeach
  199. @endforeach
  200. @endif
  201. @if (in_array($item['method'], ['POST', 'PUT', 'PATCH']))
  202. <div class="mb-3">
  203. <label class="mb-1 block text-xs font-semibold text-slate-600">请求体 (JSON)</label>
  204. <textarea
  205. name="request_body"
  206. rows="5"
  207. class="w-full rounded-lg border border-slate-300 px-3 py-2 text-sm font-mono"
  208. placeholder='{"example": "value"}'
  209. ></textarea>
  210. </div>
  211. @endif
  212. <div class="flex gap-2">
  213. <button
  214. type="submit"
  215. class="rounded-lg bg-primary-600 px-4 py-2 text-sm font-semibold text-white hover:bg-primary-700"
  216. >
  217. 发送请求
  218. </button>
  219. <button
  220. type="button"
  221. class="rounded-lg border border-slate-300 px-4 py-2 text-sm font-semibold text-slate-700 hover:bg-slate-100"
  222. onclick="toggleApiTest('{{ $item['uri'] }}', '{{ $item['method'] }}')"
  223. >
  224. 取消
  225. </button>
  226. </div>
  227. </form>
  228. <div id="test-response-{{ $item['uri'] }}-{{ $item['method'] }}" class="mt-4 hidden">
  229. <div class="mb-2 text-xs font-semibold text-slate-600">响应结果</div>
  230. <pre class="rounded-lg bg-slate-900 p-3 text-xs text-slate-100 overflow-x-auto"></pre>
  231. </div>
  232. </div>
  233. @endif
  234. </div>
  235. </details>
  236. @endforeach
  237. </div>
  238. </x-filament::section>
  239. @endforeach
  240. </div>
  241. <script>
  242. async function loadPaperJson() {
  243. const input = document.getElementById('paper-json-id');
  244. const output = document.getElementById('paper-json-output');
  245. const paperId = (input?.value || '').trim();
  246. if (!paperId) {
  247. output.textContent = '请输入 paper_id';
  248. return;
  249. }
  250. output.textContent = '加载中...';
  251. try {
  252. const response = await fetch(`/api/papers/${encodeURIComponent(paperId)}/json`);
  253. const data = await response.json();
  254. output.textContent = JSON.stringify(data, null, 2);
  255. } catch (error) {
  256. output.textContent = `请求失败: ${error}`;
  257. }
  258. }
  259. function downloadPaperJson() {
  260. const input = document.getElementById('paper-json-id');
  261. const paperId = (input?.value || '').trim();
  262. if (!paperId) {
  263. return;
  264. }
  265. window.location.href = `/api/papers/${encodeURIComponent(paperId)}/json?download=1`;
  266. }
  267. function toggleApiTest(uri, method) {
  268. const formId = `test-form-${uri}-${method}`;
  269. const form = document.getElementById(formId);
  270. if (form) {
  271. form.classList.toggle('hidden');
  272. }
  273. }
  274. async function testApi(event, uri, method) {
  275. event.preventDefault();
  276. const formId = `test-form-${uri}-${method}`;
  277. const responseId = `test-response-${uri}-${method}`;
  278. const form = document.getElementById(formId);
  279. const responseDiv = document.getElementById(responseId);
  280. const pre = responseDiv.querySelector('pre');
  281. // 构建 URL
  282. let url = uri;
  283. const queryParams = new URLSearchParams();
  284. // 收集查询参数
  285. const formData = new FormData(event.target);
  286. for (let [key, value] of formData.entries()) {
  287. if (key.startsWith('query_')) {
  288. const paramName = key.replace('query_', '');
  289. if (value) {
  290. queryParams.append(paramName, value);
  291. }
  292. }
  293. }
  294. // 替换路径参数
  295. const pathParams = uri.match(/\{([^}]+)\}/g);
  296. if (pathParams) {
  297. pathParams.forEach(param => {
  298. const paramName = param.slice(1, -1);
  299. const value = formData.get(`query_${paramName}`);
  300. if (value) {
  301. url = url.replace(param, encodeURIComponent(value));
  302. }
  303. });
  304. }
  305. if (queryParams.toString()) {
  306. url += '?' + queryParams.toString();
  307. }
  308. // 准备请求选项
  309. const options = {
  310. method: method,
  311. headers: {
  312. 'Content-Type': 'application/json',
  313. 'Accept': 'application/json',
  314. }
  315. };
  316. // 添加请求体
  317. const body = formData.get('request_body');
  318. if (body) {
  319. try {
  320. options.body = JSON.stringify(JSON.parse(body));
  321. } catch (e) {
  322. pre.textContent = 'JSON 格式错误: ' + e.message;
  323. responseDiv.classList.remove('hidden');
  324. return;
  325. }
  326. }
  327. try {
  328. pre.textContent = '发送中...';
  329. responseDiv.classList.remove('hidden');
  330. const response = await fetch(url, options);
  331. const data = await response.json();
  332. pre.textContent = `状态: ${response.status}\n\n` + JSON.stringify(data, null, 2);
  333. } catch (error) {
  334. pre.textContent = '请求失败: ' + error.message;
  335. }
  336. }
  337. </script>
  338. </x-filament::page>