This commit is contained in:
2026-07-21 16:40:38 +08:00
parent 9f8d6d629a
commit 9789a64ebb
4 changed files with 24 additions and 27 deletions

View File

@@ -1,4 +1,5 @@
<?php
namespace app\common\Trait;
use think\Response;
@@ -13,9 +14,9 @@ trait ApiResponse
* @param int $code 自定义业务状态码
* @return Json
*/
protected function success(int $code = 200, string $message = '操作成功', mixed $data = []): Json
protected function success(string $message = '操作成功', mixed $data = [], int $code = 200): Json
{
return $this->jsonResponse($code, $message, $data);
return $this->jsonResponse($message, $data, $code);
}
/**
@@ -25,15 +26,15 @@ trait ApiResponse
* @param mixed $data 额外的错误数据(如验证未通过的具体字段)
* @return Json
*/
protected function error(int $code = 400, string $message = '操作失败', mixed $data = []): Json
protected function error(string $message = '操作失败', mixed $data = [], int $code = 400): Json
{
return $this->jsonResponse($code, $message, $data);
return $this->jsonResponse($message, $data, $code);
}
/**
* 统一 JSON 返回格式
*/
private function jsonResponse(int $code, string $message, mixed $data): Json
private function jsonResponse(string $message, mixed $data, int $code): Json
{
$result = [
'code' => $code,