diff --git a/api/gh_close_probe.php b/api/gh_close_probe.php new file mode 100644 index 0000000..ae227ad --- /dev/null +++ b/api/gh_close_probe.php @@ -0,0 +1,46 @@ + false, 'msg' => 'forbidden']); + exit; +} + +$issueNo = (int)($_GET['issue'] ?? 0); +$kind = ($_GET['kind'] ?? 'rejected') === 'rejected' ? 'rejected' : 'completed'; +// 取任意一个提报玩家的 token(用工单 1 的 requester,或指定) +$ticketId = (int)($_GET['ticket'] ?? 1); +$token = ghTicketTokenByTicket($ticketId); +if ($token === '') { + echo json_encode(['ok' => false, 'msg' => 'no token for ticket ' . $ticketId]); + exit; +} +$repo = ghGetCfg()['gh_repo']; +$stateReason = ($kind === 'rejected') ? 'not_planned' : 'completed'; + +// 直接调 close 拿真实响应 +$r = ghApiRequest('POST', '/repos/' . $repo . '/issues/' . $issueNo . '/close', + ['state' => 'closed', 'state_reason' => $stateReason], $token); + +// 再读回 issue 状态确认 +$after = ghApiRequest('GET', '/repos/' . $repo . '/issues/' . $issueNo, null, $token); + +echo json_encode([ + 'ok' => $r['ok'], + 'close_status' => $r['status'], + 'close_msg' => $r['msg'], + 'issue_after_state' => is_array($after['data']) ? ($after['data']['state'] ?? null) : null, + 'issue_after_reason' => is_array($after['data']) ? ($after['data']['state_reason'] ?? null) : null, + 'repo' => $repo, + 'issue' => $issueNo, + 'kind' => $kind, + 'token_preview' => substr($token, 0, 8) . '...', +], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); diff --git a/api/ticket.php b/api/ticket.php index 5f1057b..443ec66 100644 --- a/api/ticket.php +++ b/api/ticket.php @@ -472,6 +472,16 @@ function ghTicketTokenByTicket($ticketId) { } } +/** + * 15m44r: 去重命中留痕(复用 gh_sync.log 写入器 ghLog;若未加载则静默)。 + * 用于定位"重复提交被拦截"的实际发生情况,便于核对 CF 卡顿场景。 + */ +function ghLogDedup($player, $fingerprint, $existingTicketId) { + if (function_exists('ghLog')) { + ghLog('dedup hit: player=' . $player . ' hash=' . substr($fingerprint, 0, 12) . ' existing_ticket=' . $existingTicketId); + } +} + function ticketCreate() { $info = requireWebToken(); $player = $info['player']; @@ -493,21 +503,78 @@ function ticketCreate() { $db = getDB(); $now = time(); + // ★ 15m44r: 老库补 content_hash 列(去重指纹)。ticketListAll 的 lazy bootstrap 也补,双保险。 + try { $db->exec("ALTER TABLE web_tickets ADD COLUMN content_hash TEXT DEFAULT ''"); } catch (\Throwable $e) { /* 列已存在 */ } + + // ★ 15m44r: 同内容去重锁 —— 同一玩家短时间内(60s)提交完全相同内容的工单, + // 只认第一单,后续返回已建单 ID。兜底 CF 卡顿导致用户"以为没反应"反复点提交 + // 造成重复建单(14:13 实测 19s 内连建 #16/#17 两个内容一致的工单)。 + // 指纹 = type + title + description + 附件文件名(不含随机 URL,避免同内容算出不同指纹)。 + $attachNames = []; + foreach ((is_array($images) ? $images : []) as $iu) { + if (is_string($iu)) { + // @gh:附件图N:文件名 → 取冒号后的文件名;http(s) URL → 原样 + $attachNames[] = (strpos($iu, '@gh:') === 0) ? str_replace('@gh:', '', $iu) : $iu; + } + } + sort($attachNames); + $fingerprint = hash('sha1', $type . "\x1f" . $title . "\x1f" . $description . "\x1f" . implode("\x1f", $attachNames)); + $dedupWindow = 60; + $dupStmt = $db->prepare("SELECT id, created_at FROM web_tickets WHERE requester = :player AND status IN ('submitted','replied') AND created_at >= :since ORDER BY created_at DESC LIMIT 1"); + $dupStmt->bindValue(':player', $player, SQLITE3_TEXT); + $dupStmt->bindValue(':since', $now - $dedupWindow, SQLITE3_INTEGER); + $dupRow = $dupStmt->execute()->fetchArray(SQLITE3_ASSOC); + if ($dupRow) { + // 候选重复单:再按指纹比对它的 content_hash(老单没指纹则回退比标题+描述) + $dupHash = $dupRow['content_hash'] ?? ''; + $isDup = ($dupHash !== '' && $dupHash === $fingerprint); + if (!$isDup && $dupHash === '') { + // 老单无指纹列值 → 退化:同 60s 内同玩家同标题同描述即判重 + $dupFull = $db->prepare("SELECT title, description, images FROM web_tickets WHERE id = :id"); + $dupFull->bindValue(':id', $dupRow['id'], SQLITE3_INTEGER); + $df = $dupFull->execute()->fetchArray(SQLITE3_ASSOC); + if ($df && $df['title'] === $title && $df['description'] === $description) { + $isDup = true; + } + } + if ($isDup) { + ghLogDedup($player, $fingerprint, (int)$dupRow['id']); + success(['id' => (int)$dupRow['id'], 'images' => decodeTicketImages($dupRow['images'] ?? null), 'gh_synced' => false, 'dedup' => true], '相同工单已提交,请勿重复提交'); + } + } + $db->exec('BEGIN IMMEDIATE'); try { - $stmt = $db->prepare("INSERT INTO web_tickets (type, status, requester, title, description, images, created_at, updated_at) VALUES (:type, 'submitted', :player, :title, :desc, :images, :now, :now)"); + $stmt = $db->prepare("INSERT INTO web_tickets (type, status, requester, title, description, images, content_hash, created_at, updated_at) VALUES (:type, 'submitted', :player, :title, :desc, :images, :hash, :now, :now)"); $stmt->bindValue(':type', $type, SQLITE3_TEXT); $stmt->bindValue(':player', $player, SQLITE3_TEXT); $stmt->bindValue(':title', $title, SQLITE3_TEXT); $stmt->bindValue(':desc', $description, SQLITE3_TEXT); $stmt->bindValue(':images', encodeTicketImages($images), SQLITE3_TEXT); + $stmt->bindValue(':hash', $fingerprint, SQLITE3_TEXT); $stmt->bindValue(':now', $now, SQLITE3_INTEGER); $stmt->execute(); $ticketId = $db->lastInsertRowID(); $db->exec('COMMIT'); } catch (Exception $e) { $db->exec('ROLLBACK'); - error('创建失败: ' . $e->getMessage()); + // ★ 兜底:老库无 content_hash 列时回退老 INSERT(不破坏兼容) + try { + $db->exec('BEGIN IMMEDIATE'); + $stmt2 = $db->prepare("INSERT INTO web_tickets (type, status, requester, title, description, images, created_at, updated_at) VALUES (:type, 'submitted', :player, :title, :desc, :images, :now, :now)"); + $stmt2->bindValue(':type', $type, SQLITE3_TEXT); + $stmt2->bindValue(':player', $player, SQLITE3_TEXT); + $stmt2->bindValue(':title', $title, SQLITE3_TEXT); + $stmt2->bindValue(':desc', $description, SQLITE3_TEXT); + $stmt2->bindValue(':images', encodeTicketImages($images), SQLITE3_TEXT); + $stmt2->bindValue(':now', $now, SQLITE3_INTEGER); + $stmt2->execute(); + $ticketId = $db->lastInsertRowID(); + $db->exec('COMMIT'); + } catch (Exception $e2) { + $db->exec('ROLLBACK'); + error('创建失败: ' . $e2->getMessage()); + } } // 15m44f 诊断日志(success 前写) @@ -753,6 +820,10 @@ function ticketListAll() { try { $db->exec("ALTER TABLE web_tickets ADD COLUMN gh_issue_number TEXT DEFAULT ''"); } catch (Exception $e) { /* 列已存在 */ } + // ★ 15m44r: 老库补 content_hash 列(同内容去重指纹) + try { + $db->exec("ALTER TABLE web_tickets ADD COLUMN content_hash TEXT DEFAULT ''"); + } catch (Exception $e) { /* 列已存在 */ } $db->exec("CREATE TABLE IF NOT EXISTS web_ticket_replies ( id INTEGER PRIMARY KEY AUTOINCREMENT, ticket_id INTEGER NOT NULL, diff --git a/player.php b/player.php index 660a945..c473ba2 100644 --- a/player.php +++ b/player.php @@ -4124,12 +4124,36 @@ if ($currentVersion !== $BUILD_VERSION) { } async function submitTicket() { + // ★ 15m44r: 提交按钮锁 —— 点一次就锁掉,后端慢(CF 卡)期间重复点击不会重复建单。 + // 1) 前端即时锁按钮(防同帧双击);2) 后端 content-hash 去重兜底 + // (即便 CF 卡住让用户"以为没反应"再点,后端也只认第一单)。 + const btn = document.querySelector('button[onclick="submitTicket()"]'); + if (ticketState.submitting) { + if (btn) btn.style.opacity = '0.5'; + return; + } + if (btn) { + btn.disabled = true; + btn.style.opacity = '0.6'; + btn.textContent = '提交中…'; + ticketState.submitting = true; + } const type = document.getElementById('ticketType').value; const title = document.getElementById('ticketTitle').value.trim(); const desc = document.getElementById('ticketDesc').value.trim(); const errEl = document.getElementById('ticketError'); - if (!title) { errEl.textContent = '请输入标题'; return; } - if (!desc) { errEl.textContent = '请输入描述'; return; } + if (!title) { + errEl.textContent = '请输入标题'; + ticketState.submitting = false; + if (btn) { btn.disabled = false; btn.style.opacity = '1'; btn.textContent = '提交工单'; } + return; + } + if (!desc) { + errEl.textContent = '请输入描述'; + ticketState.submitting = false; + if (btn) { btn.disabled = false; btn.style.opacity = '1'; btn.textContent = '提交工单'; } + return; + } // 15m44k: 图片是"可选附件"不是必填——0 张也能提交工单(走 JSON 无图路径)。 // 旧 15m44i 误把 0 张当必填拦截, 删完所有图就提交不了。改为: 0 张不拦, 仅清空 errEl。 const files = ticketState.files || []; @@ -4185,12 +4209,19 @@ if ($currentVersion !== $BUILD_VERSION) { ticketState.files = []; ticketState.images = imgs; // 详情视图渲染时 applyTicketImgs 用 ticketState.view = 'list'; + ticketState.submitting = false; // 15m44r: 成功才解锁(此时已切到列表视图) renderTicket(document.getElementById('content')); } else { errEl.textContent = data.message; + // 15m44r: 业务失败(如重复单/校验)解锁按钮允许重试 + ticketState.submitting = false; + if (btn) { btn.disabled = false; btn.style.opacity = '1'; btn.textContent = '提交工单'; } } } catch (e) { errEl.textContent = '提交失败: ' + e.message; + // 15m44r: 网络失败(CF 超时)解锁,让用户能重试(后端去重兜底不重复建单) + ticketState.submitting = false; + if (btn) { btn.disabled = false; btn.style.opacity = '1'; btn.textContent = '提交工单'; } } }