From 25687e8b2c8fb914ef4aa17d5d452ba9f5a6de59 Mon Sep 17 00:00:00 2001 From: JING Date: Mon, 15 Jun 2026 12:35:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BA=86post=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/factory/impl/OneLevelFactory.php | 4 +-- src/factory/object/RequestObj.php | 2 +- src/factory/object/onelevel/UploadRequest.php | 31 ++++++++++++++++++- src/http/Factory.php | 4 +++ src/http/IHttpClient.php | 3 ++ src/http/Response.php | 1 - src/http/impl/HFPayHttpClient.php | 18 +++++++++++ 7 files changed, 58 insertions(+), 5 deletions(-) diff --git a/src/factory/impl/OneLevelFactory.php b/src/factory/impl/OneLevelFactory.php index 816d79b..e46c124 100644 --- a/src/factory/impl/OneLevelFactory.php +++ b/src/factory/impl/OneLevelFactory.php @@ -56,11 +56,11 @@ class OneLevelFactory extends BaseFactory { $request = $params->toParams(); - $data = $this->post('/api/alseFile/file01', $request); - + $data = $this->uploadPost('/api/alseFile/file01', $request); if(!$data->isSuccess()) { throw new BizException('上传失败', $data->getCode()); } + return UploadResponse::make($data->getBody()); } /** diff --git a/src/factory/object/RequestObj.php b/src/factory/object/RequestObj.php index 6497a8b..32423ff 100644 --- a/src/factory/object/RequestObj.php +++ b/src/factory/object/RequestObj.php @@ -4,7 +4,7 @@ namespace Tansilu\HfPayLib\factory\object; trait RequestObj { - private array $data = []; + private array $data =[]; public function __set($name, $value) { diff --git a/src/factory/object/onelevel/UploadRequest.php b/src/factory/object/onelevel/UploadRequest.php index 89e4949..3a82480 100644 --- a/src/factory/object/onelevel/UploadRequest.php +++ b/src/factory/object/onelevel/UploadRequest.php @@ -67,7 +67,36 @@ class UploadRequest throw new ParamsException('上传类型不能为空', 'handle_type'); } + // 构建 multipart 数据结构 + $multipartData = [ + [ + 'name' => 'attach_file', // 这里的 'file' 是接口定义的文件字段名 + 'contents' => fopen($this->data['attach_file'], 'r'), // 使用 fopen 打开文件流 + ], + // 可以同时上传其他文本字段 + [ + 'name' => 'attach_no', + 'contents' => $this->data['attach_no'], + ], + [ + 'name' => 'trans_type', + 'contents' => $this->data['trans_type'], + ], + [ + 'name' => 'attach_type', + 'contents' => $this->data['attach_type'], + ], + [ + 'name' => 'attach_desc', + 'contents' => $this->data['attach_desc'], + ], + [ + 'name' => 'handle_type', + 'contents' => $this->data['handle_type'], + ], + ]; - return $this->data; + + return $multipartData; } } \ No newline at end of file diff --git a/src/http/Factory.php b/src/http/Factory.php index f91b99b..c9ba83f 100644 --- a/src/http/Factory.php +++ b/src/http/Factory.php @@ -44,4 +44,8 @@ class Factory { return $this->client->postJson($url, $data); } + public function uploadPost(string $url, array $data = []): Response + { + return $this->client->uploadPost($url, $data); + } } \ No newline at end of file diff --git a/src/http/IHttpClient.php b/src/http/IHttpClient.php index 5dee64e..5d2a0b2 100644 --- a/src/http/IHttpClient.php +++ b/src/http/IHttpClient.php @@ -18,4 +18,7 @@ interface IHttpClient function postJson(string $uri, ?array $data = []): Response; + function uploadPost(string $uri, ?array $data = []): Response; + + } \ No newline at end of file diff --git a/src/http/Response.php b/src/http/Response.php index 8c5555d..a420d86 100644 --- a/src/http/Response.php +++ b/src/http/Response.php @@ -30,7 +30,6 @@ class Response $contentType = $response->getHeader('Content-Type'); if(empty($contentType)) { //throw new ApiException('反馈的消息未指明内容格式Content-Type'); - //如果没有默认为这个 $contentType[0] = 'application/json; charset=utf-8'; } $bodyFormat = explode(';', $contentType[0]); diff --git a/src/http/impl/HFPayHttpClient.php b/src/http/impl/HFPayHttpClient.php index fc6c3ff..8286b23 100644 --- a/src/http/impl/HFPayHttpClient.php +++ b/src/http/impl/HFPayHttpClient.php @@ -166,6 +166,10 @@ class HFPayHttpClient implements IHttpClient case 'body': $options['body'] = array_filter($body, fn($v) => $v !== null); break; + // 新增的分支 + case 'multipart': + $options['multipart'] = array_filter($body, fn($v) => $v !== null); + break; } try { @@ -273,4 +277,18 @@ class HFPayHttpClient implements IHttpClient { return $this->request('POST', $uri, [], $data, 'json', $needToken); } + /** + * 上传文件 + * @param string $uri + * @param array|null $data + * @return Response + * @throws ApiException + * @throws BizException + * @throws SignException + */ + function uploadPost(string $uri, ?array $data = []): Response + { + // 调用 request,并指定格式为 'multipart' + return $this->request('POST', $uri, [], $data, 'multipart'); + } } \ No newline at end of file