CheckDeviceScale.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use http\Cookie;
  5. use Illuminate\Http\Request;
  6. class CheckDeviceScale
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
  13. * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
  14. */
  15. public function handle(Request $request, Closure $next)
  16. {
  17. $deviceScale = env('DEVICE_SCALE');
  18. session()->put('ConnectionDevice', 'desktop');
  19. if (isset($deviceScale)) {
  20. $screenWidth = saveJavaScriptCookie( 'screenWidth' );
  21. $screenHeight = saveJavaScriptCookie( 'screenHeight' );
  22. $connectionDevice = 'desktop';
  23. if (empty($screenWidth) || empty($screenHeight)) {
  24. return $next($request);
  25. }
  26. $deviceScaleList = explode(';', $deviceScale);
  27. foreach ($deviceScaleList as $deviceScale) {
  28. [$device, $scale] = explode('>', $deviceScale);
  29. if ($screenWidth > $scale) {
  30. $connectionDevice = $device;
  31. }
  32. }
  33. session()->put('ConnectionDevice', $connectionDevice);
  34. }
  35. return $next($request);
  36. }
  37. }