love_php/app/Http/Middleware/OperationLogMiddleware.php
2026-04-02 09:20:51 +08:00

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);
}
}