first commit
This commit is contained in:
131
app/Http/Helpers/ApiResponse.php
Normal file
131
app/Http/Helpers/ApiResponse.php
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Helpers;
|
||||
|
||||
use Response;
|
||||
|
||||
trait ApiResponse
|
||||
{
|
||||
/**
|
||||
* 默认成功的状态码
|
||||
* @var [type]
|
||||
*/
|
||||
protected $statusCode = 0;
|
||||
|
||||
/**
|
||||
* [getStatusCode description]
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-05-22
|
||||
* @return [type] [description]
|
||||
*/
|
||||
protected function getStatusCode()
|
||||
{
|
||||
return $this->statusCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* [setStatusCode description]
|
||||
* @Author:<C.Jason>
|
||||
* @Date:2018-05-22
|
||||
* @param [type] $statusCode [description]
|
||||
*/
|
||||
protected 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 = true, $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 = 4000, $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, 4004);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, 5000);
|
||||
}
|
||||
|
||||
private function status($status, array $data, $code = null)
|
||||
{
|
||||
if ($code) {
|
||||
$this->setStatusCode($code);
|
||||
}
|
||||
|
||||
$status = [
|
||||
'status' => $status,
|
||||
'error_code' => $this->statusCode,
|
||||
];
|
||||
if ($this->statusCode == 200) {
|
||||
$data = $data->additional([
|
||||
'status' => $status,
|
||||
'error_code' => $this->statusCode,
|
||||
]);
|
||||
} else {
|
||||
$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, 200, $header);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user