api-test/views/index.ejs

192 lines
6.9 KiB
Plaintext
Raw Permalink Normal View History

2024-10-14 00:16:08 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>API 请求测试</title>
<style>
/* 页面整体布局,左右分栏 */
.container {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 20px;
margin: 20px;
}
/* 左侧接口选择和输入框 */
.left-panel {
width: 35%;
padding: 20px;
background-color: #f9f9f9;
border: 1px solid #ccc;
border-radius: 5px;
flex-shrink: 0;
}
/* 右侧响应显示框 */
.right-panel {
flex: 1;
padding: 20px;
background-color: #f1f1f1;
border: 1px solid #ccc;
border-radius: 5px;
}
/* 布局样式 */
.radio-container {
display: flex;
flex-wrap: wrap;
gap: 10px; /* 控制每个radio的间距 */
justify-content: flex-start;
}
.radio-item {
flex: 1 0 20%; /* 每个radio占30%的宽度 */
box-sizing: border-box;
padding: 10px;
background-color: #fff;
border-radius: 5px;
border: 1px solid #ccc;
text-align: left;
}
/* 控制radio按钮的对齐 */
.radio-item input {
margin-right: 10px;
}
/* 输入框样式 */
textarea {
margin-top: 20px;
width: 100%;
height: 150px;
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
font-size: 14px;
}
/* 按钮样式 */
button {
margin-top: 20px;
padding: 10px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
button:disabled {
background-color: #ccc;
cursor: not-allowed;
}
/* 响应结果样式 */
#result {
min-height: 400px;
}
/* Loading 样式 */
.loading {
display: none;
font-size: 16px;
color: #007bff;
font-style: italic;
}
</style>
</head>
<body>
<h1>选择API并发送请求</h1>
<div class="container">
<!-- 左侧接口选择 -->
<div class="left-panel">
<form id="apiForm">
<div id="radioGroup" class="radio-container">
<% interfaces.forEach(function(interface) { %>
<label class="radio-item">
<input type="radio" name="interface" value="<%=
interface %>" <%= interface === interfaces[0] ?
'checked' : '' %> >
<strong><%= interface %></strong> - <%=
mockData[interface].description || '无描述' %>
</label>
<% }); %>
</div>
<textarea id="params" name="params" rows="10" cols="50">
</textarea>
<br />
<button type="submit" id="submitButton">发送请求</button>
<!-- Loading Text -->
<div class="loading" id="loadingText">
加载中,请稍候...
</div>
</form>
</div>
<!-- 右侧响应显示框 -->
<div class="right-panel">
<div id="result">响应结果将在此处显示</div>
</div>
</div>
<script>
// 使用 <%- JSON.stringify(mockData) %> 安全地将 JSON 数据插入到 JavaScript 中
const mockData = <%- JSON.stringify(mockData) %>;
document.querySelectorAll('input[name="interface"]').forEach(function (radio) {
radio.addEventListener('change', function () {
const selectedInterface = this.value;
// 动态填充默认的模拟数据
const defaultParams = JSON.stringify(mockData[selectedInterface], null, 2);
document.getElementById('params').value = defaultParams;
});
});
document.getElementById('apiForm').addEventListener('submit', async function (event) {
event.preventDefault();
// 获取相关元素
const interfaceName = document.querySelector('input[name="interface"]:checked').value;
const params = document.getElementById('params').value;
const submitButton = document.getElementById('submitButton');
const loadingText = document.getElementById('loadingText');
const resultDiv = document.getElementById('result');
// 禁用按钮并显示加载文本
submitButton.disabled = true;
loadingText.style.display = 'block';
try {
const response = await fetch('/request-api', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ interfaceName, params })
});
const result = await response.json();
resultDiv.innerHTML = `
<h2>请求结果:</h2>
2024-10-15 21:10:08 +08:00
<p>${result.code}</p>
<p>${result.message}</p>
2024-10-14 00:16:08 +08:00
<pre>加密返回: ${JSON.stringify(result.encryptedResponse, null, 2)}</pre>
<pre>解密返回: ${JSON.stringify(result.decryptedResponse, null, 2)}</pre>
`;
} catch (error) {
resultDiv.innerText = '请求失败: ' + error;
} finally {
// 请求完成,恢复按钮并隐藏加载文本
submitButton.disabled = false;
loadingText.style.display = 'none';
}
});
// 初始化时,自动填充第一个接口的默认参数
document.querySelector('input[name="interface"]:checked').dispatchEvent(new Event('change'));
</script>
</body>
</html>