Files
ny-exam-platform/login.php
T
nxxy335 0fd880624f feat: 完善/优化/新增大批次 + 安全加固 + 性能优化
- 修复再次提交不置顶(排序键改用 last_submit_ts)
- 后端结果缓存 + 并发锁(DataCache / ny_build_data / Roster 指纹失效)
- 导出 xlsx 多表:按最新成绩 / 按班级分表 / 可选列
- 名单管理:新增改弹窗、删除多选+二次确认、登陆账号列
- 安全:CSRF token + 登录失败限流(5次锁10分钟)
- 未匹配答卷一键加入名单(unmatched_map)
- 缺考名单增强:复制学号/姓名、按班统计、打印
- 成绩快照 + 跨卷趋势(Snapshot)
- 异常检测线索(短时长高分/高频重考/雷同成绩)
- 前端性能:虚拟滚动、搜索防抖、后台暂停、失败退避
- 测试沉淀:test-new-features.php + run-all.sh
2026-09-10 17:59:45 +08:00

113 lines
4.3 KiB
PHP

<?php
/**
* 独立登录页:连接信息(系统地址/接口前缀/账号)已在 data/config.json 中预置,
* 本页只需输入密码(账号可改)完成卷王登录,成功后进入主系统。
* 连接信息有变请在【设置】中修改。
*/
declare(strict_types=1);
require __DIR__ . '/bootstrap.php';
if (ny_is_auth()) {
// 已登录但卷王 token 可能已过期:用当前 token 探一次卷王 /system,能 200 即视为有效,否则清掉本地会话强制重新登录
try {
$client = new SurveyKingClient($cfg);
$client->ping();
} catch (Throwable $e) {
// 卷王 token 已失效或配置错误 → 清会话 + 强制显示登录页(避免 login.php 把人踢回 index.php 形成死循环)
$_SESSION = [];
@session_destroy();
}
if (ny_is_auth()) {
header('Location: index.php');
exit;
}
}
$cfg = ny_load_config();
$err = '';
$ok = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// 登录失败限流:5 次失败锁 10 分钟,防止暴力破解
$throttle = ny_login_throttle_check();
if ($throttle['until'] > time()) {
$wait = (int)(($throttle['until'] - time()) / 60) + 1;
$err = '尝试次数过多,请 ' . $wait . ' 分钟后再试。';
} else {
$user = trim((string)($_POST['username'] ?? $cfg['username']));
$pwd = (string)($_POST['password'] ?? '');
// 允许用户在本页调整账号;密码必须输入
if ($user === '' || $pwd === '') {
$err = '请输入账号与密码。';
} else {
// 用当前配置的连接信息 + 本页输入的账密做一次卷王登录
$test = $cfg;
$test['username'] = $user;
$test['password'] = $pwd;
try {
$client = new SurveyKingClient($test);
$client->getToken(true);
// 登录成功:把账号与密码写回配置(本地单用户工具,持久化密码以便 token
// 失效时平台能自动重新登录卷王,避免后台刷新陷入"未配置账号"的死状态)
$cfg['username'] = $user;
$cfg['password'] = $pwd;
ny_save_config($cfg);
ny_login();
ny_login_throttle_reset();
$ok = true;
} catch (Throwable $e) {
ny_login_throttle_hit();
$err = '登录失败:' . $e->getMessage();
}
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>登录 · <?= htmlspecialchars(NY_APP_NAME) ?></title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<div class="login-gate<?= $ok ? ' hidden' : '' ?>">
<div class="login-box">
<div class="login-brand">
<span class="logo">NY</span>
<div class="brand-text">
<strong><?= htmlspecialchars(NY_APP_NAME) ?></strong>
<small>实时数据统计</small>
</div>
</div>
<p class="login-tip">系统连接信息已配置(<?= htmlspecialchars(parse_url($cfg['base_url'], PHP_URL_HOST)) ?>/<?= htmlspecialchars(trim($cfg['api_prefix'], '/')) ?>)。请输入账号密码完成登录。</p>
<?php if ($err !== ''): ?>
<div class="alert" style="white-space:pre-wrap"><?= htmlspecialchars($err) ?></div>
<?php endif; ?>
<form method="post">
<div class="fi"><label>账号</label><input type="text" name="username" value="<?= htmlspecialchars($cfg['username']) ?>" autocomplete="username"></div>
<div class="fi"><label>密码</label><input type="password" name="password" placeholder="请输入密码" autocomplete="current-password" autofocus></div>
<div class="login-actions">
<button class="btn btn-primary" type="submit">登录</button>
</div>
</form>
</div>
</div>
<?php if ($ok): ?>
<div class="login-gate">
<div class="login-box" style="text-align:center">
<div class="login-brand" style="justify-content:center"><span class="logo">NY</span>
<div class="brand-text"><strong>登录成功</strong><small>正在进入数据平台…</small></div>
</div>
<script>setTimeout(function(){ location.href='index.php'; }, 600);</script>
</div>
</div>
<?php endif; ?>
</body>
</html>