15m44z 第三部分续:匿名工单回复去玩家名 + token 保留 + 匿名开关 UI

匿名工单(gh_issue_owner=bot)回复/撤单/完结不再在 GitHub 评论写提单人名字;
ghSyncReply 按 gh_issue_owner 判:bot 单省略 [sender] 标签,实名单保留;
ticketReply 加 guard:bot 单+玩家 token 不匹配时静默跳过评论推送;
gh_cfg.php:切匿名模式时玩家 OAuth token 保留(gh_authorized 兜底 true);
player.php:匿名开时授权按钮改保留授权不隐藏,匿名关恢复重新授权。
This commit is contained in:
ypsdf1
2026-11-26 16:38:18 +08:00
parent 52594e266f
commit 84d2132070
4 changed files with 99 additions and 12 deletions
+24
View File
@@ -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'] : '',
+19
View File
@@ -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; }
+26 -4
View File
@@ -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'];
+30 -8
View File
@@ -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) {