You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.9 KiB
79 lines
1.9 KiB
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
final class ApiResponseService
|
|
{
|
|
/**
|
|
* 成功响应
|
|
*
|
|
* @param mixed $data
|
|
* @param string $message
|
|
* @param int $statusCode
|
|
* @return JsonResponse
|
|
*/
|
|
public function success($data = null, string $message = '', int $statusCode = 200): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'error' => 0,
|
|
'body' => $data ?? (object)[],
|
|
'message' => $message,
|
|
], $statusCode);
|
|
}
|
|
|
|
/**
|
|
* 错误响应
|
|
*
|
|
* @param string $message
|
|
* @param int $errorCode
|
|
* @param int $statusCode
|
|
* @return JsonResponse
|
|
*/
|
|
public function error(string $message, int $errorCode = 500, int $statusCode = 200): JsonResponse
|
|
{
|
|
return response()->json([
|
|
'error' => $errorCode,
|
|
'body' => (object)[],
|
|
'message' => $message,
|
|
], $statusCode);
|
|
}
|
|
|
|
/**
|
|
* 系统错误响应
|
|
*
|
|
* @param string $message
|
|
* @param int $statusCode
|
|
* @return JsonResponse
|
|
*/
|
|
public function systemError(string $message = '系统错误', int $statusCode = 200): JsonResponse
|
|
{
|
|
return $this->error($message, 500, $statusCode);
|
|
}
|
|
|
|
/**
|
|
* 未授权响应
|
|
*
|
|
* @param string $message
|
|
* @param int $statusCode
|
|
* @return JsonResponse
|
|
*/
|
|
public function unauthorized(string $message = '请先登录', int $statusCode = 200): JsonResponse
|
|
{
|
|
return $this->error($message, 401, $statusCode);
|
|
}
|
|
|
|
/**
|
|
* 业务错误响应
|
|
*
|
|
* @param string $message
|
|
* @param int $errorCode
|
|
* @param int $statusCode
|
|
* @return JsonResponse
|
|
*/
|
|
public function businessError(string $message, int $errorCode = 400, int $statusCode = 200): JsonResponse
|
|
{
|
|
return $this->error($message, $errorCode, $statusCode);
|
|
}
|
|
}
|