2023-12-04 20:40:49 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Helpers;
|
|
|
|
|
|
|
|
|
|
use App\Helpers\ResponseEnum;
|
|
|
|
|
use App\Exceptions\BusinessException;
|
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
|
|
trait ApiResponse
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 成功
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @param array $codeResponse
|
|
|
|
|
* @return JsonResponse
|
|
|
|
|
*/
|
2025-07-14 00:33:04 +08:00
|
|
|
public function success($data = null, $codeResponse = ResponseEnum::HTTP_OK): JsonResponse
|
2023-12-04 20:40:49 +08:00
|
|
|
{
|
|
|
|
|
return $this->jsonResponse('success', $codeResponse, $data, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 失败
|
|
|
|
|
* @param array $codeResponse
|
|
|
|
|
* @param mixed $data
|
|
|
|
|
* @param mixed $error
|
|
|
|
|
* @return JsonResponse
|
|
|
|
|
*/
|
2025-07-14 00:33:04 +08:00
|
|
|
public function fail($codeResponse = ResponseEnum::HTTP_ERROR, $data = null, $error = null): JsonResponse
|
2023-12-04 20:40:49 +08:00
|
|
|
{
|
|
|
|
|
return $this->jsonResponse('fail', $codeResponse, $data, $error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* json响应
|
|
|
|
|
* @param $status
|
|
|
|
|
* @param $codeResponse
|
|
|
|
|
* @param $data
|
|
|
|
|
* @param $error
|
|
|
|
|
* @return JsonResponse
|
|
|
|
|
*/
|
|
|
|
|
private function jsonResponse($status, $codeResponse, $data, $error): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
list($code, $message) = $codeResponse;
|
|
|
|
|
return response()
|
|
|
|
|
->json([
|
2025-07-14 00:33:04 +08:00
|
|
|
'status' => $status,
|
|
|
|
|
// 'code' => $code,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'data' => $data ?? null,
|
|
|
|
|
'error' => $error,
|
|
|
|
|
], (int) substr(((string) $code), 0, 3));
|
2023-12-04 20:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-07-14 00:33:04 +08:00
|
|
|
public function paginate(LengthAwarePaginator $page)
|
2023-12-04 20:40:49 +08:00
|
|
|
{
|
2025-07-14 00:33:04 +08:00
|
|
|
return response()->json([
|
|
|
|
|
'total' => $page->total(),
|
|
|
|
|
'current_page' => $page->currentPage(),
|
|
|
|
|
'per_page' => $page->perPage(),
|
|
|
|
|
'last_page' => $page->lastPage(),
|
|
|
|
|
'data' => $page->items()
|
|
|
|
|
]);
|
2023-12-04 20:40:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 业务异常返回
|
|
|
|
|
* @param array $codeResponse
|
|
|
|
|
* @param string $info
|
|
|
|
|
* @throws BusinessException
|
|
|
|
|
*/
|
2025-07-14 00:33:04 +08:00
|
|
|
public function throwBusinessException(array $codeResponse = ResponseEnum::HTTP_ERROR, string $info = '')
|
2023-12-04 20:40:49 +08:00
|
|
|
{
|
|
|
|
|
throw new BusinessException($codeResponse, $info);
|
|
|
|
|
}
|
|
|
|
|
}
|