123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- namespace App\Exceptions;
- use App\Http\Utils\BaseUtil;
- use App\Http\Utils\LoggerFactoryUtil;
- use App\Models\RequestLogModel;
- use Exception;
- use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Tool\ShanTaoTool\MqTool;
- class Handler extends ExceptionHandler
- {
- /**
- * A list of the exception types that are not reported.
- *
- * @var array
- */
- protected $dontReport = [
- //
- ];
- /**
- * A list of the inputs that are never flashed for validation exceptions.
- *
- * @var array
- */
- protected $dontFlash = [
- 'password',
- 'password_confirmation',
- ];
- /**
- * Report or log an exception.
- *
- * @param \Exception $exception
- * @return void
- */
- public function report(Exception $exception)
- {
- parent::report($exception);
- }
- /**
- * Render an exception into an HTTP response.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Exception $exception
- * @return \Illuminate\Http\Response
- */
- public function render($request, Exception $exception)
- {
- LoggerFactoryUtil::setIsPush(1);
- // dd($exception);
- //拦截到异常,先做事务回滚
- DB::rollBack();
-
- //拦截异常记录请求日志
- $endTime = microtime(true);
- $startTime=LoggerFactoryUtil::getStartTime();
- if(!$startTime){
- $startTime = $endTime;
- }
- $instance = new LoggerFactoryUtil(Handler::class);
- $instance->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
- ]
- );
- }
- }
|