-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug-token.html
More file actions
245 lines (213 loc) · 8.79 KB
/
debug-token.html
File metadata and controls
245 lines (213 loc) · 8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Token 调试工具</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.info {
background: #e3f2fd;
padding: 15px;
border-radius: 4px;
margin: 10px 0;
border-left: 4px solid #2196f3;
}
.error {
background: #ffebee;
color: #c62828;
padding: 15px;
border-radius: 4px;
margin: 10px 0;
border-left: 4px solid #f44336;
}
.success {
background: #e8f5e8;
color: #2e7d32;
padding: 15px;
border-radius: 4px;
margin: 10px 0;
border-left: 4px solid #4caf50;
}
pre {
background: #f8f8f8;
padding: 10px;
border-radius: 4px;
overflow-x: auto;
white-space: pre-wrap;
word-break: break-all;
}
button {
background: #2196f3;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
margin: 5px;
}
button:hover { background: #1976d2; }
.red-btn { background: #f44336; }
.red-btn:hover { background: #d32f2f; }
</style>
</head>
<body>
<div class="container">
<h1>🔐 Token 调试工具</h1>
<h2>检查存储状态</h2>
<button onclick="checkStorage()">🔍 检查 Token</button>
<button onclick="checkUser()">👤 检查用户信息</button>
<button onclick="testAPI()">🌐 测试 API</button>
<button onclick="clearStorage()" class="red-btn">🗑️ 清除存储</button>
<div id="results"></div>
<h2>手动设置 Token (测试用)</h2>
<div>
<input type="text" id="tokenInput" placeholder="输入测试 token..." style="width: 60%; padding: 8px;">
<button onclick="setTestToken()">设置 Token</button>
</div>
<h2>用户信息设置</h2>
<div>
<textarea id="userInput" placeholder="输入用户信息 JSON..." style="width: 100%; height: 100px; padding: 8px; margin: 5px 0;"></textarea>
<button onclick="setTestUser()">设置用户信息</button>
</div>
</div>
<script>
function showResult(content, type = 'info') {
const results = document.getElementById('results');
const div = document.createElement('div');
div.className = type;
div.innerHTML = content;
results.appendChild(div);
results.scrollTop = results.scrollHeight;
}
function checkStorage() {
showResult('<h3>🔍 存储检查结果</h3>');
// 检查 localStorage
const localToken = localStorage.getItem('copus_token');
const localUser = localStorage.getItem('copus_user');
// 检查 sessionStorage
const sessionToken = sessionStorage.getItem('copus_token');
const sessionUser = sessionStorage.getItem('copus_user');
showResult(`
<strong>localStorage:</strong><br>
Token: ${localToken ? '✅ 存在' : '❌ 不存在'}<br>
User: ${localUser ? '✅ 存在' : '❌ 不存在'}<br><br>
<strong>sessionStorage:</strong><br>
Token: ${sessionToken ? '✅ 存在' : '❌ 不存在'}<br>
User: ${sessionUser ? '✅ 存在' : '❌ 不存在'}<br>
`);
if (localToken) {
showResult(`<strong>localStorage Token 详情:</strong><br><pre>${localToken}</pre>`);
const parts = localToken.split('.');
showResult(`Token 格式: ${parts.length === 3 ? '✅ 有效 JWT' : '❌ 无效格式'} (${parts.length} 部分)`);
}
if (sessionToken) {
showResult(`<strong>sessionStorage Token 详情:</strong><br><pre>${sessionToken}</pre>`);
const parts = sessionToken.split('.');
showResult(`Token 格式: ${parts.length === 3 ? '✅ 有效 JWT' : '❌ 无效格式'} (${parts.length} 部分)`);
}
}
function checkUser() {
showResult('<h3>👤 用户信息检查</h3>');
const localUser = localStorage.getItem('copus_user');
const sessionUser = sessionStorage.getItem('copus_user');
if (localUser) {
try {
const user = JSON.parse(localUser);
showResult(`<strong>localStorage 用户信息:</strong><br><pre>${JSON.stringify(user, null, 2)}</pre>`, 'success');
} catch (e) {
showResult(`<strong>localStorage 用户信息解析失败:</strong><br>${e.message}`, 'error');
}
}
if (sessionUser) {
try {
const user = JSON.parse(sessionUser);
showResult(`<strong>sessionStorage 用户信息:</strong><br><pre>${JSON.stringify(user, null, 2)}</pre>`, 'success');
} catch (e) {
showResult(`<strong>sessionStorage 用户信息解析失败:</strong><br>${e.message}`, 'error');
}
}
if (!localUser && !sessionUser) {
showResult('❌ 没有找到用户信息', 'error');
}
}
async function testAPI() {
showResult('<h3>🌐 API 测试</h3>');
const token = localStorage.getItem('copus_token') || sessionStorage.getItem('copus_token');
if (!token) {
showResult('❌ 没有找到 token,无法测试 API', 'error');
return;
}
try {
showResult('📤 正在测试评论 API...');
const response = await fetch('https://api-test.copus.network/client/reader/article/comment/page?articleId=1&pageIndex=1&pageSize=10', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
showResult(`📥 响应状态: ${response.status} ${response.statusText}`);
const data = await response.json();
showResult(`📊 API 响应:<br><pre>${JSON.stringify(data, null, 2)}</pre>`,
data.success ? 'success' : 'error');
} catch (error) {
showResult(`💥 API 测试失败:<br><pre>${error.message}</pre>`, 'error');
}
}
function clearStorage() {
if (confirm('确定要清除所有存储数据吗?')) {
localStorage.removeItem('copus_token');
localStorage.removeItem('copus_user');
localStorage.removeItem('copus_auth_method');
sessionStorage.removeItem('copus_token');
sessionStorage.removeItem('copus_user');
sessionStorage.removeItem('copus_auth_method');
showResult('🗑️ 所有存储数据已清除', 'success');
}
}
function setTestToken() {
const token = document.getElementById('tokenInput').value.trim();
if (!token) {
showResult('❌ 请输入 token', 'error');
return;
}
localStorage.setItem('copus_token', token);
showResult('✅ Token 已设置到 localStorage', 'success');
}
function setTestUser() {
const userJson = document.getElementById('userInput').value.trim();
if (!userJson) {
showResult('❌ 请输入用户信息', 'error');
return;
}
try {
// 验证 JSON 格式
JSON.parse(userJson);
localStorage.setItem('copus_user', userJson);
showResult('✅ 用户信息已设置到 localStorage', 'success');
} catch (e) {
showResult(`❌ JSON 格式错误: ${e.message}`, 'error');
}
}
// 页面加载时自动检查
window.onload = function() {
showResult('<h3>🚀 页面加载完成,自动检查存储状态</h3>');
checkStorage();
checkUser();
};
</script>
</body>
</html>