catchAdmin/extend/catcher/CatchResponse.php

64 lines
1.1 KiB
PHP
Raw Permalink Normal View History

2019-12-06 09:17:40 +08:00
<?php
2019-12-24 18:21:57 +08:00
2019-12-06 09:17:40 +08:00
namespace catcher;
2019-12-06 14:44:41 +08:00
use think\Paginator;
2019-12-12 18:54:07 +08:00
use think\Response;
2019-12-24 18:21:57 +08:00
use think\response\Json;
2019-12-06 14:44:41 +08:00
2019-12-06 09:17:40 +08:00
class CatchResponse
{
2019-12-24 18:21:57 +08:00
/**
* 成功的响应
*
* @time 2019年12月02日
* @param array $data
* @param $msg
* @param int $code
* @return Json
*/
public static function success($data = [], $msg = 'success', $code = Code::SUCCESS): Json
{
return json([
'code' => $code,
'message' => $msg,
'data' => $data,
]);
}
2019-12-06 14:44:41 +08:00
2019-12-24 18:21:57 +08:00
/**
* 分页
*
* @time 2019年12月06日
* @param Paginator $list
* @return
*/
public static function paginate(Paginator $list)
{
return json([
2020-01-19 15:49:40 +08:00
'code' => Code::SUCCESS,
'message' => 'success',
2019-12-24 18:21:57 +08:00
'count' => $list->total(),
'current' => $list->currentPage(),
'limit' => $list->listRows(),
'data' => $list->getCollection(),
]);
}
2019-12-12 18:54:07 +08:00
2019-12-24 18:21:57 +08:00
/**
* 错误的响应
*
* @time 2019年12月02日
* @param string $msg
* @param int $code
* @return Json
*/
2020-01-19 15:49:40 +08:00
public static function fail($msg = '', $code = Code::FAILED): Json
2019-12-24 18:21:57 +08:00
{
return json([
2020-01-19 15:49:40 +08:00
'code' => $code,
'message' => $msg,
2019-12-24 18:21:57 +08:00
]);
}
2019-12-06 09:17:40 +08:00
}