AppResult.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils;
  4. use App\Utils\Control\ActionUtil;
  5. use Hyperf\Context\ApplicationContext;
  6. use Hyperf\HttpMessage\Stream\SwooleStream;
  7. use Hyperf\HttpServer\Contract\ResponseInterface;
  8. use Psr\Http\Message\ResponseInterface as Psr7ResponseInterface;
  9. /**
  10. * Author:Panda
  11. * Email:joeyoung0314@qq.com
  12. * Class AppResult
  13. * @package App\Utils
  14. */
  15. class AppResult
  16. {
  17. /**==============应用返回封装==============**/
  18. /**
  19. * 成功返回200
  20. * @param string $message
  21. * @param $result
  22. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  23. * @throws \Psr\Container\ContainerExceptionInterface
  24. * @throws \Psr\Container\NotFoundExceptionInterface
  25. */
  26. public static function response200(string $message = 'success', $result = null)
  27. {
  28. return self::response(json_encode([
  29. 'code' => 200,
  30. 'message' => $message,
  31. 'result' => $result,
  32. ], JSON_UNESCAPED_UNICODE));
  33. }
  34. /**
  35. * 失败返回201
  36. * @param string $message
  37. * @param $result
  38. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  39. * @throws \Psr\Container\ContainerExceptionInterface
  40. * @throws \Psr\Container\NotFoundExceptionInterface
  41. */
  42. public static function response201(string $message = 'error', $result = null)
  43. {
  44. return self::response(json_encode([
  45. 'code' => 201,
  46. 'message' => $message,
  47. 'result' => $result
  48. ], JSON_UNESCAPED_UNICODE));
  49. }
  50. /**
  51. * 失败返回202
  52. * @param string $message
  53. * @param $result
  54. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  55. * @throws \Psr\Container\ContainerExceptionInterface
  56. * @throws \Psr\Container\NotFoundExceptionInterface
  57. */
  58. public static function response202(string $message = 'error', $result = null)
  59. {
  60. return self::response(json_encode([
  61. 'code' => 202,
  62. 'message' => $message,
  63. 'result' => $result
  64. ], JSON_UNESCAPED_UNICODE));
  65. }
  66. /**
  67. * 参数缺失返回203
  68. * @param string $message
  69. * @param $result
  70. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  71. * @throws \Psr\Container\ContainerExceptionInterface
  72. * @throws \Psr\Container\NotFoundExceptionInterface
  73. */
  74. public static function response203(string $message = 'error', $result = null)
  75. {
  76. return self::response(json_encode([
  77. 'code' => 203,
  78. 'message' => $message,
  79. 'result' => $result
  80. ], JSON_UNESCAPED_UNICODE));
  81. }
  82. /**
  83. * 授权无用户返回204
  84. * @param string $message
  85. * @param $result
  86. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  87. * @throws \Psr\Container\ContainerExceptionInterface
  88. * @throws \Psr\Container\NotFoundExceptionInterface
  89. */
  90. public static function response204(string $message = '暂无此用户,请先授权手机号,并登录', $result = null)
  91. {
  92. return self::response(json_encode([
  93. 'code' => 204,
  94. 'message' => $message,
  95. 'result' => $result
  96. ], JSON_UNESCAPED_UNICODE));
  97. }
  98. /**
  99. * 请求次数过多返回206
  100. * @param string $message
  101. * @param $result
  102. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  103. * @throws \Psr\Container\ContainerExceptionInterface
  104. * @throws \Psr\Container\NotFoundExceptionInterface
  105. */
  106. public static function response206(string $message = '请求次数过多', $result = null)
  107. {
  108. return self::response(json_encode([
  109. 'code' => 206,
  110. 'message' => $message,
  111. 'result' => $result
  112. ], JSON_UNESCAPED_UNICODE));
  113. }
  114. /**
  115. * 身份信息已过期401
  116. * @param string $message
  117. * @param $result
  118. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  119. * @throws \Psr\Container\ContainerExceptionInterface
  120. * @throws \Psr\Container\NotFoundExceptionInterface
  121. */
  122. public static function response400(string $message = 'Identity is overdue', $result = null)
  123. {
  124. return self::response(json_encode([
  125. 'code' => 400,
  126. 'message' => $message,
  127. 'result' => $result
  128. ], JSON_UNESCAPED_UNICODE));
  129. }
  130. /**
  131. * 致命错误500
  132. * @param string $message
  133. * @param $result
  134. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  135. * @throws \Psr\Container\ContainerExceptionInterface
  136. * @throws \Psr\Container\NotFoundExceptionInterface
  137. */
  138. public static function response500(string $message = 'Fatal error', $result = null)
  139. {
  140. return self::response(json_encode([
  141. 'code' => 500,
  142. 'message' => $message,
  143. 'result' => $result
  144. ], JSON_UNESCAPED_UNICODE));
  145. }
  146. /**
  147. * 权限未拥有777
  148. * @param string $message
  149. * @param $result
  150. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  151. * @throws \Psr\Container\ContainerExceptionInterface
  152. * @throws \Psr\Container\NotFoundExceptionInterface
  153. */
  154. public static function response777(string $message = 'No Access', $result = null)
  155. {
  156. return self::response(json_encode([
  157. 'code' => 777,
  158. 'message' => $message,
  159. 'result' => $result
  160. ], JSON_UNESCAPED_UNICODE));
  161. }
  162. /**
  163. * 返回统一格式(兼容 fastadmin api)
  164. * @param string $message
  165. * @param $result
  166. * @return Psr7ResponseInterface
  167. * @throws \Psr\Container\ContainerExceptionInterface
  168. * @throws \Psr\Container\NotFoundExceptionInterface
  169. */
  170. public static function success(string $message = 'success', $result = null)
  171. {
  172. return self::response(json_encode([
  173. 'code' => 1,
  174. 'msg' => $message,
  175. 'data' => $result
  176. ], JSON_UNESCAPED_UNICODE));
  177. }
  178. /**
  179. * 返回统一格式(兼容 fastadmin api)
  180. * @param string $message
  181. * @param $result
  182. * @return Psr7ResponseInterface
  183. * @throws \Psr\Container\ContainerExceptionInterface
  184. * @throws \Psr\Container\NotFoundExceptionInterface
  185. */
  186. public static function error(string $message = 'error', $result = null)
  187. {
  188. return self::response(json_encode([
  189. 'code' => 0,
  190. 'msg' => $message,
  191. 'data' => $result
  192. ], JSON_UNESCAPED_UNICODE));
  193. }
  194. /**
  195. * 返回统一格式(兼容 fastadmin api)
  196. * @param string $message
  197. * @param $result
  198. * @return Psr7ResponseInterface
  199. * @throws \Psr\Container\ContainerExceptionInterface
  200. * @throws \Psr\Container\NotFoundExceptionInterface
  201. */
  202. public static function response_fast(int $code = 2,string $message = 'error', $result = null)
  203. {
  204. return self::response(json_encode([
  205. 'code' => $code,
  206. 'msg' => $message,
  207. 'data' => $result
  208. ], JSON_UNESCAPED_UNICODE));
  209. }
  210. /**
  211. * 身份信息已过期401(兼容 fastadmin api)
  212. * @param string $message
  213. * @param $result
  214. * @return \Psr\Http\Message\MessageInterface|ResponseInterface
  215. * @throws \Psr\Container\ContainerExceptionInterface
  216. * @throws \Psr\Container\NotFoundExceptionInterface
  217. */
  218. public static function response401(string $message = 'Identity is overdue', $result = null)
  219. {
  220. return self::response(json_encode([
  221. 'code' => 401,
  222. 'msg' => $message,
  223. 'data' => $result
  224. ], JSON_UNESCAPED_UNICODE));
  225. }
  226. /**
  227. * 统一返回格式
  228. * @param $data
  229. * @return \Psr\Http\Message\MessageInterface|Psr7ResponseInterface
  230. * @throws \Psr\Container\ContainerExceptionInterface
  231. * @throws \Psr\Container\NotFoundExceptionInterface
  232. */
  233. private static function response($data){
  234. $action = ActionUtil::getInstance()->get();
  235. if (!empty($action)){
  236. if (!empty($data['result']) && strlen(json_encode($data['result'])) > 1000){
  237. $data['result'] = '数据太多,不记录';
  238. }
  239. LogUtil::info('响应结果', $action['controller'], $action['action'],$data);
  240. }
  241. $response = self::container()->get(ResponseInterface::class);
  242. return $response->withAddedHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream($data));
  243. }
  244. /**
  245. * 容器实例
  246. * @return \Psr\Container\ContainerInterface
  247. */
  248. private static function container()
  249. {
  250. return ApplicationContext::getContainer();
  251. }
  252. }