From 234c607db01c304b23ce8ab8097d160a92f19650 Mon Sep 17 00:00:00 2001 From: zhenhui <1276357500@qq.com> Date: Wed, 6 May 2026 17:22:11 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BC=98=E5=8C=96=E7=BB=9F=E4=B8=80=E8=AE=A2?= =?UTF-8?q?=E5=8D=95=E6=8E=A8=E9=80=81=E6=8A=A5=E9=94=99=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/library/MallBonusGrantPush.php | 80 +++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/app/common/library/MallBonusGrantPush.php b/app/common/library/MallBonusGrantPush.php index defd585..4905c33 100644 --- a/app/common/library/MallBonusGrantPush.php +++ b/app/common/library/MallBonusGrantPush.php @@ -8,6 +8,7 @@ use app\common\model\MallItem; use app\common\model\MallOrder; use app\common\model\MallUserAsset; use GuzzleHttp\Client; +use support\Log; /** * 红利订单调用 PlayX bonus/grant(与定时任务、后台手动推送共用) @@ -23,7 +24,7 @@ final class MallBonusGrantPush if (!is_array($conf)) { return [ 'ok' => false, - 'message' => 'PlayX angpow_import not configured', + 'message' => 'playX angpow_import not configured', ]; } @@ -34,7 +35,7 @@ final class MallBonusGrantPush if ($baseUrl === '' || $path === '' || $merchantCode === '' || $authKey === '') { return [ 'ok' => false, - 'message' => 'PlayX Angpow Import API not configured', + 'message' => 'playX Angpow Import API not configured', ]; } @@ -113,7 +114,33 @@ final class MallBonusGrantPush 'json' => $payload, ]); - $data = json_decode(strval($res->getBody()), true) ?? []; + $httpStatus = 0; + if (is_object($res) && method_exists($res, 'getStatusCode')) { + $sc = $res->getStatusCode(); + if (is_int($sc)) { + $httpStatus = $sc; + } + } + + $rawBody = strval($res->getBody()); + $data = json_decode($rawBody, true); + if (!is_array($data)) { + $jsonDetail = 'root not JSON object'; + if (json_last_error() !== JSON_ERROR_NONE) { + $jem = json_last_error_msg(); + $jsonDetail = is_string($jem) && $jem !== '' ? $jem : 'JSON parse error'; + } + Log::error( + '[MallBonusGrantPush] response not a JSON object | order_id=' . $order->id + . ' | http=' . $httpStatus . ' | ' . $jsonDetail . ' | body=' . self::truncateForLog($rawBody) + ); + + return [ + 'ok' => false, + 'message' => 'Invalid response', + ]; + } + if (($data['code'] ?? null) === '0' || ($data['code'] ?? null) === 0) { return [ 'ok' => true, @@ -121,11 +148,34 @@ final class MallBonusGrantPush ]; } + $remoteMsg = $data['message'] ?? $data['msg'] ?? ''; + if (!is_string($remoteMsg)) { + $remoteMsg = ''; + } + if ($remoteMsg === '') { + $remoteMsg = 'playX angpow import not accepted'; + } + + $flags = JSON_UNESCAPED_UNICODE; + if (defined('JSON_INVALID_UTF8_SUBSTITUTE')) { + $flags = $flags | JSON_INVALID_UTF8_SUBSTITUTE; + } + $encoded = json_encode($data, $flags); + if (!is_string($encoded)) { + $encoded = $rawBody; + } + Log::error( + '[MallBonusGrantPush] angpow import rejected | order_id=' . $order->id + . ' | http=' . $httpStatus . ' | parsed=' . self::truncateForLog($encoded) + ); + return [ 'ok' => false, - 'message' => strval($data['message'] ?? 'PlayX angpow import not accepted'), + 'message' => $remoteMsg, ]; } catch (\Throwable $e) { + Log::error('[MallBonusGrantPush] request exception | order_id=' . $order->id . ' | ' . $e->getMessage()); + return [ 'ok' => false, 'message' => $e->getMessage(), @@ -133,6 +183,28 @@ final class MallBonusGrantPush } } + private static function truncateForLog(string $text, int $maxLen = 8000): string + { + $s = trim($text); + if ($s === '') { + return ''; + } + $oneLine = preg_replace('/\s+/', ' ', $s); + $snippet = is_string($oneLine) && $oneLine !== '' ? $oneLine : $s; + if (function_exists('mb_strlen') && function_exists('mb_substr')) { + if (mb_strlen($snippet, 'UTF-8') > $maxLen) { + return mb_substr($snippet, 0, $maxLen, 'UTF-8') . '…'; + } + + return $snippet; + } + if (strlen($snippet) > $maxLen) { + return substr($snippet, 0, $maxLen) . '…'; + } + + return $snippet; + } + private static function buildSignature(string $input, string $authKey): ?string { $keyBytes = null;