catchAdmin/extend/catcher/CatchResponse.php

69 lines
1.5 KiB
PHP
Raw Normal View History

2019-12-06 09:17:40 +08:00
<?php
namespace catcher;
2019-12-12 18:54:07 +08:00
use think\facade\View;
2019-12-06 14:44:41 +08:00
use think\Paginator;
2019-12-12 18:54:07 +08:00
use think\Response;
2019-12-06 14:44:41 +08:00
2019-12-06 09:17:40 +08:00
class CatchResponse
{
/**
* 成功的响应
*
* @time 2019年12月02日
* @param array $data
* @param $msg
* @param int $code
* @return \think\response\Json
*/
2019-12-06 14:44:41 +08:00
public static function success($data = [], $msg = 'success', $code = 10000): \think\response\Json
2019-12-06 09:17:40 +08:00
{
2019-12-12 18:54:07 +08:00
if (request()->isAjax()) {
return json([
'code' => $code,
'msg' => $msg,
'data' => $data,
]);
}
2019-12-06 09:17:40 +08:00
}
2019-12-06 14:44:41 +08:00
/**
* 分页
*
* @time 2019年12月06日
* @param Paginator $list
* @return \think\response\Json
*/
public static function paginate(Paginator $list): \think\response\Json
{
return json([
'code' => 10000,
'msg' => 'success',
'count' => $list->total(),
'current' => $list->currentPage(),
'data' => $list->getCollection(),
]);
}
2019-12-06 09:17:40 +08:00
/**
* 错误的响应
*
* @time 2019年12月02日
* @param string $msg
* @param int $code
2019-12-12 18:54:07 +08:00
* @return mixed
* @throws \Exception
2019-12-06 09:17:40 +08:00
*/
2019-12-12 18:54:07 +08:00
public static function fail($msg = '', $code = 10001)
2019-12-06 09:17:40 +08:00
{
2019-12-12 18:54:07 +08:00
if (request()->isAjax()) {
return json([
'code' => $code,
'msg' => $msg,
]);
}
return Response::create(config('catch.error'), 'view', $code)->assign(['msg' => $msg]);
2019-12-06 09:17:40 +08:00
}
}