From 8c9b8a10d93e206a2456ec097bfcf608d81ba27e Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Thu, 26 Nov 2026 20:04:19 +0800 Subject: [PATCH] =?UTF-8?q?15m44z=20=E7=BB=AD=EF=BC=8819:59=EF=BC=89?= =?UTF-8?q?=EF=BC=9A=E4=BF=AE=20GitHub=20=E6=8E=88=E6=9D=83=E8=AF=AF?= =?UTF-8?q?=E5=AF=BC=20=E2=80=94=E2=80=94=20=E5=88=9A=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E5=8D=B4=E6=8F=90=E7=A4=BA=E3=80=8Ctoken=20=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E5=B7=B2=E5=A4=B1=E6=95=88=E3=80=8D=20gh=5Fcfg.php=EF=BC=9A?= =?UTF-8?q?=E5=8A=A0=20gh=5Ftoken=5Ffresh=20=E5=88=A4=E5=AE=9A=EF=BC=88OAu?= =?UTF-8?q?th=20=E5=9B=9E=E8=B0=83=20updated=5Fat=20<5min=20=E2=86=92=20?= =?UTF-8?q?=E5=85=8D=20/user=20=E9=AA=8C=E6=B4=BB=EF=BC=8C=E5=BF=85?= =?UTF-8?q?=E6=9C=89=E6=95=88=EF=BC=89=EF=BC=9B=20=E6=A0=B9=E6=B2=BB=20CF?= =?UTF-8?q?=20=E8=BE=B9=E7=BC=98=20/user=20=E9=AA=8C=E6=B4=BB=E5=81=B6?= =?UTF-8?q?=E5=8F=91=20401/=E5=8D=A1=E4=BD=8F=E6=8A=8A=E5=88=9A=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E7=9A=84=E6=9C=89=E6=95=88=20token=20=E8=AF=AF?= =?UTF-8?q?=E5=88=A4=E6=88=90=E5=A4=B1=E6=95=88=E3=80=82=20token=20?= =?UTF-8?q?=E4=BF=9D=E7=95=99=E5=85=9C=E5=BA=95=E5=8F=AA=E5=AF=B9=E3=80=8C?= =?UTF-8?q?=E8=80=81=20token=20=E9=AA=8C=E6=B4=BB=20401=E3=80=8D=E7=94=9F?= =?UTF-8?q?=E6=95=88=EF=BC=8C=E5=88=9A=E6=8E=88=E6=9D=83=EF=BC=88fresh?= =?UTF-8?q?=EF=BC=89=E4=B8=8D=E8=BF=9B=E5=85=A5=E8=AF=A5=E5=88=86=E6=94=AF?= =?UTF-8?q?=E3=80=82=20player.php=EF=BC=9A=E6=8C=89=20gh=5Ftoken=5Ffresh?= =?UTF-8?q?=20=E5=8C=BA=E5=88=86=E6=96=87=E6=A1=88=20=E2=80=94=E2=80=94=20?= =?UTF-8?q?=E5=88=9A=E6=8E=88=E6=9D=83=E6=98=BE=E7=A4=BA=E3=80=8C=E5=B7=B2?= =?UTF-8?q?=E6=8E=88=E6=9D=83=20token=20=E6=9C=89=E6=95=88=E3=80=8D?= =?UTF-8?q?=EF=BC=8C=20=E8=80=81=20token=20=E5=A4=B1=E6=95=88=E6=89=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E3=80=8C=E6=8E=88=E6=9D=83=E5=B7=B2=E4=BF=9D?= =?UTF-8?q?=E7=95=99=EF=BC=8Ctoken=20=E5=8F=AF=E8=83=BD=E5=B7=B2=E5=A4=B1?= =?UTF-8?q?=E6=95=88=E3=80=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/gh_cfg.php | 39 +++++++++++++++++++++++++++++---------- player.php | 13 ++++++++++++- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/api/gh_cfg.php b/api/gh_cfg.php index a74cb8e..4483fee 100644 --- a/api/gh_cfg.php +++ b/api/gh_cfg.php @@ -23,6 +23,11 @@ require_once __DIR__ . '/../gh_api.php'; // 15m44v: ghApiRequest(验活 toke $cfg = loadImgJson(); $ghAuthorized = false; +// ★ 15m44z 续(19:59 修):gh_token_fresh 区分「刚授权(免验活必有效)」vs「老 token(验活判)」。 +// 刚走完 gh_oauth 回调的玩家 updated_at 很近(<5min),token 必有效 —— 不再发 /user 验活 +// (CF 边缘偶发卡住/401 会把有效 token 误判成失效 → 前端显示「token 可能已失效」误导)。 +$ghTokenFresh = false; +$ghTokenUpdatedAt = 0; // ★ 15m44v: 带 token 时查该玩家是否已有【有效】GitHub token(验活,非只看存没存)。 // 前端 loadGhCfg 改成 fetch('gh_cfg.php?token=' + TOKEN) 后,据此显示「已授权」态, @@ -60,16 +65,27 @@ if ($cfg['gh_enabled'] && $probeTk !== '') { $db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens ( player_name VARCHAR(500) PRIMARY KEY, gh_login VARCHAR(500) DEFAULT '', access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)"); - $st = $db->prepare("SELECT access_token, gh_login FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); + $st = $db->prepare("SELECT access_token, gh_login, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); $st->bindValue(':p', $probePlayer, SQLITE3_TEXT); $trow = $st->execute()->fetchArray(SQLITE3_ASSOC); if ($trow && !empty($trow['access_token'])) { - // 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法) - $v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']); - $ghAuthorized = !empty($v['ok']); - // ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀) - ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) - . ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n')); + // ★ 15m44z 续(19:59 修):刚授权(updated_at 距今 < 5min)→ token 必有效,免验活。 + // 根治「刚走完 gh_oauth 回调回来,gh_cfg 却显示 token 可能已失效」的误导。 + // 老 token(>5min)才走 /user 验活判真失效。 + $ghTokenUpdatedAt = (int)($trow['updated_at'] ?? 0); + $ghTokenFresh = ($ghTokenUpdatedAt > 0 && (time() - $ghTokenUpdatedAt) < 300); + if ($ghTokenFresh) { + $ghAuthorized = true; // 刚授权 = 有效,不再发 /user(防 CF 卡住误判 401) + ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) + . ' fresh=y updated_at=' . $ghTokenUpdatedAt . ' (免验活, 刚授权必有效)'); + } else { + // 验活:/user 加唯一 query 破 CF 缓存(同 gh_oauth.php 15m44v 修法) + $v = ghApiRequest('GET', '/user?x=nocache_' . (int)microtime(true) . mt_rand(100000, 999999), null, $trow['access_token']); + $ghAuthorized = !empty($v['ok']); + // ★ 诊断日志:验活结果写 gh_sync.log,定位横条消失断点(status/ok/token 前缀) + ghLog('cfg verify player=' . $probePlayer . ' token=' . substr((string)$trow['access_token'], 0, 8) + . ' status=' . (int)($v['status'] ?? -1) . ' ok=' . ($v['ok'] ? 'y' : 'n')); + } } else { ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens'); } @@ -99,11 +115,13 @@ if ($ghAppbot && $ghAnonMode) { // 只要 token 行还在,gh_authorized 也应为 true(「token 已保留,可重新授权」), // 避免前端误判"未授权"让玩家以为切匿名后授权被清掉了。 // 判法:$ghAnonAvailable=true 且 $ghAuthorized 验活 401 失败 + gh_auth_tokens 有 token 行 -// → 改 gh_authorized=true,前端文案提示"实名通道失效,已保留授权记录,可重新授权"。 -if ($ghAnonAvailable && $probePlayer !== '' && !$ghAuthorized) { +// → 改 gh_authorized=true(但 gh_token_fresh 保持 false,前端显示「已保留」而非「已授权」)。 +// ★ 15m44z 续(19:59):兜底只对「老 token 验活 401」生效;刚授权(fresh)已在上面直接判有效, +// 不会进这个分支,所以刚授权不会误显示「token 可能已失效」。 +if ($ghAnonAvailable && $probePlayer !== '' && !$ghAuthorized && !$ghTokenFresh) { try { $dbTok = getDB(); - $stTok = $dbTok->prepare("SELECT access_token FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); + $stTok = $dbTok->prepare("SELECT access_token, updated_at FROM gh_auth_tokens WHERE player_name = :p AND access_token != '' ORDER BY updated_at DESC LIMIT 1"); $stTok->bindValue(':p', $probePlayer, SQLITE3_TEXT); $tokRow = $stTok->execute()->fetchArray(SQLITE3_ASSOC); if ($tokRow && !empty($tokRow['access_token'])) { @@ -121,6 +139,7 @@ success([ 'gh_enabled' => !empty($cfg['gh_enabled']), 'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '', 'gh_authorized' => $ghAuthorized, + 'gh_token_fresh' => $ghTokenFresh, // ★ 15m44z 续(19:59):刚授权免验活=必有效 'gh_appbot' => $ghAppbot, 'gh_anon_available' => $ghAnonAvailable, ], 'ok'); diff --git a/player.php b/player.php index fe18730..22d1dc2 100644 --- a/player.php +++ b/player.php @@ -4002,11 +4002,22 @@ if ($currentVersion !== $BUILD_VERSION) { // - 匿名模式可用 + gh_authorized=true → "授权已保留(可重新授权)" // - 匿名模式不可用 + gh_authorized=true → "已授权,自动同步"(老态) if (isAuthed) { - if (d.gh_anon_available === true) { + // ★ 15m44z 续(19:59):按 gh_token_fresh 区分「刚授权(必有效)」vs「老 token(可能失效)」。 + // 刚走完 gh_oauth 回调(updated_at <5min)→ gh_token_fresh=true → 显示「已授权」, + // 根治「刚授权却提示 token 可能已失效」的误导(CF 验活偶发 401 把有效 token 误判)。 + const tokenFresh = d.gh_token_fresh === true; + if (tokenFresh) { + // 刚授权 = 必有效,不显示「可能失效」 + if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue'; + if (sub) sub.textContent = '刚完成授权,token 有效,工单提交将自动同步到 GitHub issue'; + if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } + } else if (d.gh_anon_available === true) { + // 老 token 失效 + 匿名通道可用 → 已保留授权记录(可重新授权或走匿名) if (txt) txt.textContent = '🔗 GitHub 授权已保留,切匿名模式不影响授权记录'; if (sub) sub.textContent = '实名通道 token 可能已失效,点重新授权可刷新;切匿名模式走系统机器人,无需玩家授权'; if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } } else { + // 老 token 失效 + 无匿名通道 if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue'; if (sub) sub.textContent = '当前账号 token 有效,工单提交将自动同步到 GitHub issue'; if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; }