根因:login.php 仅持久化 username、不持久化 password;token 缓存失效后
自动重登因缺少密码而抛 RuntimeException('未配置卷王登录账号'),前端仅提示
"后台刷新失败,已显示本地缓存"却无法自愈,刷新循环永不恢复。
修复:
- login.php:登录成功时同时持久化 username 与 password,token 失效时平台
可凭凭据自动重新登录卷王,避免死状态。
- api.php:全局 catch 对鉴权类错误(未配置账号/登录失败)打标 extra.auth=true。
- assets/app.js、dashboard.php:api() 透传 auth 标记;刷新失败若为鉴权错误,
自动跳转 login.php 重新登录,不再停留在本地缓存死状态。
验证:本地 mock 卷王端到端复现 CASE1(空账号精确复现报错)与 CASE2
(凭据齐备自动重登成功),HTTP 接口确认返回 extra.auth:true 与 ok:true。
93 lines
3.3 KiB
PHP
93 lines
3.3 KiB
PHP
<?php
|
|
/**
|
|
* 独立登录页:连接信息(系统地址/接口前缀/账号)已在 data/config.json 中预置,
|
|
* 本页只需输入密码(账号可改)完成卷王登录,成功后进入主系统。
|
|
* 连接信息有变请在【设置】中修改。
|
|
*/
|
|
declare(strict_types=1);
|
|
require __DIR__ . '/bootstrap.php';
|
|
|
|
if (ny_is_auth()) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
$cfg = ny_load_config();
|
|
$err = '';
|
|
$ok = false;
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$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();
|
|
$ok = true;
|
|
} catch (Throwable $e) {
|
|
$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>
|