107 lines
3.8 KiB
PHP
107 lines
3.8 KiB
PHP
<?php
|
|
/**
|
|
* 班级操行分管理系统 - 登录入口
|
|
*
|
|
* 开发者: Canglan
|
|
* 联系方式: admin@sea-studio.top
|
|
* 版权归属: Sea Network Technology Studio
|
|
* 许可证: MIT License
|
|
*
|
|
* 版权所有 © Sea Network Technology Studio
|
|
*/
|
|
|
|
require_once __DIR__ . '/config.php';
|
|
|
|
if (isset($_SESSION['user_id']) && isset($_SESSION['user_type'])) {
|
|
$redirect = [
|
|
'student' => '/student/dashboard.php',
|
|
'parent' => '/parent/dashboard.php',
|
|
'admin' => '/admin/dashboard.php'
|
|
];
|
|
header("Location: " . ($redirect[$_SESSION['user_type']] ?? '/index.php'));
|
|
exit();
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
<title><?php echo SITE_NAME; ?> - 登录</title>
|
|
<link rel="stylesheet" href="/assets/css/style.css">
|
|
</head>
|
|
<body>
|
|
<div class="login-container">
|
|
<div class="login-header">
|
|
<h1><?php echo SITE_NAME; ?></h1>
|
|
<p>学生 / 家长 / 管理端 统一登录</p>
|
|
</div>
|
|
|
|
<form id="loginForm" class="login-form">
|
|
<div class="form-group">
|
|
<label>用户名</label>
|
|
<input type="text" id="username" name="username" required autocomplete="off" placeholder="学号/手机号/管理员账号">
|
|
</div>
|
|
<div class="form-group">
|
|
<label>密码</label>
|
|
<input type="password" id="password" name="password" required placeholder="请输入密码">
|
|
</div>
|
|
<button type="submit" class="btn-login">登 录</button>
|
|
<div id="errorMsg" class="error-msg" style="display: none;"></div>
|
|
</form>
|
|
|
|
<div class="login-footer">
|
|
<p>© Sea Network Technology Studio</p>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const API_BASE_URL = '<?php echo API_BASE_URL; ?>';
|
|
const JWT_STORAGE_KEY = '<?php echo JWT_STORAGE_KEY; ?>';
|
|
const USER_STORAGE_KEY = '<?php echo USER_STORAGE_KEY; ?>';
|
|
|
|
document.getElementById('loginForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const username = document.getElementById('username').value.trim();
|
|
const password = document.getElementById('password').value;
|
|
const errorMsg = document.getElementById('errorMsg');
|
|
|
|
if (!username || !password) {
|
|
showError('请填写用户名和密码');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, password })
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success && data.data) {
|
|
localStorage.setItem(JWT_STORAGE_KEY, data.data.token);
|
|
localStorage.setItem(USER_STORAGE_KEY, JSON.stringify(data.data));
|
|
window.location.href = data.data.redirect;
|
|
} else {
|
|
showError(data.message || '登录失败');
|
|
}
|
|
} catch (error) {
|
|
console.error('登录错误:', error);
|
|
showError('网络错误,请检查后端服务是否启动');
|
|
}
|
|
});
|
|
|
|
function showError(msg) {
|
|
const errorMsg = document.getElementById('errorMsg');
|
|
errorMsg.textContent = msg;
|
|
errorMsg.style.display = 'block';
|
|
setTimeout(() => {
|
|
errorMsg.style.display = 'none';
|
|
}, 3000);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|