Files
Sdf1_login/api/gh_cfg.php
T
ypsdf1 7acbcf6198 15m44w 续5: 横条根因(gh_auth_tokens建表TEXT PK改VARCHAR)+webhook撤销联动+诊断端点
① 横条: gh_auth_tokens 建表声明 TEXT PRIMARY KEY 在 MySQL 下不合法(TEXT 不能做 PK),
   改 VARCHAR(500) 对齐线上 caoyuanphp 实际 schema(gh_cfg.php+gh_oauth.php 两处)。
   gh_cfg.php 验活加 ghLog 日志(status/ok/token前缀)便于定位断点。
② webhook 撤销联动(用户定调): issue 关闭事件 state_reason=completed/done 且 label 含「已撤销」
   → PHP 工单撤销(withdrawn)。原只分 not_planned→rejected/其它→completed, 现加 已撤销+done→withdrawn。
③ 新增 api/gh_diag.php 临时诊断端点(验活全链路逐步输出, 定位横条断点)。
2026-11-25 19:47:23 +08:00

64 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 15m44m: 工单前端判断「GitHub issue 同步是否启用」的懒重载端点。
* 返回 {gh_enabled:bool, gh_repo:string, gh_authorized:bool}。前端进工单表单时 fetch:
* - gh_enabled=true 才显示「授权 GitHub 工单同步」按钮
* - gh_repo 用于按钮文案(caoyuanwiki)
* - 15m44v: gh_authorized=true 时前端显示「已授权」态(不再误判为未授权)
* 带 ?token= 时反解玩家名查 gh_auth_tokens 是否已有有效 token。
*/
while (ob_get_level() > 0) { @ob_end_clean(); }
ob_start();
header('Content-Type: application/json; charset=utf-8');
header('X-Content-Type-Options: nosniff');
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();
// ★ 15m44w 续4:建表用 VARCHAR(500) 对齐线上 caoyuanphp.gh_auth_tokens 实际 schema
// (旧版 TEXT PRIMARY KEY 在 MySQL 下 TEXT 不能做 PK 报 1101; 表已存在时 IF NOT EXISTS
// 跳过不抛, 但为双模式兼容(老 SQLite 库也建 VARCHAR 无害)改 VARCHAR(500))。
$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->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'));
} else {
ghLog('cfg verify player=' . $probePlayer . ' no token row in gh_auth_tokens');
}
} catch (\Throwable $e) {
ghLog('cfg verify exception: ' . $e->getMessage());
$ghAuthorized = false;
}
}
}
success([
'gh_enabled' => !empty($cfg['gh_enabled']),
'gh_repo' => isset($cfg['gh_repo']) ? $cfg['gh_repo'] : '',
'gh_authorized' => $ghAuthorized,
], 'ok');