Files
demo-ai-develops/static/js/login.js
潘舒啸 3cbb00a95b
All checks were successful
ai-reviews / review (pull_request) Successful in 2m17s
Add static/js/login.js from workspace
2001-01-01 00:00:00 +00:00

52 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('loginForm');
const username = document.getElementById('username');
const password = document.getElementById('password');
const remember = document.getElementById('remember');
const error = document.getElementById('error');
function showError(msg){
error.textContent = msg;
}
function validate() {
if (!username.value || username.value.trim().length < 3) {
showError('用户名至少 3 个字符');
return false;
}
if (!password.value || password.value.length < 6) {
showError('密码至少 6 个字符');
return false;
}
return true;
}
form.addEventListener('submit', (e) => {
e.preventDefault();
showError('');
if (!validate()) return;
// 本地模拟认证:示例用户名/密码对
const validUsers = { 'alice':'password123', 'bob':'secret123', 'beppeb':'demoPass' };
const inputUser = username.value.trim();
const inputPass = password.value;
if (validUsers[inputUser] && validUsers[inputUser] === inputPass) {
// 登录成功:写入本地 token示例并跳转首页
const token = 'demo-token-' + Math.random().toString(36).slice(2);
if (remember.checked) {
localStorage.setItem('demo_auth_token', token);
} else {
sessionStorage.setItem('demo_auth_token', token);
// keep a copy in localStorage to let index.html detect login; it's demo-only
localStorage.setItem('demo_auth_token', token);
}
// 简短提示后跳转
showError('登录成功,正在跳转……');
setTimeout(() => { window.location.href = '/'; }, 700);
} else {
showError('用户名或密码不正确(此示例使用本地模拟账号)。');
}
});
});