Manage.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Utils\ApiResponse;
  4. use Closure;
  5. use Illuminate\Support\Facades\Cache;
  6. class Manage
  7. {
  8. /**
  9. * Handle an incoming request.
  10. *
  11. * @param \Illuminate\Http\Request $request
  12. * @param \Closure $next
  13. * @return mixed
  14. */
  15. public function handle($request, Closure $next)
  16. {
  17. $authorization = $request->input('auth_data') ?? $request->header('authorization');
  18. if (!$authorization)
  19. return ApiResponse::apiResponse(403,'未登录或者登录已经过期');
  20. // abort(403, '未登录或登陆已过期');
  21. $authData = explode(':', base64_decode($authorization));
  22. if (!Cache::has($authorization)) {
  23. if (!isset($authData[1]) || !isset($authData[0]))
  24. return ApiResponse::apiResponse(403,'请重新登入');
  25. $user = \App\Models\User::where('password', $authData[1])
  26. ->where('email', $authData[0])
  27. ->select([
  28. 'id',
  29. 'email',
  30. 'is_admin',
  31. 'is_staff'
  32. ])
  33. ->first();
  34. if (!$user)
  35. return ApiResponse::apiResponse(403,'请重新登入');
  36. Cache::put($authorization, $user->toArray(), 3600);
  37. }
  38. $request->merge([
  39. 'user' => Cache::get($authorization)
  40. ]);
  41. return $next($request);
  42. }
  43. }