info("错误栈:".$exception->getTraceAsString()); //获取自定义的日志 $requestLog = ""; if(LoggerFactoryUtil::getMessage()){ $requestLog = json_encode(LoggerFactoryUtil::getMessage()); } //获取监听到的sql语句 $requestSqlLog = ""; if(LoggerFactoryUtil::getSqlMessage()){ $requestSqlLog = json_encode(LoggerFactoryUtil::getSqlMessage()); } $errCode = 500; $errMsg = ""; $oauthUrl = ""; //判断登录是否过期 if($exception->getCode()==403){ if(env("APP_ENV")=="local"){ $host = "127.0.0.1"; $redirectUrl = "http://test-daogou.codedreamit.com/api/wechat/account/callback"; }else{ $refer = $request->header("Referer"); Log::info("网址:".$refer); if(!$refer){ return $this->error("未知网址"); } //公众号未登录,获取授权地址 $host = parse_url($refer); if($host){ $host = $host["host"]; } $redirectUrl = "http://".$host."/api/wechat/account/callback"; } Log::info("域名:".$host); if($host=="servicewechat.com"){ return $this->error("未登录",401); } $wechatAccount = BaseUtil::getWechatAccountByHost($host); //获取邀请id $inviteId = $request->input("invite_id",""); $oauthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?next=dev&appid=".$wechatAccount->wechat_app_id; $callUrl = ''; if($request->input("callback_url ")){ $callUrl = $request->input("callback_url "); } $oauthUrl .= "&redirect_uri=".urldecode($redirectUrl)."&response_type=code&scope=snsapi_userinfo&state=".$wechatAccount->wechat_app_id."|$inviteId|".$callUrl; $oauthUrl .= "#wechat_redirect"; $errMsg = "未登录"; $errCode = 401; }elseif($exception instanceof ValidateException){ $errMsg = $exception->getMessage(); }else{ $errMsg = $exception->getMessage(); } if(env("REQUEST_LOG_FLAG")){ // RequestLogModel::insert($logData); $requestId = LoggerFactoryUtil::getRequestId()?LoggerFactoryUtil::getRequestId():floor(microtime(true)*1000); //获取请求数据 $param = $request->all(); //获取用户token $token = $request->header("X-TOKEN")?$request->header("X-TOKEN"):""; $param = array_merge($param,["token"=>$token]); //判断请求参数中是否存在traceId if(isset($_GET["traceId"])){ $traceId= $_GET["traceId"]; }else{ $traceId = md5(uniqid().time()); } $mqMessage = [ "requestPath"=>$request->path(), "requestParam"=>json_encode($param), "requestResponse"=>json_encode( [ "status"=>false, "data"=>"", "code"=>$errCode, "msg"=>$errMsg, "url"=>$oauthUrl ] ), "requestSqlLog"=>$requestSqlLog, "requestLog"=>$requestLog, "requestProjectName"=>"daogou", "createdAt"=>$startTime, "updatedAt"=>$endTime, "requestId"=>$requestId, "uniqueTraceId"=>$traceId ]; try{ MqTool::pushLogMessage(json_encode($mqMessage),"daogouLogKey"); }catch (\Exception $exception){}finally{} } return $this->error($errMsg,$errCode,$oauthUrl); } /** * 错误返回 * @param $msg * @param string $code * @param string $url */ public function error($msg,$code="500",$url="") { return response()->json( [ "status"=>false, "data"=>"", "code"=>$code, "msg"=>$msg, "url"=>$url ] ); } }