blob: 5cac11456f88f1fd4ceee1fcf024b9059e4c90c0 [file] [log] [blame]
Your Name695f6a62025-05-17 00:58:04 +08001<!DOCTYPE html>
2<html lang="zh-CN">
Your Name4fbe8112025-05-20 23:24:17 +08003<head>
4 <meta charset="UTF-8">
5 <title>首页</title>
6</head>
Your Name695f6a62025-05-17 00:58:04 +08007<body>
8<h1>欢迎访问受保护页面</h1>
9<div id="userInfo"></div>
10<script>
Your Name4fbe8112025-05-20 23:24:17 +080011 (async () => {
12 const token = localStorage.getItem('token'); // 从 localStorage 获取 token
13 if (!token) return window.location.href = 'login.html'; // 如果 token 不存在,重定向到登录页面
14
Your Name695f6a62025-05-17 00:58:04 +080015 const res = await fetch('/api/me', {
Your Name4fbe8112025-05-20 23:24:17 +080016 headers: { 'token': token } // 使用 token 请求用户信息
Your Name695f6a62025-05-17 00:58:04 +080017 });
Your Name4fbe8112025-05-20 23:24:17 +080018
19 const json = await res.json(); // 解析响应数据
20 if (json.code !== 0) {
21 return window.location.href = 'login.html'; // 如果获取用户信息失败,重定向到登录页面
Your Name695f6a62025-05-17 00:58:04 +080022 }
Your Name4fbe8112025-05-20 23:24:17 +080023
24 // 输出当前用户和 token
Your Name695f6a62025-05-17 00:58:04 +080025 document.getElementById('userInfo')
Your Name4fbe8112025-05-20 23:24:17 +080026 .innerText = '当前用户:' + json.data.username + ',Token:' + json.data; // 显示用户名和 token
Your Name695f6a62025-05-17 00:58:04 +080027 })();
28</script>
29</body>
Your Name4fbe8112025-05-20 23:24:17 +080030</html>