74 lines
3.3 KiB
PHP
74 lines
3.3 KiB
PHP
<?php
|
||
// +----------------------------------------------------------------------
|
||
// | 企油通 · 回调接收
|
||
// +----------------------------------------------------------------------
|
||
// | 入口:public/qytconsume.php -> /qytNotify/consume
|
||
// | 对应企油通 7.10「运单加油明细推送(异步)」,我方须返回 {"code":"100","msg":"Success"}
|
||
// +----------------------------------------------------------------------
|
||
declare (strict_types=1);
|
||
|
||
namespace app\api\controller;
|
||
|
||
use app\common\crypto\Qyt as QytCrypto;
|
||
use app\common\service\QueueService;
|
||
use app\common\service\QytOil;
|
||
use think\facade\Request;
|
||
|
||
class QytNotify extends Controller
|
||
{
|
||
/**
|
||
* 7.10 运单加油明细推送
|
||
* @return \think\response\Json
|
||
*/
|
||
public function consume()
|
||
{
|
||
$request = Request::instance();
|
||
// ⚠️ 企油通用 multipart/form-data 推送,业务参数在表单字段 params 里(JSON 字符串)。
|
||
// 这种 Content-Type 下 PHP 不会填充 php://input,读请求体恒为空 —— 必须取表单字段。
|
||
// 回退读请求体只是为了兼容 JSON 推送与本地测试脚本。
|
||
// ⚠️ 这里必须直接读 $_POST:cores\Request 给所有入参挂了全局过滤器
|
||
// ['my_trim','my_htmlspecialchars','filter_emoji'],走 $request->post() 取到的 JSON
|
||
// 引号会被转义成 ",json_decode 必然失败(回调会被静默当成空明细忽略)
|
||
$formParams = isset($_POST['params']) ? trim((string) $_POST['params']) : '';
|
||
$raw = $request->getContent();
|
||
|
||
(new QytOil())->saveLog('7.10 消费回调原文[' . $request->contentType() . ']:'
|
||
. ($formParams !== '' ? 'params=' . $formParams : $raw));
|
||
|
||
if ($formParams !== '') {
|
||
$params = json_decode($formParams, true);
|
||
// 兜底:若上游或中间层做过 HTML 转义,反转义后再解析一次
|
||
if (!is_array($params) && strpos($formParams, '"') !== false) {
|
||
$params = json_decode(htmlspecialchars_decode($formParams, ENT_QUOTES), true);
|
||
}
|
||
} else {
|
||
// 兼容:外层信封 {appId,timestamp,sign,params(JSON字符串)} 或 直接推 body
|
||
$body = json_decode($raw, true);
|
||
$params = $body;
|
||
if (is_array($body) && isset($body['params'])) {
|
||
$params = is_string($body['params']) ? json_decode($body['params'], true) : $body['params'];
|
||
}
|
||
}
|
||
|
||
// TODO 验签(规则待对接人确认)
|
||
// if (is_array($body) && !empty($body['sign'])) {
|
||
// $crypto = new QytCrypto();
|
||
// if (!$crypto->verify($params, $body['sign'])) {
|
||
// return json(['code' => '99', 'msg' => '验签失败']);
|
||
// }
|
||
// }
|
||
|
||
// 无明细也回 100,避免企油通重复推送
|
||
if (is_array($params) && !empty($params['order_list'])) {
|
||
(new QueueService())->push('callback_queue', json_encode([
|
||
'header' => ['method' => 'qyt_consume'],
|
||
'res' => $params,
|
||
], 320));
|
||
} else {
|
||
(new QytOil())->saveLog('7.10 消费回调无 order_list,已忽略');
|
||
}
|
||
|
||
return json(['code' => '100', 'msg' => 'Success']);
|
||
}
|
||
}
|