feat: 多租户账号体系 + 前台收藏按钮
- 新增 users 表(user_id 数据隔离,bcrypt 密码) - 认证: 注册/登录(用户名+密码)/会话绑定用户, 首个用户成为管理员并接管旧数据 - 数据隔离: 笔记/分类/标签/回收站/版本/草稿/图谱/FTS 全部按用户隔离 - 前台: 登录/注册弹窗, 登录后★收藏自己的笔记, 游客只读公开笔记 - 后台: 用户名+密码登录, 每人管理自己的工作区, 越权访问返回404 - 冒烟测试重构+新增多租户隔离用例(78/78)
This commit is contained in:
+103
-19
@@ -81,58 +81,142 @@
|
||||
color: #667eea;
|
||||
text-decoration: none;
|
||||
}
|
||||
.switch-link {
|
||||
display: block;
|
||||
text-align: center;
|
||||
margin-top: 12px;
|
||||
color: #764ba2;
|
||||
text-decoration: none;
|
||||
font-size: 14px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.switch-link:hover { text-decoration: underline; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="login-box">
|
||||
<h1>后台管理登录</h1>
|
||||
<h1 id="boxTitle">后台管理登录</h1>
|
||||
<!-- 登录表单 -->
|
||||
<form id="loginForm">
|
||||
<div class="form-group">
|
||||
<label>管理密码</label>
|
||||
<input type="password" id="password" placeholder="请输入管理密码" required>
|
||||
<label>用户名</label>
|
||||
<input type="text" id="username" placeholder="请输入用户名" autocomplete="username" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>密码</label>
|
||||
<input type="password" id="password" placeholder="请输入密码" autocomplete="current-password" required>
|
||||
</div>
|
||||
<button type="submit" class="btn-login" id="loginBtn">登 录</button>
|
||||
<p class="error-msg" id="errorMsg"></p>
|
||||
</form>
|
||||
<!-- 注册表单 -->
|
||||
<form id="registerForm" style="display:none;">
|
||||
<div class="form-group">
|
||||
<label>用户名</label>
|
||||
<input type="text" id="regUsername" placeholder="设置用户名" autocomplete="username" required>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>昵称(可选)</label>
|
||||
<input type="text" id="regDisplay" placeholder="显示昵称" autocomplete="nickname">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label>密码(至少 6 位)</label>
|
||||
<input type="password" id="regPassword" placeholder="设置密码" autocomplete="new-password" required>
|
||||
</div>
|
||||
<button type="submit" class="btn-login" id="regBtn">注 册</button>
|
||||
<p class="error-msg" id="regErrorMsg"></p>
|
||||
</form>
|
||||
<a href="javascript:void(0)" class="switch-link" id="switchLink">没有账号?去注册</a>
|
||||
<a href="/" class="back-link">返回前台</a>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const form = document.getElementById('loginForm');
|
||||
const loginForm = document.getElementById('loginForm');
|
||||
const registerForm = document.getElementById('registerForm');
|
||||
const username = document.getElementById('username');
|
||||
const password = document.getElementById('password');
|
||||
const regUsername = document.getElementById('regUsername');
|
||||
const regDisplay = document.getElementById('regDisplay');
|
||||
const regPassword = document.getElementById('regPassword');
|
||||
const loginBtn = document.getElementById('loginBtn');
|
||||
const regBtn = document.getElementById('regBtn');
|
||||
const errorMsg = document.getElementById('errorMsg');
|
||||
const regErrorMsg = document.getElementById('regErrorMsg');
|
||||
const switchLink = document.getElementById('switchLink');
|
||||
const boxTitle = document.getElementById('boxTitle');
|
||||
let isRegister = false;
|
||||
|
||||
form.addEventListener('submit', async (e) => {
|
||||
function submit(btn, text) {
|
||||
btn.disabled = true;
|
||||
btn.textContent = text;
|
||||
}
|
||||
function reset(btn, text) {
|
||||
btn.disabled = false;
|
||||
btn.textContent = text;
|
||||
}
|
||||
|
||||
function setMode(reg) {
|
||||
isRegister = reg;
|
||||
loginForm.style.display = reg ? 'none' : 'block';
|
||||
registerForm.style.display = reg ? 'block' : 'none';
|
||||
boxTitle.textContent = reg ? '注册账号' : '后台管理登录';
|
||||
switchLink.textContent = reg ? '已有账号?去登录' : '没有账号?去注册';
|
||||
errorMsg.style.display = 'none';
|
||||
regErrorMsg.style.display = 'none';
|
||||
}
|
||||
switchLink.addEventListener('click', () => setMode(!isRegister));
|
||||
|
||||
loginForm.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
errorMsg.style.display = 'none';
|
||||
loginBtn.disabled = true;
|
||||
loginBtn.textContent = '登录中...';
|
||||
|
||||
submit(loginBtn, '登录中...');
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('username', username.value);
|
||||
formData.append('password', password.value);
|
||||
|
||||
const res = await fetch('/admin/login', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
});
|
||||
|
||||
const res = await fetch('/admin/login', { method: 'POST', body: formData });
|
||||
const data = await res.json();
|
||||
|
||||
if (data.code === 0) {
|
||||
window.location.href = '/admin/';
|
||||
} else {
|
||||
errorMsg.textContent = data.message;
|
||||
errorMsg.style.display = 'block';
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = '登 录';
|
||||
reset(loginBtn, '登 录');
|
||||
}
|
||||
} catch (err) {
|
||||
errorMsg.textContent = '登录失败,请重试';
|
||||
errorMsg.style.display = 'block';
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = '登 录';
|
||||
reset(loginBtn, '登 录');
|
||||
}
|
||||
});
|
||||
|
||||
registerForm.addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
regErrorMsg.style.display = 'none';
|
||||
submit(regBtn, '注册中...');
|
||||
try {
|
||||
const body = {
|
||||
username: regUsername.value,
|
||||
password: regPassword.value,
|
||||
display_name: regDisplay.value
|
||||
};
|
||||
const res = await fetch('/api/auth/register', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.code === 0) {
|
||||
window.location.href = '/admin/';
|
||||
} else {
|
||||
regErrorMsg.textContent = data.message;
|
||||
regErrorMsg.style.display = 'block';
|
||||
reset(regBtn, '注 册');
|
||||
}
|
||||
} catch (err) {
|
||||
regErrorMsg.textContent = '注册失败,请重试';
|
||||
regErrorMsg.style.display = 'block';
|
||||
reset(regBtn, '注 册');
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user