MyException.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\service;
  3. use think\exception\Handle;
  4. use think\exception\HttpException;
  5. use think\exception\ValidateException;
  6. use think\Request;
  7. /**
  8. * TP5异常捕获托管类
  9. * Class MyException
  10. * @package app\common\service
  11. */
  12. class MyException extends Handle
  13. {
  14. public function render(\Exception $e)
  15. {
  16. // 参数验证错误
  17. if ($e instanceof ValidateException) {
  18. die(json_encode(['code'=>400,'msg'=>$e->getError(),'data'=>[]]));
  19. }
  20. // 请求异常
  21. if ($e instanceof HttpException && request()->isAjax()) {
  22. die(json_encode(['code'=>$e->getStatusCode(),'msg'=>$e->getMessage(),'data'=>[]]));
  23. }
  24. $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
  25. //TODO::开发者对异常的操作
  26. $input = file_get_contents("php://input");
  27. $errData = [
  28. 'requestUrl'=>$url,
  29. 'requestParam'=>['request'=>$_REQUEST,'input'=>@json_decode($input,true),'old_input'=>$input],
  30. 'msg'=>$e->getMessage(),
  31. 'line'=>$e->getLine(),
  32. 'path'=>$e->getFile(),
  33. ];
  34. if(strpos($url, "debug=xiepeng123@")){
  35. //可以在此交由系统处理
  36. return parent::render($e);
  37. }
  38. $this->pushRedis($errData);
  39. die(json_encode((['msg'=>$e->getMessage(),'code'=>400,'data'=>[]])));
  40. }
  41. public function pushRedis($data){
  42. try {
  43. $request = Request::instance();
  44. $data = [
  45. "companyCode"=>"CHENSEN",
  46. "projectCode"=>"CSAPI",
  47. "requestUrl"=>$data['requestUrl'],
  48. "requestParam"=>$data['requestParam'],
  49. "msg"=>$data['msg'],
  50. "line"=>$data['line'],
  51. "path"=>$data['path'],
  52. "requestTime"=>microtime(true),
  53. "_client_ip_"=>$request->ip(0,true)
  54. ];
  55. $redis = new \Redis();
  56. $redis->connect('47.97.187.118', 6379,2);
  57. $redis->auth('gudong-hz');
  58. $redis->rpush('email_list', json_encode($data));
  59. }catch (\Exception $ex){
  60. return true;
  61. }
  62. }
  63. }