Passed
Push — v6 ( aac15a...ba732b )
by 光春
04:36
created

Returns::jsonSuccess()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 16
rs 9.8666
cc 3
nc 2
nop 4
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
declare (strict_types=1);
21
22
namespace DtApp\ThinkLibrary\helper;
23
24
use think\exception\HttpResponseException;
25
26
/**
27
 * 返回管理类
28
 * @mixin Returns
29
 * @package DtApp\ThinkLibrary\helper
30
 */
31
class Returns
32
{
33
    public function __construct()
34
    {
35
        date_default_timezone_set('Asia/Shanghai');
36
        header('Content-Type:application/json; charset=utf-8');
37
    }
38
39
    /**
40
     * 返回Json-成功
41
     * @param array $data 数据
42
     * @param string $msg 描述
43
     * @param int $code 状态
44
     * @param array $ext 扩展字段
45
     */
46
    public function jsonSuccess(array $data = [], string $msg = 'success', int $code = 0, array $ext = [])
47
    {
48
        if (!empty($ext) && is_array($ext)) {
49
            throw new HttpResponseException(json(array_merge([
50
                'code' => $code,
51
                'msg' => $msg,
52
                'time' => time(),
53
                'data' => $data
54
            ], $ext)));
55
        }
56
57
        throw new HttpResponseException(json([
58
            'code' => $code,
59
            'msg' => $msg,
60
            'time' => time(),
61
            'data' => $data
62
        ]));
63
    }
64
65
    /**
66
     * 返回Json-错误
67
     * @param string $msg 描述
68
     * @param int $code 状态码
69
     * @param array $data 数据
70
     * @param array $ext 扩展字段
71
     */
72
    public function jsonError(string $msg = 'error', int $code = 1, array $data = [], array $ext = [])
73
    {
74
        if (!empty($ext) && is_array($ext)) {
75
            throw new HttpResponseException(json(array_merge([
76
                'code' => $code,
77
                'msg' => $msg,
78
                'time' => time(),
79
                'data' => $data
80
            ], $ext)));
81
        }
82
83
        throw new HttpResponseException(json([
84
            'code' => $code,
85
            'msg' => $msg,
86
            'time' => time(),
87
            'data' => $data
88
        ]));
89
    }
90
}
91