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>用户数据显示测试</title>
|
||
<style>
|
||
body {
|
||
font-family: Arial, sans-serif;
|
||
max-width: 1000px;
|
||
margin: 50px auto;
|
||
padding: 20px;
|
||
background-color: #f5f5f5;
|
||
}
|
||
.container {
|
||
background: white;
|
||
padding: 30px;
|
||
border-radius: 8px;
|
||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||
}
|
||
.test-item {
|
||
margin: 20px 0;
|
||
padding: 15px;
|
||
border: 1px solid #ddd;
|
||
border-radius: 4px;
|
||
}
|
||
.success {
|
||
background-color: #f6ffed;
|
||
border-color: #b7eb8f;
|
||
}
|
||
.error {
|
||
background-color: #fff2f0;
|
||
border-color: #ffccc7;
|
||
}
|
||
button {
|
||
background-color: #1890ff;
|
||
color: white;
|
||
border: none;
|
||
padding: 8px 16px;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
margin: 5px;
|
||
}
|
||
button:hover {
|
||
background-color: #40a9ff;
|
||
}
|
||
table {
|
||
width: 100%;
|
||
border-collapse: collapse;
|
||
margin-top: 15px;
|
||
}
|
||
th, td {
|
||
border: 1px solid #ddd;
|
||
padding: 8px;
|
||
text-align: left;
|
||
}
|
||
th {
|
||
background-color: #f5f5f5;
|
||
}
|
||
.json-display {
|
||
background: #f8f8f8;
|
||
padding: 10px;
|
||
border-radius: 4px;
|
||
font-family: monospace;
|
||
white-space: pre-wrap;
|
||
max-height: 300px;
|
||
overflow-y: auto;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<h1>用户数据显示测试</h1>
|
||
|
||
<div class="test-item">
|
||
<h3>测试步骤:</h3>
|
||
<ol>
|
||
<li>点击"测试API调用"按钮</li>
|
||
<li>查看API响应数据</li>
|
||
<li>点击"访问用户管理页面"查看前端显示</li>
|
||
<li>对比数据是否一致</li>
|
||
</ol>
|
||
</div>
|
||
|
||
<div class="test-item">
|
||
<h3>API测试:</h3>
|
||
<button onclick="testAPI()">测试API调用</button>
|
||
<button onclick="visitUsersPage()">访问用户管理页面</button>
|
||
<button onclick="checkLocalStorage()">检查本地存储</button>
|
||
</div>
|
||
|
||
<div class="test-item">
|
||
<h3>API响应数据:</h3>
|
||
<div id="api-response" class="json-display">点击"测试API调用"查看数据...</div>
|
||
</div>
|
||
|
||
<div class="test-item">
|
||
<h3>用户数据表格:</h3>
|
||
<div id="users-table">暂无数据</div>
|
||
</div>
|
||
|
||
<div class="test-item">
|
||
<h3>测试结果:</h3>
|
||
<div id="test-results"></div>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
function addResult(message, isSuccess = true) {
|
||
const results = document.getElementById('test-results');
|
||
const div = document.createElement('div');
|
||
div.className = isSuccess ? 'success' : 'error';
|
||
div.style.margin = '10px 0';
|
||
div.style.padding = '10px';
|
||
div.style.borderRadius = '4px';
|
||
div.innerHTML = `<strong>${new Date().toLocaleTimeString()}</strong>: ${message}`;
|
||
results.appendChild(div);
|
||
}
|
||
|
||
async function testAPI() {
|
||
try {
|
||
addResult('正在调用用户API...');
|
||
|
||
const token = localStorage.getItem('token');
|
||
if (!token) {
|
||
addResult('未找到token,请先登录', false);
|
||
return;
|
||
}
|
||
|
||
const response = await fetch('http://localhost:5350/api/users', {
|
||
headers: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
}
|
||
});
|
||
|
||
if (!response.ok) {
|
||
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
||
}
|
||
|
||
const data = await response.json();
|
||
|
||
// 显示API响应
|
||
document.getElementById('api-response').textContent = JSON.stringify(data, null, 2);
|
||
|
||
// 创建用户表格
|
||
if (data.success && data.data) {
|
||
createUsersTable(data.data);
|
||
addResult(`API调用成功,获取到 ${data.data.length} 个用户`);
|
||
} else {
|
||
addResult('API响应格式异常', false);
|
||
}
|
||
|
||
} catch (error) {
|
||
addResult(`API调用失败: ${error.message}`, false);
|
||
document.getElementById('api-response').textContent = `错误: ${error.message}`;
|
||
}
|
||
}
|
||
|
||
function createUsersTable(users) {
|
||
const tableContainer = document.getElementById('users-table');
|
||
|
||
if (!users || users.length === 0) {
|
||
tableContainer.innerHTML = '<p>没有用户数据</p>';
|
||
return;
|
||
}
|
||
|
||
let tableHTML = `
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th>ID</th>
|
||
<th>用户名</th>
|
||
<th>邮箱</th>
|
||
<th>角色</th>
|
||
<th>状态</th>
|
||
<th>创建时间</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
`;
|
||
|
||
users.forEach(user => {
|
||
tableHTML += `
|
||
<tr>
|
||
<td>${user.id}</td>
|
||
<td>${user.username}</td>
|
||
<td>${user.email}</td>
|
||
<td>${user.role || '未知'}</td>
|
||
<td>${user.status || '未知'}</td>
|
||
<td>${user.created_at ? new Date(user.created_at).toLocaleString('zh-CN') : '未知'}</td>
|
||
</tr>
|
||
`;
|
||
});
|
||
|
||
tableHTML += `
|
||
</tbody>
|
||
</table>
|
||
`;
|
||
|
||
tableContainer.innerHTML = tableHTML;
|
||
}
|
||
|
||
function visitUsersPage() {
|
||
addResult('正在打开用户管理页面...');
|
||
window.open('http://localhost:5300/#/users', '_blank');
|
||
}
|
||
|
||
function checkLocalStorage() {
|
||
const token = localStorage.getItem('token');
|
||
const user = localStorage.getItem('user');
|
||
|
||
addResult(`Token: ${token ? '存在' : '不存在'}, 用户信息: ${user ? '存在' : '不存在'}`);
|
||
|
||
if (token) {
|
||
addResult('Token内容: ' + token.substring(0, 50) + '...');
|
||
}
|
||
}
|
||
|
||
// 页面加载时检查登录状态
|
||
window.onload = function() {
|
||
checkLocalStorage();
|
||
addResult('测试页面已加载,请按照测试步骤进行操作');
|
||
};
|
||
</script>
|
||
</body>
|
||
</html> |