新增了post上传文件的判断

develop
JING 1 hour ago
parent 6abef5214e
commit 25687e8b2c
  1. 4
      src/factory/impl/OneLevelFactory.php
  2. 2
      src/factory/object/RequestObj.php
  3. 31
      src/factory/object/onelevel/UploadRequest.php
  4. 4
      src/http/Factory.php
  5. 3
      src/http/IHttpClient.php
  6. 1
      src/http/Response.php
  7. 18
      src/http/impl/HFPayHttpClient.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());
}
/**

@ -4,7 +4,7 @@ namespace Tansilu\HfPayLib\factory\object;
trait RequestObj
{
private array $data = [];
private array $data =[];
public function __set($name, $value)
{

@ -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;
}
}

@ -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);
}
}

@ -18,4 +18,7 @@ interface IHttpClient
function postJson(string $uri, ?array $data = []): Response;
function uploadPost(string $uri, ?array $data = []): Response;
}

@ -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]);

@ -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');
}
}
Loading…
Cancel
Save