49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Middleware;
|
||
|
|
use Closure;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
use PHPUnit\Exception;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
use App\Models\OperationLog;
|
||
|
|
class OperationLogMiddleware
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Handle an incoming request.
|
||
|
|
*
|
||
|
|
* @param Request $request
|
||
|
|
* @param \Closure $next
|
||
|
|
* @return mixed
|
||
|
|
*/
|
||
|
|
public function handle(Request $request, Closure $next)
|
||
|
|
{
|
||
|
|
$response = $next($request);;
|
||
|
|
$input = json_encode($request->all()); //操作的内容
|
||
|
|
$path = $request->path(); //操作的路由
|
||
|
|
$method = $request->getRealMethod(); //操作的方法
|
||
|
|
$ip = $request->ip(); //操作的IP
|
||
|
|
$authorization = $request->header('Authorization');
|
||
|
|
$return_data = $response->getContent();
|
||
|
|
if(strlen($return_data) > 128){
|
||
|
|
$return_data = substr($return_data,128) . '......';
|
||
|
|
}
|
||
|
|
try{
|
||
|
|
}catch (Exception $e){
|
||
|
|
echo $e->getMessage();
|
||
|
|
}
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
public function writeLog($input,$path,$method,$ip,$return,$authorization){
|
||
|
|
$data = array(
|
||
|
|
'path'=>$path,
|
||
|
|
'method'=>$method,
|
||
|
|
'ip'=>$ip,
|
||
|
|
'input'=>json_encode($input, JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE),
|
||
|
|
'out' => json_encode($return,JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE),
|
||
|
|
'authorization' => $authorization
|
||
|
|
);
|
||
|
|
OperationLog::create($data);
|
||
|
|
}
|
||
|
|
}
|