feat(static): 首页添加登录态检测与进入笔记入口
- index.html: lang 改为 zh-CN,新增"关于本站"介绍段落,按钮改为有 id 的登录/注册/进入/退出四按钮(默认隐藏进入/退出) - 新增 index.js: POST /whoami 检测登录态(HttpOnly Cookie 自动携带),已登录则显示用户名+进入笔记/退出按钮,未登录显示登录/注册 - 退出按钮调用 POST /logout 后刷新页面
This commit is contained in:
1 parent
84adec4722
commit
1a5e9f7064
2 files changed
+61
-5
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="zh-CN">
|
||||||
<head>
|
<head>
|
||||||
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
@@ -24,6 +24,13 @@
|
|||||||
margin-left: 5px;
|
margin-left: 5px;
|
||||||
margin-right: 5px;
|
margin-right: 5px;
|
||||||
}
|
}
|
||||||
|
.site-intro {
|
||||||
|
max-width: 640px;
|
||||||
|
margin: 0 auto;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
line-height: 1.9;
|
||||||
|
color: var(--fg-secondary);
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
<title>DNFS 欢迎来到树状笔记</title>
|
<title>DNFS 欢迎来到树状笔记</title>
|
||||||
</head>
|
</head>
|
||||||
@@ -33,16 +40,23 @@
|
|||||||
<div class="right-group">
|
<div class="right-group">
|
||||||
<img src="favicon.ico" alt="头像加载失败" id="avatar" width="70px" height="70px" style="border-radius: 100px;">
|
<img src="favicon.ico" alt="头像加载失败" id="avatar" width="70px" height="70px" style="border-radius: 100px;">
|
||||||
<b id="name">DNFS</b>
|
<b id="name">DNFS</b>
|
||||||
<button onclick="window.location.href='login.html'">登录</button>
|
<button id="enterBtn" onclick="window.location.href='dnfs.html'" style="display:none">进入笔记</button>
|
||||||
<button onclick="window.location.href='register.html'">注册</button>
|
<button id="logoutBtn" style="display:none">退出</button>
|
||||||
|
<button id="loginBtn" onclick="window.location.href='login.html'">登录</button>
|
||||||
|
<button id="registerBtn" onclick="window.location.href='register.html'">注册</button>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
<div id="main-page">
|
<div id="main-page">
|
||||||
<h2>推荐节点</h2>
|
<h2>关于本站</h2>
|
||||||
<div id="recommended-node"></div>
|
<p class="site-intro">
|
||||||
|
DNFS 树状笔记是一个以树状结构组织内容的笔记社区:每个节点都可以生长出子节点,
|
||||||
|
像树枝一样层层延展,让知识自然成体系。支持 Markdown 文本、链接卡片、图片与文件分享,
|
||||||
|
注册登录后即可创建属于你自己的知识树。
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<footer class="footer">
|
<footer class="footer">
|
||||||
<a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener">蜀ICP备2024107064号-2</a>
|
<a href="https://beian.miit.gov.cn/" target="_blank" rel="noopener">蜀ICP备2024107064号-2</a>
|
||||||
</footer>
|
</footer>
|
||||||
|
<script src="js/index.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/* index.js — 社区首页逻辑
|
||||||
|
*
|
||||||
|
* 登录态检测:POST /whoami(HttpOnly q4_token Cookie 自动携带,JS 不经手 Token)
|
||||||
|
* 已登录:显示用户名与"进入笔记/退出"按钮;未登录:显示"登录/注册"
|
||||||
|
*/
|
||||||
|
(function () {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
const nameEl = document.getElementById('name');
|
||||||
|
const enterBtn = document.getElementById('enterBtn');
|
||||||
|
const logoutBtn = document.getElementById('logoutBtn');
|
||||||
|
const loginBtn = document.getElementById('loginBtn');
|
||||||
|
const registerBtn = document.getElementById('registerBtn');
|
||||||
|
|
||||||
|
async function checkLogin() {
|
||||||
|
try {
|
||||||
|
const res = await fetch('/whoami', { method: 'POST' });
|
||||||
|
if (!res.ok) return;
|
||||||
|
const data = await res.json();
|
||||||
|
if (data && data.login && data.user) {
|
||||||
|
nameEl.textContent = String(data.user);
|
||||||
|
enterBtn.style.display = '';
|
||||||
|
logoutBtn.style.display = '';
|
||||||
|
loginBtn.style.display = 'none';
|
||||||
|
registerBtn.style.display = 'none';
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('index: 登录态检测失败', e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logoutBtn.addEventListener('click', async function () {
|
||||||
|
try {
|
||||||
|
await fetch('/logout', { method: 'POST' });
|
||||||
|
} catch (e) {
|
||||||
|
console.warn('index: 退出登录请求异常', e.message);
|
||||||
|
}
|
||||||
|
location.reload();
|
||||||
|
});
|
||||||
|
|
||||||
|
checkLogin();
|
||||||
|
})();
|
||||||
Reference in new issue
Block a user