<!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; margin: 20px; }
|
.result { margin: 10px 0; padding: 10px; border: 1px solid #ccc; }
|
.error { background-color: #ffe6e6; }
|
.success { background-color: #e6ffe6; }
|
button { padding: 10px 20px; margin: 5px; }
|
</style>
|
</head>
|
<body>
|
<h1>角色API调试</h1>
|
|
<button onclick="testWithoutAuth()">测试无认证API调用</button>
|
<button onclick="testWithAuth()">测试带认证API调用</button>
|
<button onclick="checkLocalStorage()">检查本地存储</button>
|
|
<div id="results"></div>
|
|
<script>
|
function addResult(content, isError = false) {
|
const div = document.createElement('div');
|
div.className = `result ${isError ? 'error' : 'success'}`;
|
div.innerHTML = content;
|
document.getElementById('results').appendChild(div);
|
}
|
|
async function testWithoutAuth() {
|
try {
|
const response = await fetch('/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json'
|
},
|
body: JSON.stringify({
|
query: `query GetActiveRoles {
|
activeRoles {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}`
|
})
|
});
|
|
const data = await response.json();
|
addResult(`<strong>无认证API调用结果:</strong><br>状态: ${response.status}<br>数据: <pre>${JSON.stringify(data, null, 2)}</pre>`);
|
} catch (error) {
|
addResult(`<strong>无认证API调用失败:</strong><br>${error.message}`, true);
|
}
|
}
|
|
async function testWithAuth() {
|
try {
|
const token = localStorage.getItem('token');
|
const response = await fetch('/api/graphql', {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
'Authorization': token ? `Bearer ${token}` : ''
|
},
|
body: JSON.stringify({
|
query: `query GetActiveRoles {
|
activeRoles {
|
id
|
code
|
name
|
description
|
state
|
createTime
|
updateTime
|
}
|
}`
|
})
|
});
|
|
const data = await response.json();
|
addResult(`<strong>带认证API调用结果:</strong><br>状态: ${response.status}<br>Token: ${token ? '存在' : '不存在'}<br>数据: <pre>${JSON.stringify(data, null, 2)}</pre>`);
|
} catch (error) {
|
addResult(`<strong>带认证API调用失败:</strong><br>${error.message}`, true);
|
}
|
}
|
|
function checkLocalStorage() {
|
const token = localStorage.getItem('token');
|
const user = localStorage.getItem('user');
|
addResult(`<strong>本地存储检查:</strong><br>Token: ${token || '无'}<br>User: ${user || '无'}`);
|
}
|
</script>
|
</body>
|
</html>
|