58 lines
1.2 KiB
PHP
58 lines
1.2 KiB
PHP
<?php
|
||
/*
|
||
统一格式的返回json数据
|
||
*/
|
||
|
||
namespace app\result;
|
||
|
||
class Result
|
||
{
|
||
//success:code值为0,data:数据
|
||
static public function Success($data)
|
||
{
|
||
$rs = [
|
||
'code' => 0,
|
||
'msg' => "",
|
||
'data' => $data,
|
||
];
|
||
return json($rs);
|
||
}
|
||
//ErrorCode:需要code/msg参数
|
||
static public function ErrorCode($code, $msg)
|
||
{
|
||
$rs = [
|
||
'code' => $code,
|
||
'msg' => $msg,
|
||
'data' => "",
|
||
];
|
||
return json($rs);
|
||
}
|
||
//error,传入定义的数组常量
|
||
static public function Error($arr)
|
||
{
|
||
$rs = [
|
||
'code' => $arr['code'],
|
||
'msg' => $arr['msg'],
|
||
'data' => "",
|
||
];
|
||
return json($rs);
|
||
}
|
||
/**
|
||
* 通用化API数据格式输出
|
||
* @param $status
|
||
* @param string $message
|
||
* @param array $data
|
||
* @param int $httpStatus
|
||
* @return \think\response\Json
|
||
*/
|
||
static public function show($status, $message = 'error', $data = [], $httpStatus = 200)
|
||
{
|
||
$result = [
|
||
"status" => $status,
|
||
"message" => $message,
|
||
"result" => $data
|
||
];
|
||
return json($result, $httpStatus);
|
||
}
|
||
}
|