You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
3.6 KiB
PHP
94 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
use App\Services\LogService;
|
|
|
|
class Log
|
|
{
|
|
/**
|
|
* Handle an incoming request.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
|
|
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
|
|
*/
|
|
public function handle(Request $request, Closure $next)
|
|
{
|
|
// $insert_id=0;
|
|
// $insert_id=self::requestLog($request,$insert_id); //记录请求时日志,不含返回信息
|
|
|
|
$response = $next($request);
|
|
$content = $response->getContent();
|
|
$data = json_decode($content, true); // 解码响应内容为关联数组
|
|
|
|
// 在关联数组中添加 code 字段
|
|
// $data['code'] = 200;
|
|
$data['code'] = $response->getStatusCode();
|
|
$modifiedContent = json_encode($data,JSON_UNESCAPED_UNICODE); // 编码修改后的关联数组为 JSON 字符串
|
|
$response->setContent($modifiedContent);
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
public function terminate(Request $request, $response)
|
|
{
|
|
|
|
if(env('REQUEST_LOG') ){ //如果返回状态为200进行log
|
|
|
|
$ip=self::getTrustedProxiesIp(); //真实ip
|
|
$request_header=$request->header(); //请求头
|
|
// dd($response);
|
|
// $response_data = $response->getData(); //返回data,json格式
|
|
$post_data=$request->post(); //post请求数据
|
|
$get_data=$request->query(); //get请求
|
|
$request_url=$request->getPathInfo();//访问的接口地址
|
|
$log=app()->make(LogService::class);
|
|
$log->RequestLog([
|
|
'ip'=>$ip,
|
|
'response_data'=>json_decode($response->getContent(),true),
|
|
'request_header'=>$request_header,
|
|
'post_data'=>$post_data,
|
|
'get_data'=>$get_data,
|
|
'request_url'=>$request_url,
|
|
],0);
|
|
}
|
|
|
|
}
|
|
public static function getTrustedProxiesIp(){ //获取用户真实ip
|
|
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
|
|
$ip = getenv('HTTP_CLIENT_IP');
|
|
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
|
|
$ip = getenv('HTTP_X_FORWARDED_FOR');
|
|
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
|
|
$ip = getenv('REMOTE_ADDR');
|
|
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
$res = preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches [0] : '';
|
|
return $res;
|
|
}
|
|
|
|
public static function requestLog($request,$insert_id){ //记录请求时日志,不含返回信息
|
|
if(env('REQUEST_LOG') ){ //如果返回状态为200进行log
|
|
$ip=self::getTrustedProxiesIp(); //真实ip
|
|
$request_header=$request->header(); //请求头
|
|
$post_data=$request->post(); //post请求数据
|
|
$get_data=$request->query(); //get请求
|
|
$request_url=$request->getPathInfo();//访问的接口地址
|
|
$log=app()->make(LogService::class);
|
|
return $log->RequestLog([
|
|
'ip'=>$ip,
|
|
'request_header'=>$request_header,
|
|
'post_data'=>$post_data,
|
|
'get_data'=>$get_data,
|
|
'request_url'=>$request_url,
|
|
],$insert_id);
|
|
}
|
|
|
|
}
|
|
}
|