AppResult.php 8.1 KB

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