Files
douba_shop/crmeb/services/wechat/SwooleResponse.php
Karlcarlo c9a98f342c chore: 添加静态资源文件和版本配置文件
添加大量图片、字体、音频等静态资源文件,包括用户界面图片、模板文件和安装界面素材
添加系统版本配置文件(.version)和常量定义文件(.constant)
更新供应商依赖文件,包括Symfony VarDumper组件和相关许可证文件
添加验证码配置文件(captcha.php)和.gitignore规则
2026-03-06 20:14:23 +08:00

164 lines
4.1 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* +----------------------------------------------------------------------
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
* +----------------------------------------------------------------------
* | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
* +----------------------------------------------------------------------
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
* +----------------------------------------------------------------------
* | Author: CRMEB Team <admin@crmeb.com>
* +----------------------------------------------------------------------
*/
namespace crmeb\services\wechat;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\RedirectionException;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Swoole\Coroutine\Http\Client;
/**
* 携程请求返回资源
* Class SwooleResponse
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
* @package crmeb\services\wechat
*/
class SwooleResponse implements ResponseInterface
{
/**
* @var Client
*/
private $swooleResponse;
/**
* SwooleResponse constructor.
* @param Client $swooleResponse
*/
public function __construct(Client $swooleResponse)
{
$this->swooleResponse = $swooleResponse;
}
/**
* @param bool $throw
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function getContent(bool $throw = true): string
{
if ($throw) {
$this->checkStatusCode();
}
return $this->swooleResponse->getBody();
}
/**
* @return int
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function getStatusCode(): int
{
return $this->swooleResponse->getStatusCode();
}
/**
* @param bool $throw
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function getHeaders(bool $throw = true): array
{
if ($throw) {
$this->checkStatusCode();
}
$headers = [];
foreach ($this->swooleResponse->getHeaders() as $name => $value) {
$headers[strtolower($name)][] = $value;
}
return $headers;
}
/**
* @param bool $throw
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function toArray(bool $throw = true): array
{
$content = $this->getContent($throw);
if ($content === '' && $throw) {
throw new \InvalidArgumentException('The content is not valid JSON.');
}
return json_decode($content, true);
}
/**
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function cancel(): void
{
$this->swooleResponse->close();
}
/**
* @param string|null $type
* @return mixed
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/24
*/
public function getInfo(string $type = null): mixed
{
if ($type === null || $type === 'http_code') {
return $this->getStatusCode();
}
$httpResponse = [
'http_code' => $this->getStatusCode(),
'response_headers' => $this->swooleResponse->getHeaders(),
'url' => $this->swooleResponse->host,
];
return $type ? $httpResponse[$type] ?? null : $httpResponse;
}
/**
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
private function checkStatusCode()
{
$code = $this->getInfo('http_code');
if (500 <= $code) {
throw new ServerException($this);
}
if (400 <= $code) {
throw new ClientException($this);
}
if (300 <= $code) {
throw new RedirectionException($this);
}
}
}