求大佬看看 API 项目中这样自定义返回数据结构好不好
1.Responser.php文件中
<?php
namespace App\Utils;
/**
* 自定义返回json
* Class Responser
* @package App\Utils
*/
class Responser
{
private $code = '200';
private $status = '1';
private $data = [];
/**
* 响应成功消息
*
* @param array $data
* @param string $code
* @return array
*/
public function success($data = [], $code = '200')
{
$this->data = $data;
$this->code = $code;
$this->status = '1';
return $this->response($this->process());
}
/**
* 响应错误消息
*
* @param string $code 错误码
* @param array $data 错误时需要返回的数据
* @return \Illuminate\Http\JsonResponse
*/
public function error($code = '414', $data = [])
{
$this->data = $data;
$this->code = $code;
$this->status = '0';
return $this->response($this->process());
}
/**
* 处理为需要的数据格式
* @access public
* @return array
*/
private function rule()
{
return [
'status' => $this->status,
'code' => self::getMessage($this->code) ? $this->code : '414',
'desc' => self::getMessage($this->code) ? self::getMessage($this->code) : $this->code,
'data' => $this->data,
];
}
/**
* 处理为json数据
* @access public
* @return array
*/
private function process()
{
return json_encode($this->rule(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
}
/**
* 返回数据
* @access public
* @param string $json
* @return array
*/
private function response(string $json)
{
return response($json)->header('Content-Type', 'application/json')->header('Cache-Control', 'no-cache, must-revalidate');
}
/**
* 获取错误码
*
**/
private function getCodes()
{
$code_raw = file_get_contents(__DIR__."/json/code.json");
return json_decode($code_raw, JSON_FORCE_OBJECT|JSON_UNESCAPED_UNICODE);
}
/**
* 获取错误信息
* @param string $code
* @return bool
*/
private function getMessage(string $code)
{
$json = $this->getCodes();
if (isset($json[$code]))
{
return $json[$code];
} else {
return false;
}
}
}
2.code.json文件
{
"200": "请求成功",
"202": "请求成功,但数据未发生变化",
"400": "发出的请求有错误",
"401": "用户没有权限(令牌、用户名、密码错误)。",
"403": "token失效",
"404": "发出的请求针对的是不存在的记录,服务器没有进行操作。",
"405": "请求方式错误,服务器不允许该操作。",
"414": "未知错误,请重新发送请求",
"500": "服务器发生错误,请检查服务器。",
"501": "系统内部异常,请检查模块代码是否出现问题。",
"502": "Redis服务器发生错误",
"503": "请求非法",
}
3.在controller.php中
<?php
namespace App\Http\Controllers;
use App\Utils\Responser;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* 成功返回数据
* @access public
* @param array $data
* @param string $code
* @return array
*/
public function success($data = [], $code = '200')
{
$responser = new Responser();
return $responser->success($data, $code);
}
/**
* 失败返回数据
* @access public
* @param string $code
* @param array $data
* @return \Illuminate\Http\JsonResponse
*/
public function error($code = '414', $data = [])
{
$responser = new Responser();
return $responser->error($code, $data);
}
}
挺好的啊,实现了自定义了错误码和错误消息返回。异常响应是不是没有考虑呢,错误码和对象描述信息是不是可以考虑放
resources/lang/zh-CN/response.php
中,通过__("response.{$code}"')
来取会好点?另外,我也在整理相关的 API 响应结构,是时候使用 Lumen 7 + API Resource 开发项目了!,进来讨论下?
挺不错的
trait
的类。