Files
lotteryLaravel/app/Support/ApiResponse.php

34 lines
883 B
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Support;
use App\Lottery\ErrorCode;
use Illuminate\Http\JsonResponse;
/**
* 对外 API 统一 JSON 结构:{ code, msg, data }。
*
* - `code=0` 即 {@see ErrorCode::Success};非 0 见 `ErrorCode` 与 docs/04 §10。
* - error() 的 HTTP 状态可与 code 独立(如鉴权失败 401 + code 8001
*/
final class ApiResponse
{
public static function success(mixed $data = null, string $msg = 'ok', int $code = 0): JsonResponse
{
return response()->json([
'code' => $code,
'msg' => $msg,
'data' => $data,
]);
}
public static function error(string $msg, int $code, mixed $data = null, int $httpStatus = 400): JsonResponse
{
return response()->json([
'code' => $code,
'msg' => $msg,
'data' => $data,
], $httpStatus);
}
}