Files
lotteryLaravel/app/Support/ApiResponse.php

33 lines
865 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 Illuminate\Http\JsonResponse;
/**
* 对外 API 统一 JSON 结构:{ code, msg, data }。
*
* - code=0 表示成功;非 0 为业务码(见 docs/04-领域字典与编码规范.md
* - 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);
}
}