diff --git a/api/gh_cfg.php b/api/gh_cfg.php index a82d48b..c8f1fe3 100644 --- a/api/gh_cfg.php +++ b/api/gh_cfg.php @@ -1,10 +1,11 @@ 0) { @ob_end_clean(); } ob_start(); @@ -14,9 +15,38 @@ header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0'); require_once __DIR__ . '/../core.php'; require_once __DIR__ . '/../lsky.php'; +require_once __DIR__ . '/../gh_api.php'; // 15m44v: ghApiRequest(验活 token) $cfg = loadImgJson(); +$ghAuthorized = false; + +// ★ 15m44v: 带 token 时查该玩家是否已有【有效】GitHub token(验活,非只看存没存)。 +// 前端 loadGhCfg 改成 fetch('gh_cfg.php?token=' + TOKEN) 后,据此显示「已授权」态, +// 根治"重新进表单 URL 无 gh_ok 就误判为未授权"的老问题。 +$probeTk = isset($_GET['token']) ? trim((string)$_GET['token']) : ''; +if ($cfg['gh_enabled'] && $probeTk !== '') { + $pi = validateTokenSilent($probeTk); + $probePlayer = ($pi && !empty($pi['player'])) ? $pi['player'] : ''; + if ($probePlayer !== '') { + try { + $db = getDB(); + $db->exec("CREATE TABLE IF NOT EXISTS gh_auth_tokens ( + player_name TEXT PRIMARY KEY, gh_login TEXT DEFAULT '', + access_token TEXT NOT NULL, updated_at INTEGER NOT NULL)"); + $st = $db->prepare("SELECT access_token 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']); + } + } catch (\Throwable $e) { $ghAuthorized = false; } + } +} + success([ - 'gh_enabled' => !empty($cfg['gh_enabled']), - 'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '', + 'gh_enabled' => !empty($cfg['gh_enabled']), + 'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '', + 'gh_authorized' => $ghAuthorized, ], 'ok'); diff --git a/gh_api.php b/gh_api.php index 0b46677..2c232bf 100644 --- a/gh_api.php +++ b/gh_api.php @@ -135,17 +135,19 @@ function ghSyncCreate($ticketId, $player, $title, $description, $images = [], $t // 仍带着 [图片N]。旧版 ghSyncCreate 直接把原始描述塞进 issue body,导致 GitHub 上 // 显示「abc[图片1]」占位符没被替换(用户实测附件不对根因)。附件名统一在末尾集中 // 展示一次,描述里的 [图片N] 只标记"此处有附件"不重复展示文件名。 - // ★ 15m44v: 替换串从 '\U📎 附件\1\E' 改为 '\u📎 附件\1'。 - // 根因:PCRE 的 \U…\E 是【大小写转换开关】(\U=后续全大写,\E=恢复正常), - // 夹在 emoji 之后会把中文「附件」误转成小写/乱码;且大小写开关对 CJK 无意义。 - // \u=单个字符转大写(对 CJK/emoji 恒等无副作用),\U/\E 全删,替换串干净。 + // ★ 15m44v 修:替换串几经踩坑—— + // 初版 '\U📎 附件\1\E':\U..\E 是 PCRE【大小写转换开关】(\U=全大写\E=恢复),夹在 emoji 后误转 CJK。 + // 二版 '\u📎 附件\1':PHP 单引号里 \u 是【字面 反斜杠+u】(单引号只认 \' 和 \\), + // 传给 PCRE 替换串的 \u = "下个字符转大写"指令,对 emoji 无效→吐出字面 u(issue body 出现 "u 📎 附件1")。 + // 终版:替换串不含任何大小写/转义指令,emoji 直接写字面值,只留 \1 反向引用(数字组号)。 + // PCRE 替换串里 \1 = 引用捕获组 1(占位符里的数字 N),emoji「📎」直接是普通字符,无副作用。 $desc = (string)$description; $hasAttach = false; foreach ((is_array($images) ? $images : []) as $u) { if (is_string($u)) { $hasAttach = true; break; } } if ($hasAttach) { - $desc = preg_replace('/\[图片(\d+)\]/u', '\u📎 附件\1', $desc); + $desc = preg_replace('/\[图片(\d+)\]/u', "\U0001F4CE 附件\1", $desc); } // ★ 15m44v: 建单诊断日志(写 /plugin/gh_sync.log),记录替换后描述 + 附件,定位占位符未替换。 ghLog('create ticket=' . $ticketId . ' has_attach=' . ($hasAttach ? 'y' : 'n') diff --git a/player.php b/player.php index c473ba2..7e772ff 100644 --- a/player.php +++ b/player.php @@ -3974,7 +3974,9 @@ if ($currentVersion !== $BUILD_VERSION) { const box = document.getElementById('ghTicketAuth'); if (!box) return; try { - const res = await fetch(API + 'gh_cfg.php'); + // ★ 15m44v: fetch 带 token,后端据此验活该玩家 GitHub token,返回 gh_authorized。 + // 根治"重新进表单 URL 无 gh_ok 就误判未授权"——现在只要 token 真有效就显示已授权。 + const res = await fetch(API + 'gh_cfg.php?token=' + encodeURIComponent(TOKEN)); const data = await res.json(); const d = (data.success && data.data) ? data.data : null; if (d && d.gh_enabled) { @@ -3985,13 +3987,17 @@ if ($currentVersion !== $BUILD_VERSION) { // ★ 15m44n 互斥二选一:GitHub 模式附件全走 issue,不走图床 → 切附件区文案 const hint = document.getElementById('ticketImgHint'); if (hint) hint.textContent = '📎 GitHub 模式:附件随工单同步到 GitHub issue(不走图床)。选中的文件名会记入 issue,原图请在 GitHub issue 页直接上传。'; - if (txt) txt.textContent = '⛓️ GitHub 工单同步(' + (d.gh_repo || 'issue') + ')'; - if (sub) sub.textContent = '工单与附件都同步到 GitHub issue,点右侧按钮授权你的 GitHub 账号'; - if (btn) btn.onclick = gotoGhOAuth; - // URL 带 gh_ok=1(刚从 GitHub 回跳)→ 提示已授权 - if (new URLSearchParams(location.search).get('gh_ok') === '1') { + // ★ 15m44v: 按 gh_authorized 显示授权态(不再只靠 URL gh_ok 参数) + const isAuthed = d.gh_authorized === true + || new URLSearchParams(location.search).get('gh_ok') === '1'; + if (isAuthed) { if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue'; - if (btn) { btn.textContent = '重新授权'; } + if (sub) sub.textContent = '当前账号 token 有效,工单提交将自动同步到 GitHub issue'; + if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } + } else { + if (txt) txt.textContent = '⛓️ GitHub 工单同步(' + (d.gh_repo || 'issue') + ')'; + if (sub) sub.textContent = '工单与附件都同步到 GitHub issue,点右侧按钮授权你的 GitHub 账号'; + if (btn) { btn.textContent = '授权 GitHub'; btn.onclick = gotoGhOAuth; } } } } catch (e) { /* 端点不存在/未部署 → 不显示 */ }