| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- /* Swagger UI uses the same origin as this page; no token or specification is sent to third parties. */
- 'use strict';
- const apiBase = `${window.location.origin}/api/mini/v1`;
- const statusElement = document.getElementById('service-status');
- const refreshButton = document.getElementById('refresh-status');
- document.getElementById('base-url').textContent = apiBase;
- async function refreshStatus() {
- refreshButton.disabled = true;
- statusElement.textContent = '正在检查服务状态…';
- const controller = new AbortController();
- const timeout = setTimeout(() => controller.abort(), 10000);
- try {
- const response = await fetch(`${apiBase}/config`, {
- cache: 'no-store', credentials: 'omit', signal: controller.signal,
- });
- const body = await response.json();
- if (!response.ok || body.code !== 0) throw new Error('服务状态读取失败');
- const capabilities = body.data.capabilities;
- statusElement.textContent = `服务可访问 · 微信登录${capabilities.wechat_login ? '已配置' : '未配置'} · 支付${capabilities.payment ? '已开放' : '未开放'}`;
- } catch {
- statusElement.textContent = '服务状态读取失败,请检查后端服务和来源白名单';
- } finally {
- clearTimeout(timeout);
- refreshButton.disabled = false;
- }
- }
- async function loadDocumentation() {
- try {
- if (typeof SwaggerUIBundle !== 'function') throw new Error('Swagger UI 静态资源加载失败');
- const response = await fetch('./openapi.json', { cache: 'no-store', credentials: 'omit' });
- if (!response.ok) throw new Error(`OpenAPI 文件加载失败(HTTP ${response.status})`);
- const specification = await response.json();
- specification.servers = [{ url: apiBase, description: '当前服务' }];
- const methods = new Set(['get', 'head', 'post', 'put', 'patch', 'delete', 'options', 'trace']);
- const count = Object.values(specification.paths)
- .reduce((total, item) => total + Object.keys(item).filter(key => methods.has(key)).length, 0);
- document.getElementById('operation-count').textContent = `${count} 个接口操作(含 HEAD)`;
- window.ui = SwaggerUIBundle({
- spec: specification,
- dom_id: '#swagger-ui',
- deepLinking: true,
- filter: true,
- docExpansion: 'list',
- defaultModelsExpandDepth: -1,
- displayRequestDuration: true,
- persistAuthorization: false,
- withCredentials: false,
- validatorUrl: null,
- queryConfigEnabled: false,
- presets: [SwaggerUIBundle.presets.apis],
- layout: 'BaseLayout',
- requestInterceptor(request) {
- const target = new URL(request.url, window.location.href);
- if (target.origin !== window.location.origin || !target.pathname.startsWith('/api/mini/v1/')) {
- throw new Error('此文档页面仅允许调用当前服务的小程序接口');
- }
- request.credentials = 'omit';
- return request;
- },
- });
- } catch (error) {
- const element = document.getElementById('load-error');
- element.textContent = `${error.message}。请刷新页面;也可以下载 OpenAPI 导入 Apifox。`;
- element.hidden = false;
- }
- }
- refreshButton.addEventListener('click', refreshStatus);
- refreshStatus();
- loadDocumentation();
|