Browse Source

记录接口调用时长

ccb_pay2
yangrz 3 years ago
parent
commit
97cfbb9db3
  1. 22
      app/Service/v3/CCBPayment.php

22
app/Service/v3/CCBPayment.php

@ -200,32 +200,32 @@ class CCBPayment
$options = ['json' => $params];
if (env('CCB_HTTP_PROXY')) {
$options['proxy'] = env('CCB_HTTP_PROXY');
}
$startTime = microtime(true);
try {
$response = $this->clientFactory->create(['timeout' => 120])->post($uri, $options);
$response = $this->clientFactory->create(['timeout' => 60])->post($uri, $options);
} catch (\Exception $e) {
$this->saveApiLog($uri, $params, $e->getMessage());
$this->saveApiLog(0, $uri, $params, $e->getMessage());
throw new BusinessException(500, '请求异常');
}
$useTime = round((microtime(true) - $startTime) * 1000, 2);
$content = $response->getBody()->getContents();
$result = json_decode($content, true);
if (!isset($result['Svc_Rsp_St']) || $result['Svc_Rsp_St'] != '00') {
$this->saveApiLog($uri, $params, $content);
$this->saveApiLog($useTime, $uri, $params, $content);
throw new BusinessException(500, ($result['Rsp_Inf'] ?? 'CCB请求失败'));
}
if (!isset($result['Sign_Inf']) || !$this->verifySign($this->createSign($result), $result['Sign_Inf'])) {
$this->saveApiLog($uri, $params, $content);
$this->saveApiLog($useTime, $uri, $params, $content);
throw new BusinessException(500, ($result['Rsp_Inf'] ?? 'CCB验签失败'));
}
if ($this->isDebug) {
$this->saveApiLog($uri, $params, $content);
$this->saveApiLog($useTime, $uri, $params, $content);
}
return $result;
@ -233,17 +233,19 @@ class CCBPayment
/**
* 保存API请求日志
* @param $useTime
* @param $uri
* @param $params
* @param $content
* @return void
*/
private function saveApiLog($uri, $params, $content)
private function saveApiLog($useTime, $uri, $params, $content)
{
$this->logger->info(
sprintf(
"%s\nUrl:%s\nParams:\n%s\nContent:\n%s\n",
"%s\nTime: %.2f ms\nUrl: %s\nParams:\n%s\nContent:\n%s\n",
'CCB API Log',
$useTime,
$uri,
json_encode($params, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
$content

Loading…
Cancel
Save