Logs.php 962B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use App\Models\ApiLogs;
  5. class Logs
  6. {
  7. /**
  8. * Handle an incoming request.
  9. *
  10. * @param \Illuminate\Http\Request $request
  11. * @param \Closure $next
  12. * @return mixed
  13. */
  14. public function handle($request, Closure $next)
  15. {
  16. $response = $next($request);
  17. //接口日志记录
  18. $params = $request->all();
  19. $query = [];
  20. if(!empty($params['activity_id'])){
  21. $query['activity_id'] = $params['activity_id'];
  22. }
  23. $query['api_name'] = $params['_url'];
  24. $query['params'] = json_encode($params);
  25. $userInfo = $request->session()->get('userInfo');
  26. $query['admin'] = $userInfo['username'];
  27. $codes = json_decode($response->getContent(), true);
  28. $query['code'] = $codes['error'];
  29. $query['create_time'] = time();
  30. ApiLogs::insert($query);
  31. return $response;
  32. }
  33. }