RequestLogMiddleware.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Models\UsersLog;
  4. use Carbon\Carbon;
  5. use Closure;
  6. use Illuminate\Contracts\Auth\Factory as Auth;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Jenssegers\Agent\Facades\Agent;
  9. use Zhuzhichao\IpLocationZh\Ip;
  10. use Illuminate\Support\Facades\Log;
  11. class RequestLogMiddleware
  12. {
  13. /**
  14. * The authentication guard factory instance.
  15. *
  16. * @var \Illuminate\Contracts\Auth\Factory
  17. */
  18. protected $auth;
  19. /**
  20. * Create a new middleware instance.
  21. *
  22. * @param \Illuminate\Contracts\Auth\Factory $auth
  23. * @return void
  24. */
  25. public function __construct(Auth $auth)
  26. {
  27. $this->auth = $auth;
  28. }
  29. /**
  30. * Handle an incoming request.
  31. *
  32. * @param \Illuminate\Http\Request $request
  33. * @param \Closure $next
  34. * @return mixed
  35. */
  36. public function handle($request, Closure $next)
  37. {
  38. // $ip=$request->header('ali-cdn-real-ip');
  39. // if (!$ip){
  40. // $ip = $request->header('X-Real-IP');
  41. // }
  42. // $address = Ip::find($ip);
  43. // $log = new UsersLog(); // 提前创建表、model
  44. // $log -> ip = $ip;
  45. // $log -> browser = Agent::browser(); // 浏览器名称
  46. // $log -> platform = Agent::platform();// 操作系统
  47. // $log -> device = Agent::device(); // 设备名称
  48. // $log -> address = $address[0].$address[1].$address[2];
  49. // $log -> is_phone = Agent::isPhone();
  50. // $log -> time = Carbon::now()->toDateTimeString();
  51. // $log -> created_at = Carbon::now()->toDateTimeString();
  52. // $log -> updated_at = Carbon::now()->toDateTimeString();
  53. // // 记录 用户信息
  54. // if($this->auth->check()) {
  55. // if($this->auth->user()->id) {
  56. // $log -> user_id = (int) $this->auth->user()->id ?? null;
  57. //// $log -> phone = $this->auth->user()->mobile ?? null;
  58. // $log -> nickname = $this->auth->user()->nickname ?? null;
  59. // }
  60. // }
  61. // // 记录 路由 方法 数据
  62. // $log -> path = $request->path();
  63. // $log -> method = $request->method();
  64. // $log -> input = json_encode($request->all(), JSON_UNESCAPED_UNICODE);
  65. // $log -> save(); # 记录日志
  66. return $next($request);
  67. }
  68. }