diagnose.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>401跳转诊断工具</title>
  7. <style>
  8. * { margin: 0; padding: 0; box-sizing: border-box; }
  9. body {
  10. font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  11. background: #f5f7fa;
  12. padding: 20px;
  13. }
  14. .container {
  15. max-width: 900px;
  16. margin: 0 auto;
  17. background: white;
  18. border-radius: 8px;
  19. box-shadow: 0 2px 12px rgba(0,0,0,0.1);
  20. padding: 30px;
  21. }
  22. h1 {
  23. color: #303133;
  24. margin-bottom: 10px;
  25. }
  26. .subtitle {
  27. color: #909399;
  28. margin-bottom: 30px;
  29. }
  30. .test-section {
  31. margin: 20px 0;
  32. padding: 20px;
  33. background: #f9fafc;
  34. border-radius: 6px;
  35. border-left: 4px solid #409eff;
  36. }
  37. .test-section h3 {
  38. color: #303133;
  39. margin-bottom: 15px;
  40. }
  41. button {
  42. padding: 12px 24px;
  43. margin: 5px;
  44. font-size: 14px;
  45. cursor: pointer;
  46. background: #409eff;
  47. color: white;
  48. border: none;
  49. border-radius: 4px;
  50. transition: all 0.3s;
  51. }
  52. button:hover {
  53. background: #66b1ff;
  54. }
  55. button.success {
  56. background: #67c23a;
  57. }
  58. button.danger {
  59. background: #f56c6c;
  60. }
  61. .result {
  62. margin-top: 15px;
  63. padding: 15px;
  64. background: white;
  65. border-radius: 4px;
  66. border: 1px solid #dcdfe6;
  67. font-family: 'Courier New', monospace;
  68. font-size: 13px;
  69. max-height: 300px;
  70. overflow-y: auto;
  71. }
  72. .result-item {
  73. margin: 5px 0;
  74. padding: 5px;
  75. }
  76. .success-text { color: #67c23a; }
  77. .error-text { color: #f56c6c; }
  78. .info-text { color: #409eff; }
  79. .warning-text { color: #e6a23c; }
  80. </style>
  81. </head>
  82. <body>
  83. <div class="container">
  84. <h1>🔍 401跳转诊断工具</h1>
  85. <p class="subtitle">用于诊断前端401响应跳转问题</p>
  86. <!-- 测试1: 基本跳转功能 -->
  87. <div class="test-section">
  88. <h3>测试1: 基本跳转功能</h3>
  89. <p>测试 window.location.href 是否能正常工作</p>
  90. <button onclick="test1()">执行测试1</button>
  91. <div id="result1" class="result" style="display:none;"></div>
  92. </div>
  93. <!-- 测试2: localStorage操作 -->
  94. <div class="test-section">
  95. <h3>测试2: localStorage操作</h3>
  96. <p>测试是否能正常读写localStorage</p>
  97. <button onclick="test2()">执行测试2</button>
  98. <div id="result2" class="result" style="display:none;"></div>
  99. </div>
  100. <!-- 测试3: 模拟401响应 -->
  101. <div class="test-section">
  102. <h3>测试3: 模拟401响应</h3>
  103. <p>向后台发送无效token,测试是否返回401</p>
  104. <button onclick="test3()">执行测试3</button>
  105. <div id="result3" class="result" style="display:none;"></div>
  106. </div>
  107. <!-- 测试4: 检查前端代码 -->
  108. <div class="test-section">
  109. <h3>测试4: 检查前端代码</h3>
  110. <p>检查request.ts是否包含最新的401处理代码</p>
  111. <button onclick="test4()">执行测试4</button>
  112. <div id="result4" class="result" style="display:none;"></div>
  113. </div>
  114. <!-- 测试5: 完整流程测试 -->
  115. <div class="test-section">
  116. <h3>测试5: 完整流程测试</h3>
  117. <p>模拟完整的401错误处理流程</p>
  118. <button onclick="test5()">执行测试5</button>
  119. <div id="result5" class="result" style="display:none;"></div>
  120. </div>
  121. <!-- 一键诊断 -->
  122. <div class="test-section" style="border-left-color: #67c23a;">
  123. <h3>🚀 一键诊断</h3>
  124. <p>自动执行所有测试</p>
  125. <button class="success" onclick="runAllTests()">运行所有测试</button>
  126. </div>
  127. </div>
  128. <script>
  129. function log(resultId, message, type = 'info') {
  130. const result = document.getElementById(resultId);
  131. result.style.display = 'block';
  132. const item = document.createElement('div');
  133. item.className = `result-item ${type}-text`;
  134. item.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
  135. result.appendChild(item);
  136. result.scrollTop = result.scrollHeight;
  137. }
  138. function clearLog(resultId) {
  139. const result = document.getElementById(resultId);
  140. result.innerHTML = '';
  141. result.style.display = 'none';
  142. }
  143. // 测试1: 基本跳转功能
  144. function test1() {
  145. clearLog('result1');
  146. log('result1', '开始测试基本跳转功能...', 'info');
  147. log('result1', '✓ window.location 对象存在', 'success');
  148. log('result1', `当前URL: ${window.location.href}`, 'info');
  149. log('result1', '3秒后将跳转到登录页...', 'warning');
  150. let countdown = 3;
  151. const timer = setInterval(() => {
  152. log('result1', `${countdown}...`, 'warning');
  153. countdown--;
  154. if (countdown < 0) {
  155. clearInterval(timer);
  156. log('result1', '执行跳转: window.location.href = "/login"', 'info');
  157. window.location.href = '/login';
  158. }
  159. }, 1000);
  160. }
  161. // 测试2: localStorage操作
  162. function test2() {
  163. clearLog('result2');
  164. log('result2', '开始测试localStorage操作...', 'info');
  165. try {
  166. // 写入测试
  167. localStorage.setItem('test_key', 'test_value');
  168. log('result2', '✓ localStorage.setItem 成功', 'success');
  169. // 读取测试
  170. const value = localStorage.getItem('test_key');
  171. if (value === 'test_value') {
  172. log('result2', '✓ localStorage.getItem 成功', 'success');
  173. } else {
  174. log('result2', '✗ localStorage.getItem 失败', 'error');
  175. }
  176. // 删除测试
  177. localStorage.removeItem('test_key');
  178. log('result2', '✓ localStorage.removeItem 成功', 'success');
  179. // 检查token
  180. const token = localStorage.getItem('sso_access_token');
  181. if (token) {
  182. log('result2', `当前token: ${token.substring(0, 30)}...`, 'info');
  183. } else {
  184. log('result2', '当前没有token', 'info');
  185. }
  186. log('result2', '✓ localStorage功能正常', 'success');
  187. } catch (error) {
  188. log('result2', `✗ localStorage测试失败: ${error.message}`, 'error');
  189. }
  190. }
  191. // 测试3: 模拟401响应
  192. async function test3() {
  193. clearLog('result3');
  194. log('result3', '开始测试401响应...', 'info');
  195. const baseURL = 'http://localhost:8000'; // 修改为你的后台地址
  196. const testURL = `${baseURL}/api/v1/system/user/profile`;
  197. log('result3', `请求URL: ${testURL}`, 'info');
  198. log('result3', '使用无效token: invalid_token_for_test', 'info');
  199. try {
  200. const response = await fetch(testURL, {
  201. method: 'GET',
  202. headers: {
  203. 'Authorization': 'Bearer invalid_token_for_test',
  204. 'Content-Type': 'application/json'
  205. }
  206. });
  207. log('result3', `响应状态码: ${response.status}`, 'info');
  208. if (response.status === 401) {
  209. log('result3', '✓ 后台正确返回401', 'success');
  210. const data = await response.json();
  211. log('result3', `响应数据: ${JSON.stringify(data)}`, 'info');
  212. log('result3', '前端应该在此时跳转到登录页', 'warning');
  213. } else {
  214. log('result3', `✗ 后台返回了${response.status}而不是401`, 'error');
  215. }
  216. } catch (error) {
  217. log('result3', `✗ 请求失败: ${error.message}`, 'error');
  218. log('result3', '可能原因: 后台服务未启动或CORS问题', 'warning');
  219. }
  220. }
  221. // 测试4: 检查前端代码
  222. async function test4() {
  223. clearLog('result4');
  224. log('result4', '开始检查前端代码...', 'info');
  225. try {
  226. const response = await fetch('/src/api/request.ts');
  227. const code = await response.text();
  228. log('result4', `✓ 成功读取request.ts (${code.length}字符)`, 'success');
  229. // 检查关键代码
  230. const checks = [
  231. { text: '>>> 检测到401错误', name: '401检测日志' },
  232. { text: 'window.location.href', name: '跳转代码' },
  233. { text: 'localStorage.removeItem', name: '清除token代码' },
  234. { text: 'response.status === 401', name: '401判断' }
  235. ];
  236. checks.forEach(check => {
  237. if (code.includes(check.text)) {
  238. log('result4', `✓ 包含${check.name}`, 'success');
  239. } else {
  240. log('result4', `✗ 缺少${check.name}`, 'error');
  241. }
  242. });
  243. } catch (error) {
  244. log('result4', `✗ 无法读取request.ts: ${error.message}`, 'error');
  245. log('result4', '这是正常的,生产环境无法直接读取源码', 'info');
  246. }
  247. }
  248. // 测试5: 完整流程测试
  249. function test5() {
  250. clearLog('result5');
  251. log('result5', '开始完整流程测试...', 'info');
  252. log('result5', '步骤1: 设置无效token', 'info');
  253. localStorage.setItem('sso_access_token', 'invalid_token_for_test');
  254. log('result5', '✓ 已设置无效token', 'success');
  255. log('result5', '步骤2: 3秒后刷新页面', 'info');
  256. log('result5', '页面刷新后应该自动跳转到登录页', 'warning');
  257. let countdown = 3;
  258. const timer = setInterval(() => {
  259. log('result5', `${countdown}秒后刷新...`, 'warning');
  260. countdown--;
  261. if (countdown < 0) {
  262. clearInterval(timer);
  263. log('result5', '正在刷新页面...', 'info');
  264. location.reload();
  265. }
  266. }, 1000);
  267. }
  268. // 运行所有测试
  269. async function runAllTests() {
  270. await test2();
  271. await new Promise(resolve => setTimeout(resolve, 1000));
  272. await test3();
  273. await new Promise(resolve => setTimeout(resolve, 1000));
  274. await test4();
  275. alert('诊断完成!\n\n请查看各个测试的结果。\n\n如果测试3显示后台返回401,但页面没有跳转,\n说明前端代码可能没有正确加载。\n\n请重启前端服务器并清除浏览器缓存。');
  276. }
  277. </script>
  278. </body>
  279. </html>