Handler.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace App\Exceptions;
  3. use App\Http\Utils\BaseUtil;
  4. use App\Http\Utils\LoggerFactoryUtil;
  5. use App\Models\RequestLogModel;
  6. use Exception;
  7. use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Tool\ShanTaoTool\MqTool;
  11. class Handler extends ExceptionHandler
  12. {
  13. /**
  14. * A list of the exception types that are not reported.
  15. *
  16. * @var array
  17. */
  18. protected $dontReport = [
  19. //
  20. ];
  21. /**
  22. * A list of the inputs that are never flashed for validation exceptions.
  23. *
  24. * @var array
  25. */
  26. protected $dontFlash = [
  27. 'password',
  28. 'password_confirmation',
  29. ];
  30. /**
  31. * Report or log an exception.
  32. *
  33. * @param \Exception $exception
  34. * @return void
  35. */
  36. public function report(Exception $exception)
  37. {
  38. parent::report($exception);
  39. }
  40. /**
  41. * Render an exception into an HTTP response.
  42. *
  43. * @param \Illuminate\Http\Request $request
  44. * @param \Exception $exception
  45. * @return \Illuminate\Http\Response
  46. */
  47. public function render($request, Exception $exception)
  48. {
  49. LoggerFactoryUtil::setIsPush(1);
  50. // dd($exception);
  51. //拦截到异常,先做事务回滚
  52. DB::rollBack();
  53. //拦截异常记录请求日志
  54. $endTime = microtime(true);
  55. $startTime=LoggerFactoryUtil::getStartTime();
  56. if(!$startTime){
  57. $startTime = $endTime;
  58. }
  59. $instance = new LoggerFactoryUtil(Handler::class);
  60. $instance->info("错误栈:".$exception->getTraceAsString());
  61. //获取自定义的日志
  62. $requestLog = "";
  63. if(LoggerFactoryUtil::getMessage()){
  64. $requestLog = json_encode(LoggerFactoryUtil::getMessage());
  65. }
  66. //获取监听到的sql语句
  67. $requestSqlLog = "";
  68. if(LoggerFactoryUtil::getSqlMessage()){
  69. $requestSqlLog = json_encode(LoggerFactoryUtil::getSqlMessage());
  70. }
  71. $errCode = 500;
  72. $errMsg = "";
  73. $oauthUrl = "";
  74. //判断登录是否过期
  75. if($exception->getCode()==403){
  76. if(env("APP_ENV")=="local"){
  77. $host = "127.0.0.1";
  78. $redirectUrl = "http://test-daogou.codedreamit.com/api/wechat/account/callback";
  79. }else{
  80. $refer = $request->header("Referer");
  81. Log::info("网址:".$refer);
  82. if(!$refer){
  83. return $this->error("未知网址");
  84. }
  85. //公众号未登录,获取授权地址
  86. $host = parse_url($refer);
  87. if($host){
  88. $host = $host["host"];
  89. }
  90. $redirectUrl = "http://".$host."/api/wechat/account/callback";
  91. }
  92. Log::info("域名:".$host);
  93. if($host=="servicewechat.com"){
  94. return $this->error("未登录",401);
  95. }
  96. $wechatAccount = BaseUtil::getWechatAccountByHost($host);
  97. //获取邀请id
  98. $inviteId = $request->input("invite_id","");
  99. $oauthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?next=dev&appid=".$wechatAccount->wechat_app_id;
  100. $callUrl = '';
  101. if($request->input("callback_url ")){
  102. $callUrl = $request->input("callback_url ");
  103. }
  104. $oauthUrl .= "&redirect_uri=".urldecode($redirectUrl)."&response_type=code&scope=snsapi_userinfo&state=".$wechatAccount->wechat_app_id."|$inviteId|".$callUrl;
  105. $oauthUrl .= "#wechat_redirect";
  106. $errMsg = "未登录";
  107. $errCode = 401;
  108. }elseif($exception instanceof ValidateException){
  109. $errMsg = $exception->getMessage();
  110. }else{
  111. $errMsg = $exception->getMessage();
  112. }
  113. if(env("REQUEST_LOG_FLAG")){
  114. // RequestLogModel::insert($logData);
  115. $requestId = LoggerFactoryUtil::getRequestId()?LoggerFactoryUtil::getRequestId():floor(microtime(true)*1000);
  116. //获取请求数据
  117. $param = $request->all();
  118. //获取用户token
  119. $token = $request->header("X-TOKEN")?$request->header("X-TOKEN"):"";
  120. $param = array_merge($param,["token"=>$token]);
  121. //判断请求参数中是否存在traceId
  122. if(isset($_GET["traceId"])){
  123. $traceId= $_GET["traceId"];
  124. }else{
  125. $traceId = md5(uniqid().time());
  126. }
  127. $mqMessage = [
  128. "requestPath"=>$request->path(),
  129. "requestParam"=>json_encode($param),
  130. "requestResponse"=>json_encode(
  131. [
  132. "status"=>false,
  133. "data"=>"",
  134. "code"=>$errCode,
  135. "msg"=>$errMsg,
  136. "url"=>$oauthUrl
  137. ]
  138. ),
  139. "requestSqlLog"=>$requestSqlLog,
  140. "requestLog"=>$requestLog,
  141. "requestProjectName"=>"daogou",
  142. "createdAt"=>$startTime,
  143. "updatedAt"=>$endTime,
  144. "requestId"=>$requestId,
  145. "uniqueTraceId"=>$traceId
  146. ];
  147. try{
  148. MqTool::pushLogMessage(json_encode($mqMessage),"daogouLogKey");
  149. }catch (\Exception $exception){}finally{}
  150. }
  151. return $this->error($errMsg,$errCode,$oauthUrl);
  152. }
  153. /**
  154. * 错误返回
  155. * @param $msg
  156. * @param string $code
  157. * @param string $url
  158. */
  159. public function error($msg,$code="500",$url="")
  160. {
  161. return response()->json(
  162. [
  163. "status"=>false,
  164. "data"=>"",
  165. "code"=>$code,
  166. "msg"=>$msg,
  167. "url"=>$url
  168. ]
  169. );
  170. }
  171. }