Initial commit: Car Maintenance Management System
Author: Yang Lu School: Liaoning Institute of Science and Technology Major: Computer Science and Technology Class: BZ246 Tech Stack: - Backend: Spring Boot 2.7.18 + JPA + MySQL - Frontend: HTML5 + CSS3 + JavaScript Features: - User Management (Admin/Staff/Customer roles) - Vehicle Archive Management - Service Order Management - Parts Inventory Management - Online Appointment Service - Data Statistics and Analysis Generated with Claude Code Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
102
frontend/js/login.js
Normal file
102
frontend/js/login.js
Normal file
@@ -0,0 +1,102 @@
|
||||
// 登录页面JavaScript
|
||||
|
||||
// 页面加载时检查是否已登录
|
||||
window.addEventListener('DOMContentLoaded', () => {
|
||||
const token = localStorage.getItem(STORAGE_KEYS.TOKEN);
|
||||
if (token) {
|
||||
const user = utils.getCurrentUser();
|
||||
if (user) {
|
||||
redirectToDashboard(user.role);
|
||||
}
|
||||
}
|
||||
|
||||
// 回车键登录
|
||||
document.getElementById('password').addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
handleLogin();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 处理登录
|
||||
async function handleLogin() {
|
||||
const username = document.getElementById('username').value.trim();
|
||||
const password = document.getElementById('password').value.trim();
|
||||
const role = document.getElementById('role').value;
|
||||
|
||||
// 验证输入
|
||||
if (!username) {
|
||||
utils.showError('请输入用户名');
|
||||
return;
|
||||
}
|
||||
|
||||
if (!password) {
|
||||
utils.showError('请输入密码');
|
||||
return;
|
||||
}
|
||||
|
||||
// 禁用登录按钮
|
||||
const loginBtn = document.querySelector('.btn-login');
|
||||
const originalText = loginBtn.textContent;
|
||||
loginBtn.disabled = true;
|
||||
loginBtn.textContent = '登录中...';
|
||||
|
||||
try {
|
||||
// 调用登录API
|
||||
const response = await api.post(API_ENDPOINTS.LOGIN, {
|
||||
username: username,
|
||||
password: password
|
||||
});
|
||||
|
||||
if (response.code === 200) {
|
||||
const { token, userInfo } = response.data;
|
||||
|
||||
// 验证角色
|
||||
if (userInfo.role !== role) {
|
||||
utils.showError('登录角色不匹配,请选择正确的角色');
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = originalText;
|
||||
return;
|
||||
}
|
||||
|
||||
// 保存登录信息
|
||||
localStorage.setItem(STORAGE_KEYS.TOKEN, token);
|
||||
localStorage.setItem(STORAGE_KEYS.USER_INFO, JSON.stringify(userInfo));
|
||||
|
||||
utils.showSuccess('登录成功!');
|
||||
|
||||
// 延迟跳转以显示成功消息
|
||||
setTimeout(() => {
|
||||
redirectToDashboard(userInfo.role);
|
||||
}, 500);
|
||||
|
||||
} else {
|
||||
utils.showError(response.message || '登录失败');
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = originalText;
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('登录错误:', error);
|
||||
utils.showError('登录失败,请检查网络连接');
|
||||
loginBtn.disabled = false;
|
||||
loginBtn.textContent = originalText;
|
||||
}
|
||||
}
|
||||
|
||||
// 根据角色跳转到对应的仪表板
|
||||
function redirectToDashboard(role) {
|
||||
switch (role) {
|
||||
case 'admin':
|
||||
window.location.href = 'admin/dashboard.html';
|
||||
break;
|
||||
case 'staff':
|
||||
window.location.href = 'staff/dashboard.html';
|
||||
break;
|
||||
case 'customer':
|
||||
window.location.href = 'customer/dashboard.html';
|
||||
break;
|
||||
default:
|
||||
utils.showError('未知的用户角色');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user