第65批续:修复 update_rule HTML 污染 JSON
根因: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 模式下数值列被渲染成字符串导致类型不匹配
This commit is contained in:
+29
-7
@@ -18,6 +18,21 @@ set_error_handler(function($errno, $errstr, $errfile, $errline) {
|
|||||||
@error_log("[credit.php PRE-LOAD] $errstr in $errfile on line $errline");
|
@error_log("[credit.php PRE-LOAD] $errstr in $errfile on line $errline");
|
||||||
return true;
|
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;
|
require_once $coreFile;
|
||||||
|
|
||||||
$action = $_GET['action'] ?? '';
|
$action = $_GET['action'] ?? '';
|
||||||
@@ -333,29 +348,36 @@ switch ($action) {
|
|||||||
// 构建动态 UPDATE
|
// 构建动态 UPDATE
|
||||||
$sets = [];
|
$sets = [];
|
||||||
$params = [];
|
$params = [];
|
||||||
|
$paramTypes = [];
|
||||||
$intFields = ['min_score', 'max_score', 'enabled', 'restriction_duration'];
|
$intFields = ['min_score', 'max_score', 'enabled', 'restriction_duration'];
|
||||||
$floatFields = ['limit_amount', 'multiplier', 'amount'];
|
$floatFields = ['limit_amount', 'multiplier', 'amount'];
|
||||||
$strFields = ['restriction_type', 'description'];
|
$strFields = ['restriction_type', 'description'];
|
||||||
foreach ($intFields as $f) {
|
foreach ($intFields as $f) {
|
||||||
$v = getParam($f, null);
|
$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) {
|
foreach ($floatFields as $f) {
|
||||||
$v = getParam($f, null);
|
$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) {
|
foreach ($strFields as $f) {
|
||||||
$v = getParam($f, null);
|
$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('没有要更新的字段');
|
if (empty($sets)) error('没有要更新的字段');
|
||||||
$sql = "UPDATE credit_rules SET " . implode(', ', $sets) . " WHERE id = ?";
|
$sql = "UPDATE credit_rules SET " . implode(', ', $sets) . " WHERE id = ?";
|
||||||
$params[] = $ruleId;
|
$params[] = $ruleId;
|
||||||
$stmt = $db->prepare($sql);
|
$paramTypes[] = SQLITE3_INTEGER;
|
||||||
for ($i = 0; $i < count($params); $i++) {
|
try {
|
||||||
$stmt->bindValue($i + 1, $params[$i], SQLITE3_TEXT);
|
$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 立即拉取
|
// ★ 第64批:规则变更后通知 Java 立即拉取
|
||||||
creditNotifyJavaSync($db, '__rules_sync__');
|
creditNotifyJavaSync($db, '__rules_sync__');
|
||||||
success(null, '规则已更新');
|
success(null, '规则已更新');
|
||||||
|
|||||||
Reference in New Issue
Block a user