227 lines
7.4 KiB
HTML
227 lines
7.4 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>聊天记录API测试</title>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
max-width: 800px;
|
|
margin: 0 auto;
|
|
padding: 20px;
|
|
}
|
|
.test-section {
|
|
margin: 20px 0;
|
|
padding: 15px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 5px;
|
|
}
|
|
.test-button {
|
|
background: #1890ff;
|
|
color: white;
|
|
border: none;
|
|
padding: 8px 16px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
margin: 5px;
|
|
}
|
|
.test-button:hover {
|
|
background: #40a9ff;
|
|
}
|
|
.result {
|
|
background: #f5f5f5;
|
|
padding: 10px;
|
|
border-radius: 4px;
|
|
margin-top: 10px;
|
|
white-space: pre-wrap;
|
|
font-family: monospace;
|
|
}
|
|
.error {
|
|
background: #fff2f0;
|
|
border: 1px solid #ffccc7;
|
|
color: #ff4d4f;
|
|
}
|
|
.success {
|
|
background: #f6ffed;
|
|
border: 1px solid #b7eb8f;
|
|
color: #52c41a;
|
|
}
|
|
.input-group {
|
|
margin: 10px 0;
|
|
}
|
|
.input-group label {
|
|
display: inline-block;
|
|
width: 100px;
|
|
font-weight: bold;
|
|
}
|
|
.input-group input {
|
|
padding: 5px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 4px;
|
|
width: 200px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>聊天记录API测试工具</h1>
|
|
|
|
<div class="test-section">
|
|
<h3>认证信息</h3>
|
|
<div class="input-group">
|
|
<label>Token:</label>
|
|
<input type="text" id="tokenInput" placeholder="请输入JWT Token">
|
|
</div>
|
|
<div class="input-group">
|
|
<label>API Base:</label>
|
|
<input type="text" id="apiBaseInput" value="http://localhost:8080" placeholder="API基础URL">
|
|
</div>
|
|
<button class="test-button" onclick="loadTokenFromStorage()">从localStorage加载Token</button>
|
|
</div>
|
|
|
|
<div class="test-section">
|
|
<h3>API测试</h3>
|
|
<button class="test-button" onclick="testGetUserMessages()">测试获取用户消息分页</button>
|
|
<button class="test-button" onclick="testSearchMessages()">测试搜索消息</button>
|
|
<button class="test-button" onclick="testGetRecentMessages()">测试获取最近消息</button>
|
|
<button class="test-button" onclick="testCurrentUser()">测试获取当前用户信息</button>
|
|
|
|
<div class="input-group">
|
|
<label>搜索关键词:</label>
|
|
<input type="text" id="searchKeyword" value="测试" placeholder="搜索关键词">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="test-section">
|
|
<h3>测试结果</h3>
|
|
<div id="testResult" class="result">等待测试...</div>
|
|
</div>
|
|
|
|
<script>
|
|
function getToken() {
|
|
return document.getElementById('tokenInput').value.trim();
|
|
}
|
|
|
|
function getApiBase() {
|
|
return document.getElementById('apiBaseInput').value.trim();
|
|
}
|
|
|
|
function loadTokenFromStorage() {
|
|
const token = localStorage.getItem('token');
|
|
if (token) {
|
|
document.getElementById('tokenInput').value = token;
|
|
showResult('Token已从localStorage加载', 'success');
|
|
} else {
|
|
showResult('localStorage中没有找到token', 'error');
|
|
}
|
|
}
|
|
|
|
function showResult(message, type = 'success') {
|
|
const resultDiv = document.getElementById('testResult');
|
|
resultDiv.textContent = message;
|
|
resultDiv.className = `result ${type}`;
|
|
}
|
|
|
|
async function makeRequest(url, options = {}) {
|
|
const token = getToken();
|
|
const apiBase = getApiBase();
|
|
|
|
if (!token) {
|
|
throw new Error('请先输入Token');
|
|
}
|
|
|
|
const fullUrl = `${apiBase}${url}`;
|
|
const headers = {
|
|
'Authorization': `Bearer ${token}`,
|
|
'Content-Type': 'application/json',
|
|
...options.headers
|
|
};
|
|
|
|
console.log('发送请求:', { url: fullUrl, headers });
|
|
|
|
const response = await fetch(fullUrl, {
|
|
...options,
|
|
headers
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
console.log('响应数据:', { status: response.status, data });
|
|
|
|
return { status: response.status, data };
|
|
}
|
|
|
|
async function testGetUserMessages() {
|
|
try {
|
|
showResult('正在测试获取用户消息分页...', 'success');
|
|
|
|
const result = await makeRequest('/message/user/page?current=1&size=10');
|
|
|
|
const message = `状态码: ${result.status}
|
|
响应数据: ${JSON.stringify(result.data, null, 2)}`;
|
|
|
|
showResult(message, result.status === 200 ? 'success' : 'error');
|
|
} catch (error) {
|
|
showResult(`错误: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
async function testSearchMessages() {
|
|
try {
|
|
const keyword = document.getElementById('searchKeyword').value.trim();
|
|
if (!keyword) {
|
|
showResult('请输入搜索关键词', 'error');
|
|
return;
|
|
}
|
|
|
|
showResult('正在测试搜索消息...', 'success');
|
|
|
|
const result = await makeRequest(`/message/user/search?keyword=${encodeURIComponent(keyword)}&limit=10`);
|
|
|
|
const message = `状态码: ${result.status}
|
|
响应数据: ${JSON.stringify(result.data, null, 2)}`;
|
|
|
|
showResult(message, result.status === 200 ? 'success' : 'error');
|
|
} catch (error) {
|
|
showResult(`错误: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
async function testGetRecentMessages() {
|
|
try {
|
|
showResult('正在测试获取最近消息...', 'success');
|
|
|
|
const result = await makeRequest('/message/user/recent?limit=10');
|
|
|
|
const message = `状态码: ${result.status}
|
|
响应数据: ${JSON.stringify(result.data, null, 2)}`;
|
|
|
|
showResult(message, result.status === 200 ? 'success' : 'error');
|
|
} catch (error) {
|
|
showResult(`错误: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
async function testCurrentUser() {
|
|
try {
|
|
showResult('正在测试获取当前用户信息...', 'success');
|
|
|
|
const result = await makeRequest('/user/current');
|
|
|
|
const message = `状态码: ${result.status}
|
|
响应数据: ${JSON.stringify(result.data, null, 2)}`;
|
|
|
|
showResult(message, result.status === 200 ? 'success' : 'error');
|
|
} catch (error) {
|
|
showResult(`错误: ${error.message}`, 'error');
|
|
}
|
|
}
|
|
|
|
// 页面加载时自动尝试加载token
|
|
window.onload = function() {
|
|
loadTokenFromStorage();
|
|
};
|
|
</script>
|
|
</body>
|
|
</html>
|