From 84d21320700a944f228f68e81ecaeb95415a9558 Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Thu, 26 Nov 2026 16:38:18 +0800 Subject: [PATCH] =?UTF-8?q?15m44z=20=E7=AC=AC=E4=B8=89=E9=83=A8=E5=88=86?= =?UTF-8?q?=E7=BB=AD=EF=BC=9A=E5=8C=BF=E5=90=8D=E5=B7=A5=E5=8D=95=E5=9B=9E?= =?UTF-8?q?=E5=A4=8D=E5=8E=BB=E7=8E=A9=E5=AE=B6=E5=90=8D=20+=20token=20?= =?UTF-8?q?=E4=BF=9D=E7=95=99=20+=20=E5=8C=BF=E5=90=8D=E5=BC=80=E5=85=B3?= =?UTF-8?q?=20UI=20=E5=8C=BF=E5=90=8D=E5=B7=A5=E5=8D=95(gh=5Fissue=5Fowner?= =?UTF-8?q?=3Dbot)=E5=9B=9E=E5=A4=8D/=E6=92=A4=E5=8D=95/=E5=AE=8C=E7=BB=93?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E5=9C=A8=20GitHub=20=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E5=86=99=E6=8F=90=E5=8D=95=E4=BA=BA=E5=90=8D=E5=AD=97=EF=BC=9B?= =?UTF-8?q?=20ghSyncReply=20=E6=8C=89=20gh=5Fissue=5Fowner=20=E5=88=A4?= =?UTF-8?q?=EF=BC=9Abot=20=E5=8D=95=E7=9C=81=E7=95=A5=20[sender]=20?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=EF=BC=8C=E5=AE=9E=E5=90=8D=E5=8D=95=E4=BF=9D?= =?UTF-8?q?=E7=95=99=EF=BC=9B=20ticketReply=20=E5=8A=A0=20guard=EF=BC=9Abo?= =?UTF-8?q?t=20=E5=8D=95+=E7=8E=A9=E5=AE=B6=20token=20=E4=B8=8D=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E6=97=B6=E9=9D=99=E9=BB=98=E8=B7=B3=E8=BF=87=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E6=8E=A8=E9=80=81=EF=BC=9B=20gh=5Fcfg.php=EF=BC=9A?= =?UTF-8?q?=E5=88=87=E5=8C=BF=E5=90=8D=E6=A8=A1=E5=BC=8F=E6=97=B6=E7=8E=A9?= =?UTF-8?q?=E5=AE=B6=20OAuth=20token=20=E4=BF=9D=E7=95=99(gh=5Fauthorized?= =?UTF-8?q?=20=E5=85=9C=E5=BA=95=20true)=EF=BC=9B=20player.php=EF=BC=9A?= =?UTF-8?q?=E5=8C=BF=E5=90=8D=E5=BC=80=E6=97=B6=E6=8E=88=E6=9D=83=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E6=94=B9=E4=BF=9D=E7=95=99=E6=8E=88=E6=9D=83=E4=B8=8D?= =?UTF-8?q?=E9=9A=90=E8=97=8F=EF=BC=8C=E5=8C=BF=E5=90=8D=E5=85=B3=E6=81=A2?= =?UTF-8?q?=E5=A4=8D=E9=87=8D=E6=96=B0=E6=8E=88=E6=9D=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/gh_cfg.php | 24 ++++++++++++++++++++++++ api/ticket.php | 19 +++++++++++++++++++ gh_api.php | 30 ++++++++++++++++++++++++++---- player.php | 38 ++++++++++++++++++++++++++++++-------- 4 files changed, 99 insertions(+), 12 deletions(-) diff --git a/api/gh_cfg.php b/api/gh_cfg.php index 26a84f4..a74cb8e 100644 --- a/api/gh_cfg.php +++ b/api/gh_cfg.php @@ -93,6 +93,30 @@ if ($ghAppbot && $ghAnonMode) { } catch (\Throwable $e) { $ghAnonAvailable = false; } } +// ★ 15m44z 第三部分+token保留:切匿名模式 ≠ 放弃实名授权。 +// 玩家切匿名后,bot 通道可用(ghs_ token 不依赖玩家 OAuth),但玩家之前授权的 +// GitHub OAuth token 仍然保留在 gh_auth_tokens 表里。即便 /user 验活 401 失效, +// 只要 token 行还在,gh_authorized 也应为 true(「token 已保留,可重新授权」), +// 避免前端误判"未授权"让玩家以为切匿名后授权被清掉了。 +// 判法:$ghAnonAvailable=true 且 $ghAuthorized 验活 401 失败 + gh_auth_tokens 有 token 行 +// → 改 gh_authorized=true,前端文案提示"实名通道失效,已保留授权记录,可重新授权"。 +if ($ghAnonAvailable && $probePlayer !== '' && !$ghAuthorized) { + 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->bindValue(':p', $probePlayer, SQLITE3_TEXT); + $tokRow = $stTok->execute()->fetchArray(SQLITE3_ASSOC); + if ($tokRow && !empty($tokRow['access_token'])) { + // token 行还在(哪怕 401 失效)→ 保留授权记录,前端可提示重新授权 + $ghAuthorized = true; + ghLog('cfg: anon available, token retained (401 but row exists) player=' . $probePlayer + . ' token=' . substr((string)$tokRow['access_token'], 0, 8)); + } + } catch (\Throwable $e) { + ghLog('cfg token retention check fail: ' . $e->getMessage()); + } +} + success([ 'gh_enabled' => !empty($cfg['gh_enabled']), 'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '', diff --git a/api/ticket.php b/api/ticket.php index ccbd336..ba016f9 100644 --- a/api/ticket.php +++ b/api/ticket.php @@ -889,6 +889,25 @@ function ticketReply() { // ★ 15m44z 第三部分:回复按工单 issue 归属派发——bot 建的单→机器人代发评论, // 实名单→玩家 OAuth token(老逻辑)。ghTicketTokenByTicket 内部按 gh_issue_owner 判。 $ghToken = ghTicketTokenByTicket($id); + // ★ 15m44z 第三部分:bot 工单 + 玩家 OAuth token 不匹配(ghTicketTokenByTicket 回退 + // 到 ghTicketToken 时,gh_issue_owner 未记录但匿名模式开→走 bot 路径拿 ghs_ token; + // 若拿到的还是 gho_/ghp_ 玩家 token 但工单归属 bot,说明 token 不匹配→ 静默跳过 + // 评论推送(不暴露玩家名字)。 + if (strpos($ghToken, 'gho_') === 0 || strpos($ghToken, 'ghp_') === 0) { + try { + $dbGh = getDB(); + $stO = $dbGh->prepare("SELECT gh_issue_owner FROM web_tickets WHERE id = :id LIMIT 1"); + $stO->bindValue(':id', $id, SQLITE3_INTEGER); + $oRow = $stO->execute()->fetchArray(SQLITE3_ASSOC); + $owner = ($oRow && isset($oRow['gh_issue_owner'])) ? (string)$oRow['gh_issue_owner'] : ''; + if ($owner === 'bot') { + // bot 工单但拿到玩家 token → 归属不匹配,静默跳过(避免暴露玩家名) + ghLog('reply skipped: bot ticket with player token ticket=' . $id); + success(['gh_synced' => false, 'gh_label' => false], '回复成功'); + return; + } + } catch (\Throwable $ignored) {} + } $ghSyncOk = ghSyncReply($id, $message, $ghToken, $player); $ghLabel = ghSyncLabel($id, 'user_reply', $ghToken); } catch (\Throwable $ignored) { $ghSyncOk = false; $ghLabel = false; } diff --git a/gh_api.php b/gh_api.php index 623feeb..b15f313 100644 --- a/gh_api.php +++ b/gh_api.php @@ -317,16 +317,21 @@ function ghSyncCreate($ticketId, $player, $title, $description, $images = [], $t /** * PHP 回复工单 → GitHub issue 评论。 * 评论正文带前缀 [工单#id] 防 Webhook 回推循环。 + * 15m44z 第三部分:按 gh_issue_owner 匿名/实名派发—— + * bot 建的单 → 评论不写提单人名字(匿名工单,提单信息只记 PHP 后台); + * 实名单 → 评论带 [玩家名] 角色标记(提单人是本人,公开无妨)。 */ function ghSyncReply($ticketId, $message, $token, $sender) { $cfg = ghGetCfg(); if (!$cfg['gh_enabled']) return false; if ($token === '') return false; $repo = $cfg['gh_repo']; - // 先查 gh_issue_number + // 先查 gh_issue_number + gh_issue_owner(一条 SQL 取回,省一次 DB 查询) + $issueNo = ''; + $owner = ''; try { $db = getDB(); - $st = $db->prepare("SELECT gh_issue_number FROM web_tickets WHERE id = :id"); + $st = $db->prepare("SELECT gh_issue_number, gh_issue_owner FROM web_tickets WHERE id = :id"); $st->bindValue(':id', $ticketId, SQLITE3_INTEGER); $row = $st->execute()->fetchArray(SQLITE3_ASSOC); if (!$row || empty($row['gh_issue_number'])) { @@ -334,12 +339,29 @@ function ghSyncReply($ticketId, $message, $token, $sender) { return false; } $issueNo = (string)$row['gh_issue_number']; + $owner = isset($row['gh_issue_owner']) ? (string)$row['gh_issue_owner'] : ''; } catch (\Throwable $e) { ghLog('read gh_issue_number: ' . $e->getMessage()); return false; } - // 评论带前缀 + 角色标记(Webhook 识别前缀跳过;角色标记供 UI 区分 玩家/管理/服务商) - $body = GH_TICKET_PREFIX . $ticketId . '][' . $sender . '] ' . $message; + // 15m44z 第三部分:按 issue 归属判定是否写提单人名字。 + // - gh_issue_owner = 'bot'(匿名工单)→ 不写 [sender] 标签,避免暴露提单人身份。 + // 提单人信息只记 PHP 后台 web_tickets.requester,仅管理员可见。 + // - gh_issue_owner = 'realname'(实名工单)→ 写 [玩家名] 角色标记(提单人是本人)。 + // - 老工单未记录 → 按 token 前缀判定:ghs_=bot 不写,gho_/ghp_=实名写。 + $isAnonComment = ($owner === 'bot'); + if ($isAnonComment === false && $owner === '') { + // 老工单未记录归属 → 按 token 前缀兜底判定 + $isAnonComment = (strpos((string)$token, 'ghs_') === 0); + } + // 构造评论正文 + if ($isAnonComment) { + // 匿名单:只写工单 ID 前缀 + 正文,不写提单人名字 + $body = GH_TICKET_PREFIX . $ticketId . '] ' . $message; + } else { + // 实名单:写 [工单#id][玩家名] 角色标记 + $body = GH_TICKET_PREFIX . $ticketId . '][' . $sender . '] ' . $message; + } $r = ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/comments', ['body' => $body], $token); if (!$r['ok']) ghLog('issue comment failed: ' . $r['msg']); return $r['ok']; diff --git a/player.php b/player.php index 9227bc0..fe18730 100644 --- a/player.php +++ b/player.php @@ -3997,10 +3997,20 @@ if ($currentVersion !== $BUILD_VERSION) { // ★ 15m44v: 按 gh_authorized 显示授权态(不再只靠 URL gh_ok 参数) const isAuthed = d.gh_authorized === true || new URLSearchParams(location.search).get('gh_ok') === '1'; + // ★ 15m44z 第三部分+token保留:匿名模式可用时,gh_authorized=true 可能是 + // "token 保留但 401 失效"(gh_cfg 兜底逻辑)。区分两种态: + // - 匿名模式可用 + gh_authorized=true → "授权已保留(可重新授权)" + // - 匿名模式不可用 + gh_authorized=true → "已授权,自动同步"(老态) if (isAuthed) { - if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue'; - if (sub) sub.textContent = '当前账号 token 有效,工单提交将自动同步到 GitHub issue'; - if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } + if (d.gh_anon_available === true) { + if (txt) txt.textContent = '🔗 GitHub 授权已保留,切匿名模式不影响授权记录'; + if (sub) sub.textContent = '实名通道 token 可能已失效,点重新授权可刷新;切匿名模式走系统机器人,无需玩家授权'; + if (btn) { btn.textContent = '重新授权'; btn.onclick = gotoGhOAuth; } + } else { + if (txt) txt.textContent = '✅ GitHub 已授权,工单+附件会自动同步到 issue'; + 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 账号'; @@ -4033,18 +4043,30 @@ if ($currentVersion !== $BUILD_VERSION) { window.location.href = url; } // ★ 15m44z 第三部分+开关:匿名开关 UI 联动。 - // 匿名开 → 隐藏「授权 GitHub」按钮(不需要授权,bot 代发),提示改文案; + // 匿名开 → 授权按钮隐藏(不需要玩家 OAuth,bot 代发),文案改匿名说明; // 匿名关 → 恢复授权按钮(走玩家 OAuth 建单,老逻辑)。 + // ★ 用户定调(15m44z 续):切匿名模式 ≠ 放弃实名授权。 + // 匿名开时,授权按钮文案改为「保留授权」(仍可重新授权),不隐藏。 + // 玩家切回关后授权自动恢复。 function syncGhAnonUI() { const btn = document.getElementById('ghAuthBtn'); const sub = document.getElementById('ghAuthSub'); const txt = document.getElementById('ghAuthText'); if (ticketState.anonTicket) { - if (btn) { btn.style.display = 'none'; } - if (txt) txt.textContent = '🤖 匿名工单:本单以匿名身份提交'; - if (sub) sub.textContent = '这条工单只有你自己和你主动分享的人能看到;取消勾选用你自己的账号提交'; + if (btn) { + // 匿名开:保留授权按钮但不隐藏,改文案提示"保留授权" + btn.textContent = '保留授权'; + btn.style.opacity = '0.7'; + btn.onclick = gotoGhOAuth; + } + if (txt) txt.textContent = '🤖 匿名工单:以匿名身份提交,这条工单只有你自己和你主动分享的人能看到'; + if (sub) sub.textContent = '取消勾选可改回实名授权提交;切匿名不影响已保留的授权记录'; } else { - if (btn) { btn.style.display = ''; } + if (btn) { + btn.style.opacity = '1'; + btn.textContent = '重新授权'; + btn.onclick = gotoGhOAuth; + } // 恢复默认文案(具体已授权/未授权由 loadGhCfg 的 isAuthed 段设置,这里只兜底) if (txt && txt.textContent.indexOf('🤖') === 0) txt.textContent = '⛓️ GitHub 工单同步'; if (sub && sub.textContent.indexOf('提单人信息仅后台保留') === 0) {