129 lines
3.2 KiB
PHP
129 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Api\Helpers;
|
|
|
|
use Response;
|
|
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
|
|
|
|
trait ApiResponse
|
|
{
|
|
/**
|
|
* [$statusCode description]
|
|
* @var [type]
|
|
*/
|
|
protected $statusCode = FoundationResponse::HTTP_OK;
|
|
|
|
/**
|
|
* [getStatusCode description]
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @return [type] [description]
|
|
*/
|
|
public function getStatusCode()
|
|
{
|
|
return $this->statusCode;
|
|
}
|
|
|
|
/**
|
|
* [setStatusCode description]
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @param [type] $statusCode [description]
|
|
*/
|
|
public function setStatusCode($statusCode)
|
|
{
|
|
$this->statusCode = $statusCode;
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* 成功的返回
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @param [type] $data [description]
|
|
* @param string $status [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function success($data = [], $status = "SUCCESS")
|
|
{
|
|
return $this->status($status, compact('data'));
|
|
}
|
|
|
|
/**
|
|
* 200 返回消息
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @param [type] $message [description]
|
|
* @param string $status [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function message($message = '', $status = "SUCCESS")
|
|
{
|
|
return $this->status($status, [
|
|
'message' => $message,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 400 失败
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @param [type] $message [description]
|
|
* @param [type] $code [description]
|
|
* @param string $status [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function failed($message, $code = FoundationResponse::HTTP_BAD_REQUEST, $status = 'ERROR')
|
|
{
|
|
return $this->setStatusCode($code)->message($message, $status);
|
|
}
|
|
|
|
/**
|
|
* 404 错误
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @param string $message [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function notFond($message = 'Not Fond!')
|
|
{
|
|
return $this->failed($message, Foundationresponse::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
/**
|
|
* 500 错误
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-05-22
|
|
* @param string $message [description]
|
|
* @return [type] [description]
|
|
*/
|
|
public function internalError($message = "Internal Error!")
|
|
{
|
|
return $this->failed($message, FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
private function status($status, array $data, $code = null)
|
|
{
|
|
if ($code) {
|
|
$this->setStatusCode($code);
|
|
}
|
|
|
|
$status = [
|
|
'status' => $status,
|
|
'status_code' => $this->statusCode,
|
|
];
|
|
|
|
$data = array_merge($status, $data);
|
|
return $this->respond($data);
|
|
}
|
|
|
|
private function respond($data, $header = [])
|
|
{
|
|
$rt = microtime(true) - LARAVEL_START;
|
|
|
|
$header = array_merge($header, ['rt' => round($rt * 1000, 2) . 'ms', 'qps' => round(1 / $rt, 1)]);
|
|
// return Response::json($data, $this->getStatusCode(), $header);
|
|
return Response::json($data, 200, $header);
|
|
}
|
|
}
|