## 请求成功
1.继承了BaseController请求结果返回请使用如下方法
~~~
return $this->responseSuccess();
~~~
2.没有继承的BaseController请自定义方法返回
例:responseSuccess方法
~~~
/**
* Note:请求成功返回方式
* Think:
* User: HuYang-TSJ
* Date: 2019/1/3 14:48
* @param string $list 结果
* @param string $count 数量
* @return \think\response\Json
*/
function responseSuccess($list = '', $count = '')
{
$result['backCode'] = 1;
$result['msg'] = '请求成功';
if (!empty($list)) {
if (!empty($count) && is_int($count) && $count > 0) {
$data['list'] = $list;
$data['page'] = $this->getPage();
$data['page_size'] = $this->getPageSize();
$data['count'] = $count;
} else {
$data = $list;
}
$result['data'] = $data;
}
return json($result);
}
~~~
> 所有的返回的json格式,必须按照如下格式进行统一返回,**后续作为代码审查核心点**
> backCode,msg,data
> 后续有特殊格式待补充...
3. 带分页固定格式请求请传递一下参数(page,page_size,count),**如需获取所有数据,page_size的值请设置为-1**
* * *
* 分页数据返回
~~~
{
"backCode": "1",
"msg":"请求成功",
"data": {
"count": 100,
"page_size": 10,
"page": 1,
"list": [
{
"name": "张三",
"age": 20
},
{
"name": "李四",
"age": 22
}
]
}
}
~~~
* 多条数据返回
* * *
~~~
{
"backCode": "1",
"msg":"请求成功",
"data": [
{
"name": "张三",
"age": 20
},
{
"name": "李四",
"age": 22
}
]
}
~~~
* * *
* 单条数据请求:常用的用户对象返回
~~~
{
"backCode": "1",
"msg":"请求成功",
"data": {
"user_name": "张三",
"age": 10
}
}
~~~
* 成功无返回数据
~~~
{
"backCode": "1",
"msg":"请求成功",
}
~~~
## 请求失败
> 所有的返回的json格式,必须按照如下格式进行统一返回,**后续作为代码审查核心点**
> errorCode,msg,request_url
* 错误返回
~~~
{
"errorCode": "100 ",
"msg":"请求参数必须为正整数",
"request_url":"user/getUser"
}
~~~