|
|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
<?php
|
|
|
|
|
namespace App\Lib;
|
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
|
|
class HSM
|
|
|
|
|
{
|
|
|
|
|
@ -46,7 +47,7 @@ public static function HsmDecrypt($str){
|
|
|
|
|
$encryptStr=self::post($url,$data);
|
|
|
|
|
|
|
|
|
|
$r_data=json_decode($encryptStr, true);
|
|
|
|
|
if($r_data['status']==0){
|
|
|
|
|
if($r_data && $r_data['status']==0){
|
|
|
|
|
return ['status'=>true,'data'=>hex2bin($r_data['body']['plain'])];
|
|
|
|
|
}else{
|
|
|
|
|
return ['status'=>false];
|
|
|
|
|
@ -80,17 +81,41 @@ public function post($url, $data_string,$or_str='')
|
|
|
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 添加超时:最大执行时间30秒
|
|
|
|
|
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); // 添加超时:连接超时10秒
|
|
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, [
|
|
|
|
|
'Content-Type: application/json; charset=utf-8',
|
|
|
|
|
'Content-Length: ' . strlen($data_string)
|
|
|
|
|
]);
|
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
|
|
|
|
|
$r = curl_exec($curl);
|
|
|
|
|
|
|
|
|
|
// 检查curl错误
|
|
|
|
|
if (curl_errno($curl)) {
|
|
|
|
|
$error_msg = curl_error($curl);
|
|
|
|
|
$error_code = curl_errno($curl);
|
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
|
|
// 记录到Laravel日志
|
|
|
|
|
Log::error('HSM curl请求失败', [
|
|
|
|
|
'url' => $url,
|
|
|
|
|
'error_code' => $error_code,
|
|
|
|
|
'error_msg' => $error_msg,
|
|
|
|
|
'request_data' => $or_str,
|
|
|
|
|
'post_data_preview' => substr($data_string, 0, 500)
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// 返回错误信息
|
|
|
|
|
return json_encode(['status' => 1, 'message' => 'curl错误: ' . $error_msg]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
curl_close($curl);
|
|
|
|
|
|
|
|
|
|
date_default_timezone_set('PRC');
|
|
|
|
|
$table_name='zz_request_log_' . date('ym');
|
|
|
|
|
$formatted_date= date("Y-m-d H:i:s");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
DB::table($table_name)->insert([
|
|
|
|
|
'request_ip'=>'',
|
|
|
|
|
'response_data'=>json_encode($r, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
@ -100,8 +125,16 @@ public function post($url, $data_string,$or_str='')
|
|
|
|
|
'request_url'=>$url,
|
|
|
|
|
'create_time' => $formatted_date,
|
|
|
|
|
'update_time' => $formatted_date
|
|
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
// 数据库日志记录失败不影响主流程,只记录到Laravel日志
|
|
|
|
|
Log::error('HSM数据库日志记录失败', [
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
'file' => $e->getFile(),
|
|
|
|
|
'line' => $e->getLine(),
|
|
|
|
|
'url' => $url
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $r;
|
|
|
|
|
}
|
|
|
|
|
|