1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/gsang-laravel-vue-element-admin

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
ApiResponse.php 3.3 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
Gsangu Отправлено 6 лет назад 6023959
<?php
/**
* 响应体封装
*
* @author wujunzhong <1250044515@qq.com>
* @date 2018/1/12
*/
namespace App\Traits;
use Response;
use Symfony\Component\HttpFoundation\Response as FoundationResponse;
trait ApiResponse
{
/**
* @var int HTTP code
*/
protected $statusCode = FoundationResponse::HTTP_OK;
/**
* get the HTTP code
* @return mixed
*/
private function getStatusCode()
{
return $this->statusCode;
}
/**
* set the HTTP code
*
* @param $statusCode
* @return $this
*/
private function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
/**
* Repond a no content response.
*
* @param $data
* @param array $header
* @return mixed
*/
public function respond($data, $header = [])
{
return Response::json($data, $this->getStatusCode(), $header);
}
/**
* Repond a no content response.
*
* @return response
*/
public function noContent()
{
return Response::json(null, FoundationResponse::HTTP_NO_CONTENT);
}
/**
* Repond a Request format error!
*
* @param $message
* @param int $code
* @return mixed
*/
public function failed($message = 'Request format error!', $code = FoundationResponse::HTTP_BAD_REQUEST)
{
return $this->setStatusCode($code)->respond(['message' => $message]);
}
/**
* Repond a not found!
*
* @param string $message
* @return mixed
*/
public function notFond($message = 'not found!')
{
return $this->failed($message, Foundationresponse::HTTP_NOT_FOUND);
}
/**
* Repond a validation error!
*
* @param string $message
* @return mixed
*/
public function formError($error = [])
{
return $this->setStatusCode(422)->respond(['message' => "The given data was invalid.", 'error' => $error]);
}
/**
* Repond a Interface requests are too frequent!
*
* @param string $message
* @return mixed
*/
public function requestsMany($message = 'Interface requests are too frequent!')
{
return $this->failed($message, 429);
}
/**
* Respond the error of 'Unauthorized'.
*
* @param string $message
* @return json
*/
public function errorUnauthorized($message = 'Unauthorized')
{
return $this->failed($message, 401);
}
/**
* Respond No access
*
* @param string $message
* @return mixed
*/
public function notAccess($message = 'No access!')
{
return $this->failed($message, 403);
}
/**
* resond a network error!
*
* @param string $message
* @return mixed
*/
public function internalError($message = "network error!")
{
return $this->failed($message, FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
}
/**
* @param $data
* @param int $code
* @param array $header
* @return mixed
*/
public function success($data, $code = FoundationResponse::HTTP_CREATED, $header = [])
{
$data = is_string($data) ? ['message' => $data] : $data;
return $this->setStatusCode($code)->respond($data, $header);
}
}

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/gsang-laravel-vue-element-admin.git
git@gitlife.ru:oschina-mirror/gsang-laravel-vue-element-admin.git
oschina-mirror
gsang-laravel-vue-element-admin
gsang-laravel-vue-element-admin
master