From 6a7f22f0fa78ef8d6d898b162873b91c765f48dc Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Fri, 30 Oct 2026 22:16:18 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC65=E6=89=B9=E7=BB=AD=EF=BC=9A=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20update=5Frule=20HTML=20=E6=B1=A1=E6=9F=93=20JSON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:credit.php 只设了 set_error_handler,没设 set_exception_handler。 update_rule case 无 try/catch,当 web 侧 DB(MySQL)连接断开或 execute() 失败时,SdfMySqlDb::fail() 对 DML 语句 throw new Exception(), 异常传播到 PHP 顶层默认处理器输出 HTML 错误页,污染 JSON 响应, 前端 r.json() 崩溃 'Unexpected token <'。 修复: 1. credit.php 顶部添加 set_exception_handler,全局兜底所有未捕获异常 转为 JSON 响应,不再输出 HTML 错误页 2. update_rule case 加 try/catch 第二层保护,返回精确错误信息 3. bindValue 类型修正:数值字段用 SQLITE3_INTEGER/SQLITE3_FLOAT, 避免 MySQL 模式下数值列被渲染成字符串导致类型不匹配 --- api/credit.php | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/api/credit.php b/api/credit.php index 2ba76a2..aea0cac 100644 --- a/api/credit.php +++ b/api/credit.php @@ -18,6 +18,21 @@ set_error_handler(function($errno, $errstr, $errfile, $errline) { @error_log("[credit.php PRE-LOAD] $errstr in $errfile on line $errline"); return true; }); +// ★ 第65批:未捕获异常(如 DB 连接断开、SQL 执行失败抛出的 Exception) +// 默认会被 PHP 顶层处理器输出成 HTML 错误页,污染 JSON 响应导致前端崩溃。 +// 这里把所有未捕获异常兜底转为 JSON,保证 API 任何路径都返回合法 JSON。 +set_exception_handler(function($ex) { + @error_log('[credit.php UNCAUGHT] ' . $ex->getMessage() + . ' @ ' . $ex->getFile() . ':' . $ex->getLine()); + while (ob_get_level() > 0) ob_end_clean(); + header('Content-Type: application/json; charset=utf-8'); + http_response_code(500); + echo json_encode([ + 'success' => false, + 'message' => '服务器内部错误: ' . $ex->getMessage(), + ], JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); + exit; +}); require_once $coreFile; $action = $_GET['action'] ?? ''; @@ -333,29 +348,36 @@ switch ($action) { // 构建动态 UPDATE $sets = []; $params = []; + $paramTypes = []; $intFields = ['min_score', 'max_score', 'enabled', 'restriction_duration']; $floatFields = ['limit_amount', 'multiplier', 'amount']; $strFields = ['restriction_type', 'description']; foreach ($intFields as $f) { $v = getParam($f, null); - if ($v !== null) { $sets[] = "$f = ?"; $params[] = (int)$v; } + if ($v !== null) { $sets[] = "$f = ?"; $params[] = (int)$v; $paramTypes[] = SQLITE3_INTEGER; } } foreach ($floatFields as $f) { $v = getParam($f, null); - if ($v !== null) { $sets[] = "$f = ?"; $params[] = (float)$v; } + if ($v !== null) { $sets[] = "$f = ?"; $params[] = (float)$v; $paramTypes[] = SQLITE3_FLOAT; } } foreach ($strFields as $f) { $v = getParam($f, null); - if ($v !== null) { $sets[] = "$f = ?"; $params[] = $v; } + if ($v !== null) { $sets[] = "$f = ?"; $params[] = $v; $paramTypes[] = SQLITE3_TEXT; } } if (empty($sets)) error('没有要更新的字段'); $sql = "UPDATE credit_rules SET " . implode(', ', $sets) . " WHERE id = ?"; $params[] = $ruleId; - $stmt = $db->prepare($sql); - for ($i = 0; $i < count($params); $i++) { - $stmt->bindValue($i + 1, $params[$i], SQLITE3_TEXT); + $paramTypes[] = SQLITE3_INTEGER; + try { + $stmt = $db->prepare($sql); + for ($i = 0; $i < count($params); $i++) { + $stmt->bindValue($i + 1, $params[$i], $paramTypes[$i]); + } + $stmt->execute(); + } catch (\Throwable $e) { + @error_log('[credit.php] update_rule 失败: ' . $e->getMessage()); + error('规则更新失败: ' . $e->getMessage(), 500); } - $stmt->execute(); // ★ 第64批:规则变更后通知 Java 立即拉取 creditNotifyJavaSync($db, '__rules_sync__'); success(null, '规则已更新');