<?php
namespace App\common\response;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Utils\ApplicationContext;
use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
class RespResult
{
public static function success(){
$data = json_encode(["code"=>SystemCode::SYSTEM_SUCCESS,"message"=>SystemMessage::SYSTEM_SUCCESS,"data"=>[]],JSON_UNESCAPED_UNICODE);
return self::response($data);
}
public static function error(){
$data = json_encode(["code"=>SystemCode::SYSTEM_ERROR,"message"=>SystemMessage::SYSTEM_ERROR,"data"=>[]],JSON_UNESCAPED_UNICODE);
return self::response($data);
}
public static function result(string $code,string $message,array $data = []){
$data = json_encode(["code"=>$code,"message"=>$message,"data"=>$data],JSON_UNESCAPED_UNICODE);
return self::response($data);
}
private static function response($data){
$response = self::container()->get(ResponseInterface::class);
return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
}
private static function container()
{
return ApplicationContext::getContainer();
}
}