From 638755df290ff3178c514ebdb2cc0da2d4bb8dc2 Mon Sep 17 00:00:00 2001 From: ypsdf1 Date: Wed, 28 Oct 2026 21:12:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC58=E6=89=B9=E7=BB=AD25=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9Async=5Fpush=20=E5=85=BC=E5=AE=B9=20getParam?= =?UTF-8?q?=20=E5=B7=B2=E8=A7=A3=E7=A0=81=E6=95=B0=E7=BB=84=EF=BC=88PHP8?= =?UTF-8?q?=20json=5Fdecode(=E6=95=B0=E7=BB=84)=20=E6=8A=9B=20TypeError?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根因:Java pushCreditToPhp 发 JSON body {players:{...},records:[...]}, getParam 对 JSON body 直接返回已解码数组,原代码再 json_decode() 在 PHP8 下 TypeError 500。 修复:getParam 拿到后仅当 is_string 才 json_decode,否则直接用。 另修复生产服 PHP 端 credit.php 未部署导致 sync_pull 返回「未知操作」的问题。 --- api/credit.php | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/api/credit.php b/api/credit.php index 21b8b3a..6a11a2c 100644 --- a/api/credit.php +++ b/api/credit.php @@ -296,12 +296,17 @@ switch ($action) { $syncSecret = getParam('secret', ''); if (!$syncSecret || $syncSecret !== SECRET_KEY) error('同步密钥无效', 401); - $playersJson = getParam('players', ''); - $recordsJson = getParam('records', ''); + // ★ getParam 对 JSON body 直接返回已解码数组,无需再 json_decode(PHP8 json_decode(数组) 抛 TypeError) + $playerData = getParam('players', null); + if (is_string($playerData)) $playerData = json_decode($playerData, true); + if (!is_array($playerData)) $playerData = []; + + $recordData = getParam('records', null); + if (is_string($recordData)) $recordData = json_decode($recordData, true); + if (!is_array($recordData)) $recordData = []; // 批量更新玩家积分 - $playerData = json_decode($playersJson, true); - if (is_array($playerData) && !empty($playerData)) { + if (!empty($playerData)) { $db->beginTransaction(); foreach ($playerData as $pname => $score) { // 确保玩家存在 @@ -325,8 +330,7 @@ switch ($action) { } // 批量插入/更新积分记录(去重:同 target+source+created_at 的记录跳过) - $recordData = json_decode($recordsJson, true); - if (is_array($recordData) && !empty($recordData)) { + if (!empty($recordData)) { foreach ($recordData as $rec) { $target = $rec['target_player'] ?? ''; $reporter = $rec['reporter_player'] ?? '';