You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
think-library/src/helper/Returns.php

88 lines
2.7 KiB

2 years ago
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
declare (strict_types=1);
namespace DtApp\ThinkLibrary\helper;
use think\exception\HttpResponseException;
/**
* 返回管理类
* @mixin Returns
* @package DtApp\ThinkLibrary\helper
*/
class Returns
{
public function __construct()
{
date_default_timezone_set('Asia/Shanghai');
header('Content-Type:application/json; charset=utf-8');
}
/**
* 返回Json-成功
* @param array $data 数据
* @param string $msg 描述
* @param int $code 状态
* @param array $ext 扩展字段
*/
public function jsonSuccess(array $data = [], string $msg = 'success', int $code = 0, array $ext = [])
{
if (!empty($ext) && is_array($ext)) {
throw new HttpResponseException(json(array_merge([
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data
], $ext)));
}
throw new HttpResponseException(json([
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data
]));
}
/**
* 返回Json-错误
* @param string $msg 描述
* @param int $code 状态码
* @param array $data 数据
* @param array $ext 扩展字段
*/
public function jsonError(string $msg = 'error', int $code = 1, array $data = [], array $ext = []): void
{
if (!empty($ext) && is_array($ext)) {
throw new HttpResponseException(json(array_merge([
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data
], $ext)));
}
throw new HttpResponseException(json([
'code' => $code,
'msg' => $msg,
'time' => time(),
'data' => $data
]));
}
}