From 8570b65a8cc4b6e0887e8d321b36bdb488f4370b Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Sat, 4 Jan 2025 15:12:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BF=AE=E6=94=B9Api=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 12 +++---- extend/.gitignore | 1 - extend/apiret/Api.php | 79 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 extend/apiret/Api.php diff --git a/app/common.php b/app/common.php index 05a14a9c..5bd19fa0 100644 --- a/app/common.php +++ b/app/common.php @@ -3,26 +3,26 @@ // 接口错误返回 if (!function_exists('error')) { - function error($msg = '', $data = []): \think\response\Json + function error($msg = '', $data = [], $status_code = 200): \think\response\Json { - return \apiret\Api::error($msg, $data); + return \apiret\Api::error($msg, $data, $status_code); } } // 接口成功返回 if (!function_exists('success')) { - function success($msg = '', $data = []): \think\response\Json + function success($msg = '', $data = [], $status_code = 200): \think\response\Json { - return \apiret\Api::success($msg, $data); + return \apiret\Api::success($msg, $data, $status_code); } } // 接口调结果返回 if (!function_exists('result')) { - function result($errno, $msg = '', $data = []): \think\response\Json + function result($errno, $msg = '', $data = [], $status_code = 200): \think\response\Json { - return \apiret\Api::result($errno)->message($msg)->response($data); + return \apiret\Api::result($errno)->message($msg)->response($data, $status_code); } } diff --git a/extend/.gitignore b/extend/.gitignore index c96a04f0..b722e9e1 100644 --- a/extend/.gitignore +++ b/extend/.gitignore @@ -1,2 +1 @@ -* !.gitignore \ No newline at end of file diff --git a/extend/apiret/Api.php b/extend/apiret/Api.php new file mode 100644 index 00000000..1a04d257 --- /dev/null +++ b/extend/apiret/Api.php @@ -0,0 +1,79 @@ + 'status', + 'message_var' => 'message', + 'data_var' => 'data', + 'states' => [ + 'success' => [ + 'status' => 200, + 'message' => '操作成功!', + 'data' => [] + ], + 'error' => [ + 'status' => 2001, + 'message' => '操作错误!', + 'data' => [] + ], + ] + ]; + + private function __construct($errno) + { + isset($errno) || die('请确认返回类型!'); + $this->config = config('apiret'); + $this->state = $this->config['states'][$errno]; + return $this; + } + + // 单例 + public static function result($errno): Api + { + if (!self::$response) { + self::$response = new self($errno); + } + return self::$response; + } + + // 写入返回信息 + public function message($message=''): Api + { + $message ? $this->state[$this->config['message_var']] = $message : ''; + return $this; + } + + // 返回错误 + public static function error($msg='', $data=[], $status_code = 200): \think\response\Json + { + $ins = self::result('error'); + if ($msg) { + $ins = $ins->message($msg); + } + + return $ins->response($data, $status_code); + } + + // 返回成功 + public static function success($msg='', $data=[], $status_code = 200): \think\response\Json + { + $ins = self::result('success'); + if ($msg) { + $ins = $ins->message($msg); + } + + return $ins->response($data, $status_code); + } + + // 返回结果 + public function response($data=[], $status_code = 200): \think\response\Json + { + $this->state[$this->config['data_var']] = $data; + return \think\Response::create($this->state, 'json', $status_code); + } +}