1.优化统一订单推送报错记录日志

This commit is contained in:
2026-05-06 17:22:11 +08:00
parent 6883e74cf5
commit 234c607db0

View File

@@ -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;