From ac382e288c5b0b5a6a2e8694839634098f2e6a6c Mon Sep 17 00:00:00 2001 From: wucongxing <815046773@qq.com> Date: Thu, 23 Mar 2023 10:34:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=89=A9=E6=B5=81=E5=9B=9E?= =?UTF-8?q?=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CallBackController.php | 83 ++++++++++++++++++++++++++- app/Controller/TestController.php | 8 ++- extend/Ca/Ca.php | 57 +++++++++++++++--- 3 files changed, 137 insertions(+), 11 deletions(-) diff --git a/app/Controller/CallBackController.php b/app/Controller/CallBackController.php index dc56c26..9b04f5b 100644 --- a/app/Controller/CallBackController.php +++ b/app/Controller/CallBackController.php @@ -535,8 +535,6 @@ class CallBackController extends AbstractController return $server->serve(); } - - /** * 微信支付返回错误响应 * @param string $message @@ -733,8 +731,87 @@ class CallBackController extends AbstractController } // 处方平台物流回调 - public function platformLogisticsCallBack(){ + public function platformLogisticsCallBack(): ResponseInterface + { + $request_params = $this->request->all(); + try { + Log::getInstance()->info("处方平台物流回调数据:" . json_encode($request_params,JSON_UNESCAPED_UNICODE)); + if (!isset($request_params['sign']) ){ + Log::getInstance()->error("处方平台物流回调数据处理失败:缺少签名结果"); + return $this->platformLogisticsErrorReturn("缺少签名结果"); + } + + if (!isset($request_params['nonce']) ){ + Log::getInstance()->error("处方平台物流回调数据处理失败:缺少随机数"); + return $this->platformLogisticsErrorReturn("缺少随机数"); + } + + if (!isset($request_params['timestamp']) ){ + Log::getInstance()->error("处方平台物流回调数据处理失败:缺少签名时间戳"); + return $this->platformLogisticsErrorReturn("缺少签名时间戳"); + } + + if (!isset($request_params['paramJsonStr']) ){ + Log::getInstance()->error("处方平台物流回调数据处理失败:缺少数据体"); + return $this->platformLogisticsErrorReturn("缺少数据体"); + } + + $param_json_str = json_decode($request_params['paramJsonStr'],true); + if (empty($param_json_str)){ + Log::getInstance()->error("处方平台物流回调数据处理失败:数据体解析失败"); + return $this->platformLogisticsErrorReturn("数据体解析失败"); + } + + + } catch (\Exception $e) { + // 验证失败 + Log::getInstance()->error("Im回调数据处理失败:" . $e->getMessage()); + return $this->platformLogisticsErrorReturn($e->getMessage()); + } + + return $this->platformLogisticsSuccessReturn(); + } + + /** + * 处方平台物流返回错误响应 + * @param string $message + * @return ResponseInterface + */ + protected function platformLogisticsErrorReturn(string $message): ResponseInterface + { + return $this->response + ->withStatus(200) + ->withBody( + new SwooleStream( + strval( + json_encode([ + 'resultCode' => '0', + 'resultDesc' => $message, + ], JSON_UNESCAPED_UNICODE) + ) + ) + ); + } + + /** + * 处方平台物流返回正确响应 + * @return ResponseInterface + */ + protected function platformLogisticsSuccessReturn(): ResponseInterface + { + return $this->response + ->withStatus(200) + ->withBody( + new SwooleStream( + strval( + json_encode([ + 'resultCode' => "1000", + 'resultDesc' => "成功", + ], JSON_UNESCAPED_UNICODE) + ) + ) + ); } } \ No newline at end of file diff --git a/app/Controller/TestController.php b/app/Controller/TestController.php index eab2eb5..6920453 100644 --- a/app/Controller/TestController.php +++ b/app/Controller/TestController.php @@ -20,7 +20,7 @@ class TestController extends AbstractController // $this->test_3(); // $this->test_4(); // $this->test_6(); - $this->test_9(); +// $this->test_9(); } @@ -391,6 +391,12 @@ class TestController extends AbstractController public function test_9(){ $ca = new Ca(); $result = $ca->getSignedFile("491925054435950592","1638477783898161153"); + if (empty($result)){ + return fail(); + } + + // 上传阿里云oss + } } \ No newline at end of file diff --git a/extend/Ca/Ca.php b/extend/Ca/Ca.php index 02fb17a..2144951 100644 --- a/extend/Ca/Ca.php +++ b/extend/Ca/Ca.php @@ -265,18 +265,59 @@ class Ca */ public function getSignedFile(string $user_id, string $file_id): mixed { - $option = [ + $arg = [ 'form_params' => [ 'userId' => $user_id,// 用户标识信息(为云证书entityId) 'fileId' => $file_id,// 签章接口返回的文件标识 ] ]; + try { + $option = [ + "headers" => [ + "app_id" => config("ca.app_id"), + "signature" => $this->getRequestSign($arg), + ], + ]; + + $option = array_merge($arg, $option); + + $response = $this->client->post(config("ca.api_url") . '/signature-server/api/open/signature/fetchSignedFile', $option); + + if ($response->getStatusCode() != '200') { + // 请求失败 + throw new BusinessException($response->getBody()->getContents()); + } + + // 将 Stream 对象转换为字符串 + return (string)$response->getBody()->getContents(); + } catch (GuzzleException $e) { + throw new BusinessException($e->getMessage()); + } + } + + /** + * PDF签章文件验证 + * @param array $data + * @return mixed + */ + public function verifyPdf(array $data): mixed + { + $option = [ + 'multipart' => [ + [ + 'name' => 'pdfFile', + 'contents' => $data['pdf_file'],// 待签章PDF文件(字节流) + ], + ] + ]; + try { $response = $this->httpRequest( - config("ca.api_url") . '/signature-server/api/open/signature/fetchSignedFile', + config("ca.api_url") . '/signature-server/api/open/signature/verifyPdf', $option ); + if (empty($response)) { // 返回值为空 throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); @@ -287,7 +328,6 @@ class Ca } } - /** * 获取请求签名 * @param array $data @@ -311,11 +351,14 @@ class Ca } } - ksort($sign_data); - $data = implode('&', $sign_data); + if (!empty($sign_data)){ + ksort($sign_data); + $data = implode('&', $sign_data); + }else{ + $data = ""; + } - $data = hash_hmac("sha1", $data, config("ca.secret")); - return $data; + return hash_hmac("sha1", $data, config("ca.secret")); } /**