Files
mini_tp/app/api/common/Response.php
T
nepiedg 166940d5a6 Initial commit: ThinkPHP refactor (tp)
Made-with: Cursor
2026-04-02 02:13:12 +00:00

69 lines
1.7 KiB
PHP

<?php
declare (strict_types = 1);
namespace app\api\common;
/**
* 统一响应类
*/
class Response
{
/**
* 成功响应
* @param mixed $data 返回数据
* @param string $message 提示信息
* @param int $code 状态码
* @return \think\response\Json
*/
public static function success($data = [], string $message = 'success', int $code = 200)
{
return json([
'code' => $code,
'msg' => $message,
'data' => $data,
'time' => time(),
]);
}
/**
* 失败响应
* @param string $message 提示信息
* @param int $code 状态码
* @param mixed $data 返回数据
* @return \think\response\Json
*/
public static function error(string $message = 'error', int $code = 400, $data = [])
{
return json([
'code' => $code,
'msg' => $message,
'data' => $data,
'time' => time(),
]);
}
/**
* 分页数据响应
* @param mixed $list 数据列表
* @param int $total 总数
* @param int $page 当前页
* @param int $pageSize 每页数量
* @param string $message 提示信息
* @return \think\response\Json
*/
public static function paginate($list, int $total, int $page, int $pageSize, string $message = 'success')
{
return json([
'code' => 200,
'msg' => $message,
'data' => [
'list' => $list,
'total' => $total,
'page' => $page,
'page_size' => $pageSize,
],
'time' => time(),
]);
}
}