Render.php
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace app\common\service;
use app\common\exception\BaseException;
trait Render
{
/**
* 输出错误信息
* @param int $code
* @param $msg
* @throws BaseException
*/
protected function throwError($msg, $code = 0)
{
throw new BaseException(['code' => $code, 'msg' => $msg]);
}
/**
* 返回封装后的 API 数据
* @param int $code
* @param string $msg
* @param array $data
* @return array
*/
protected function renderJson($code = 0, $msg = '', $data = [])
{
return compact('code', 'msg', 'data');
}
/**
* 返回成功json
* @param array $data
* @param string|array $msg
* @return array
*/
protected function renderSuccess($data = [], $msg = 'success', $code = 0)
{
return $this->renderJson($code, $msg, $data);
}
/**
* 返回操作失败json
* @param string $msg
* @param array $data
* @return array
*/
protected function renderError($msg = 'error', $code = 1, $data = [])
{
return $this->renderJson($code, $msg, $data);
}
}