123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- declare(strict_types=1);
- namespace App\Utils;
- use App\Utils\Control\ActionUtil;
- use Hyperf\Context\ApplicationContext;
- use Hyperf\HttpMessage\Stream\SwooleStream;
- use Hyperf\HttpServer\Contract\ResponseInterface;
- use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
- /**
- * Author:Panda
- * Email:joeyoung0314@qq.com
- * Class AppResult
- * @package App\Utils
- */
- class AppResult
- {
- /**==============应用返回封装==============**/
- /**
- * 成功返回200
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response200(string $message = 'success', $result = null)
- {
- return self::response(json_encode([
- 'code' => 200,
- 'message' => $message,
- 'result' => $result,
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 失败返回201
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response201(string $message = 'error', $result = null)
- {
- return self::response(json_encode([
- 'code' => 201,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 失败返回202
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response202(string $message = 'error', $result = null)
- {
- return self::response(json_encode([
- 'code' => 202,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 参数缺失返回203
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response203(string $message = 'error', $result = null)
- {
- return self::response(json_encode([
- 'code' => 203,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 授权无用户返回204
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response204(string $message = '暂无此用户,请先授权手机号,并登录', $result = null)
- {
- return self::response(json_encode([
- 'code' => 204,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 请求次数过多返回206
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response206(string $message = '请求次数过多', $result = null)
- {
- return self::response(json_encode([
- 'code' => 206,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 身份信息已过期401
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response400(string $message = 'Identity is overdue', $result = null)
- {
- return self::response(json_encode([
- 'code' => 400,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 致命错误500
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response500(string $message = 'Fatal error', $result = null)
- {
- return self::response(json_encode([
- 'code' => 500,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 权限未拥有777
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response777(string $message = 'No Access', $result = null)
- {
- return self::response(json_encode([
- 'code' => 777,
- 'message' => $message,
- 'result' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 返回统一格式(兼容 fastadmin api)
- * @param string $message
- * @param $result
- * @return Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function success(string $message = 'success', $result = null)
- {
- return self::response(json_encode([
- 'code' => 1,
- 'msg' => $message,
- 'data' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 返回统一格式(兼容 fastadmin api)
- * @param string $message
- * @param $result
- * @return Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function error(string $message = 'error', $result = null)
- {
- return self::response(json_encode([
- 'code' => 0,
- 'msg' => $message,
- 'data' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 返回统一格式(兼容 fastadmin api)
- * @param string $message
- * @param $result
- * @return Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response_fast(int $code = 2,string $message = 'error', $result = null)
- {
- return self::response(json_encode([
- 'code' => $code,
- 'msg' => $message,
- 'data' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 身份信息已过期401(兼容 fastadmin api)
- * @param string $message
- * @param $result
- * @return \Psr\Http\Message\MessageInterface|ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- public static function response401(string $message = 'Identity is overdue', $result = null)
- {
- return self::response(json_encode([
- 'code' => 401,
- 'msg' => $message,
- 'data' => $result
- ], JSON_UNESCAPED_UNICODE));
- }
- /**
- * 统一返回格式
- * @param $data
- * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
- * @throws \Psr\Container\ContainerExceptionInterface
- * @throws \Psr\Container\NotFoundExceptionInterface
- */
- private static function response($data){
- $action = ActionUtil::getInstance()->get();
- if (!empty($action)){
- if (!empty($data['result']) && strlen(json_encode($data['result'])) > 1000){
- $data['result'] = '数据太多,不记录';
- }
- LogUtil::info('响应结果', $action['controller'], $action['action'],$data);
- }
- $response = self::container()->get(ResponseInterface::class);
- return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
- }
- /**
- * 容器实例
- * @return \Psr\Container\ContainerInterface
- */
- private static function container()
- {
- return ApplicationContext::getContainer();
- }
- }
|