海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
965 B

  1. <?php
  2. namespace App\Http\Middleware;
  3. use App\Models\User;
  4. use Closure;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\Cache;
  7. class ApiAuth
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param Request $request
  13. * @param Closure $next
  14. * @return mixed
  15. */
  16. public function handle(Request $request, Closure $next)
  17. {
  18. $auth = $request->header('Authentication');
  19. if (empty($auth)) {
  20. return response()->json([
  21. 'code' => -1,
  22. 'msg' => '关键认证参数缺失',
  23. 'data' => [],
  24. 'status' => 500,
  25. ]);
  26. }
  27. //检查用户 TODO 登录部分待优化
  28. $user_id = Cache::get($auth);
  29. if (empty($user_id) || $user_id != User::query()->where(['id' => $user_id, 'status' => 1])->value('id')) {
  30. return response()->json([
  31. 'code' => -1,
  32. 'msg' => '登录已超时或用户不存在,请重新登录',
  33. 'data' => [],
  34. 'status' => 403,
  35. ]);
  36. }
  37. return $next($request);
  38. }
  39. }