ApiPermission.php 998 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace App\Base\Middleware;
  3. use App\Api\Facades\ApiFacade;
  4. use App\Base\Exceptions\ApiException;
  5. use Closure;
  6. class ApiPermission
  7. {
  8. public function handle($request, Closure $next, $guard = null)
  9. {
  10. $user = $request->user();
  11. if (!isset($user['id']) || !$user['id']) {
  12. throw new ApiException('common.auth_fail', '认证失败');
  13. }
  14. if (is_object($user)) {
  15. $user = $user->toArray();
  16. }
  17. $userId = $user['id'];
  18. $tk = $request->header('api_token');
  19. if(empty($tk)){
  20. $tk = $request->input('api_token');
  21. }
  22. $login_singleton=config('app.login_singleton');
  23. //如果不是最近登录的token
  24. if ($login_singleton&&!empty($tk)&& !ApiFacade::isLastToken($userId, $tk)) {
  25. ApiFacade::logout($tk);
  26. throw new ApiException('common.user_other_login', '您的账号已在其他地方登录!');
  27. }
  28. return $next($request);
  29. }
  30. }