56 lines
2.0 KiB
PHP
56 lines
2.0 KiB
PHP
|
<?php
|
||
|
namespace app\util;
|
||
|
use app\exception\LogicException;
|
||
|
use app\service\util\CmbLifeUtils;
|
||
|
use GuzzleHttp\Client;
|
||
|
use GuzzleHttp\RequestOptions;
|
||
|
|
||
|
class CmbHttpUtils
|
||
|
{
|
||
|
/**
|
||
|
* post 提交
|
||
|
* @param $funcName
|
||
|
* @param $requestParams
|
||
|
* @return mixed|string
|
||
|
* @throws LogicException
|
||
|
*/
|
||
|
public static function doPost($funcName, $requestParams)
|
||
|
{
|
||
|
//添加公共参数
|
||
|
$cmbConfig = config('cmb');
|
||
|
$requestParams['mid'] = $cmbConfig['mid'];
|
||
|
$requestParams['aid'] = $cmbConfig['aid'];
|
||
|
$requestParams['date'] = date('YmdHis');
|
||
|
$requestParams['random'] = StringUtil::generateRandomString(32); // 随机字符串,
|
||
|
$requestParams['keyAlias'] = 'CO_PUB_KEY_SM2';
|
||
|
$requestParams['cmbKeyAlias'] = 'SM2_CMBLIFE'; //当 cmbKeyAlias 为 SM2_CMBLIFE 时,走国密算法进行验签
|
||
|
$sign = CmbLifeUtils::signForRequest($funcName, $requestParams, $cmbConfig['merchant_sm2_pri_key']);
|
||
|
$requestParams['sign'] = $sign;
|
||
|
$url = env('cmb.api_host') . $funcName . '.json';
|
||
|
$data = self::httpPost($url, $requestParams);
|
||
|
$res = \GuzzleHttp\json_decode($data, true);
|
||
|
// 校验返回报文
|
||
|
if (isset($res['sign'])) {
|
||
|
$verifyResult = CmbLifeUtils::verifyForResponse($res);
|
||
|
if ($verifyResult) { // 验签成功获取数据
|
||
|
if (isset($res['encryptBody'])) {
|
||
|
$res['decryptBody'] = CmbLifeUtils::decrypt((string)$res['encryptBody'], $cmbConfig['merchant_sm2_pri_key']);
|
||
|
} else {
|
||
|
return $res;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return $res;
|
||
|
}
|
||
|
private static function httpPost(string $uri, array $data)
|
||
|
{
|
||
|
$client = new Client([
|
||
|
'base_uri' => env('cmb.api_host'),
|
||
|
'timeout' => 50,
|
||
|
]);
|
||
|
$response = $client->request('POST', $uri, [
|
||
|
RequestOptions::FORM_PARAMS => $data,
|
||
|
]);
|
||
|
return $response->getBody()->getContents();
|
||
|
}
|
||
|
}
|