Http.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\common\exception;
  3. use app\hander\HelpHander;
  4. use Exception;
  5. use think\exception\Handle;
  6. use think\exception\HttpException;
  7. use think\exception\ValidateException;
  8. use think\Response;
  9. class Http extends Handle
  10. {
  11. public function render(Exception $e)
  12. {
  13. // 参数验证错误
  14. if ($e instanceof ValidateException) {
  15. $this->error('请求错误');
  16. }
  17. // 请求异常
  18. if ($e instanceof HttpException && request()->isAjax()) {
  19. $this->error('请求异常');
  20. }
  21. $this->error($e->getMessage());
  22. }
  23. private function error($msg, $code = 1, $data = [])
  24. {
  25. $code = $code == 0 ? 1 : $code; // code不能等于0
  26. if($data){
  27. $data = array_change_line_to_hump($data);
  28. }
  29. $json = [
  30. 'code' => $code,
  31. 'message' => $msg,
  32. 'data' => $data
  33. ];
  34. header('Content-Type:application/json; charset=utf-8');
  35. if(version_compare(PHP_VERSION,'5.4.0','<')){
  36. exit(json_encode($json));
  37. }else{
  38. exit(json_encode($json,JSON_UNESCAPED_UNICODE)); //显示中文
  39. }
  40. }
  41. }