workerman/app/result/Result.php

58 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2024-04-20 14:59:53 +08:00
<?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);
}
}