187 lines
6.0 KiB
HTML
187 lines
6.0 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;
|
||
margin: 20px;
|
||
background-color: #f5f5f5;
|
||
}
|
||
.container {
|
||
max-width: 800px;
|
||
margin: 0 auto;
|
||
background: white;
|
||
padding: 20px;
|
||
border-radius: 8px;
|
||
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
|
||
}
|
||
.status {
|
||
padding: 10px;
|
||
margin: 10px 0;
|
||
border-radius: 4px;
|
||
}
|
||
.success {
|
||
background-color: #d4edda;
|
||
color: #155724;
|
||
border: 1px solid #c3e6cb;
|
||
}
|
||
.error {
|
||
background-color: #f8d7da;
|
||
color: #721c24;
|
||
border: 1px solid #f5c6cb;
|
||
}
|
||
.info {
|
||
background-color: #d1ecf1;
|
||
color: #0c5460;
|
||
border: 1px solid #bee5eb;
|
||
}
|
||
button {
|
||
background-color: #007bff;
|
||
color: white;
|
||
border: none;
|
||
padding: 10px 20px;
|
||
border-radius: 4px;
|
||
cursor: pointer;
|
||
margin: 5px;
|
||
}
|
||
button:hover {
|
||
background-color: #0056b3;
|
||
}
|
||
pre {
|
||
background-color: #f8f9fa;
|
||
padding: 10px;
|
||
border-radius: 4px;
|
||
overflow-x: auto;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<h1>用户管理调试页面</h1>
|
||
|
||
<div id="tokenStatus" class="status info">
|
||
检查中...
|
||
</div>
|
||
|
||
<div id="apiStatus" class="status info">
|
||
等待测试...
|
||
</div>
|
||
|
||
<div>
|
||
<button onclick="checkToken()">检查Token</button>
|
||
<button onclick="testLogin()">测试登录</button>
|
||
<button onclick="testUsersAPI()">测试用户API</button>
|
||
<button onclick="clearToken()">清除Token</button>
|
||
</div>
|
||
|
||
<div id="results">
|
||
<h3>测试结果:</h3>
|
||
<pre id="output"></pre>
|
||
</div>
|
||
</div>
|
||
|
||
<script>
|
||
const API_BASE_URL = 'http://localhost:5350/api';
|
||
|
||
function log(message) {
|
||
const output = document.getElementById('output');
|
||
output.textContent += new Date().toLocaleTimeString() + ': ' + message + '\n';
|
||
}
|
||
|
||
function checkToken() {
|
||
const token = localStorage.getItem('token');
|
||
const tokenStatus = document.getElementById('tokenStatus');
|
||
|
||
if (token) {
|
||
tokenStatus.className = 'status success';
|
||
tokenStatus.textContent = `Token存在 (长度: ${token.length})`;
|
||
log('Token存在: ' + token.substring(0, 50) + '...');
|
||
} else {
|
||
tokenStatus.className = 'status error';
|
||
tokenStatus.textContent = 'Token不存在';
|
||
log('Token不存在');
|
||
}
|
||
}
|
||
|
||
async function testLogin() {
|
||
try {
|
||
log('开始测试登录...');
|
||
const response = await fetch(`${API_BASE_URL}/auth/login`, {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json'
|
||
},
|
||
body: JSON.stringify({
|
||
username: 'admin',
|
||
password: '123456'
|
||
})
|
||
});
|
||
|
||
const data = await response.json();
|
||
log('登录响应: ' + JSON.stringify(data, null, 2));
|
||
|
||
if (data.success && data.token) {
|
||
localStorage.setItem('token', data.token);
|
||
log('Token已保存到localStorage');
|
||
checkToken();
|
||
} else {
|
||
log('登录失败: ' + data.message);
|
||
}
|
||
} catch (error) {
|
||
log('登录错误: ' + error.message);
|
||
}
|
||
}
|
||
|
||
async function testUsersAPI() {
|
||
try {
|
||
log('开始测试用户API...');
|
||
const token = localStorage.getItem('token');
|
||
|
||
if (!token) {
|
||
log('错误: 没有找到token,请先登录');
|
||
return;
|
||
}
|
||
|
||
const response = await fetch(`${API_BASE_URL}/users`, {
|
||
method: 'GET',
|
||
headers: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
}
|
||
});
|
||
|
||
const data = await response.json();
|
||
log('用户API响应: ' + JSON.stringify(data, null, 2));
|
||
|
||
const apiStatus = document.getElementById('apiStatus');
|
||
if (data.success && data.data) {
|
||
apiStatus.className = 'status success';
|
||
apiStatus.textContent = `API正常 (获取到${data.data.length}个用户)`;
|
||
} else {
|
||
apiStatus.className = 'status error';
|
||
apiStatus.textContent = 'API异常';
|
||
}
|
||
} catch (error) {
|
||
log('用户API错误: ' + error.message);
|
||
const apiStatus = document.getElementById('apiStatus');
|
||
apiStatus.className = 'status error';
|
||
apiStatus.textContent = 'API调用失败';
|
||
}
|
||
}
|
||
|
||
function clearToken() {
|
||
localStorage.removeItem('token');
|
||
log('Token已清除');
|
||
checkToken();
|
||
}
|
||
|
||
// 页面加载时自动检查token
|
||
window.onload = function() {
|
||
checkToken();
|
||
};
|
||
</script>
|
||
</body>
|
||
</html> |