Login.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\Http\Middleware\Company;
  3. use App\Facades\Servers\Encrypts\AccessToken;
  4. use Closure;
  5. use Illuminate\Http\Request;
  6. use App\Models\Admin;
  7. class Login
  8. {
  9. // 无需验证的路径
  10. protected $except = [
  11. 'api/login/index',
  12. ];
  13. //默认配置
  14. protected $_config = [
  15. 'auth_on' => true, // 认证开关
  16. 'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。
  17. 'auth_group' => 'auth_group', // 用户组数据表名
  18. 'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
  19. 'auth_rule' => 'auth_rule' // 权限规则表
  20. ];
  21. /**
  22. * $prefix表前缀
  23. */
  24. public function __construct()
  25. {
  26. // 判断配置
  27. if ( config('AUTH_CONFIG') ) {
  28. //可设置配置项 AUTH_CONFIG, 此配置项为数组。
  29. $this->_config = array_merge($this->_config, config('AUTH_CONFIG'));
  30. }
  31. }
  32. /**
  33. * Handle an incoming request.
  34. *
  35. * @param \Illuminate\Http\Request $request
  36. * @param \Closure $next
  37. *
  38. * @return mixed
  39. */
  40. public function handle(Request $request, Closure $next)
  41. {
  42. // 当前路径
  43. $path = ltrim($request->getPathInfo(), '/');
  44. // 判断是否需要验证登录
  45. if ( !in_array($path, $this->except) ) {
  46. // 获取登录结果
  47. $token = (string) $request->input('access_token_admin','');
  48. // 解码
  49. $userInfo = AccessToken::decode($token);
  50. // 判断登录时效
  51. if( isset($userInfo['error']) ) return json_send(['code'=>'no_login','msg'=>'请您登录','data'=>$userInfo['error']]);
  52. if( $userInfo['type'] != 'admin' ) return json_send(['code'=>'no_login','msg'=>'请您登录','data'=>'登录失效']);
  53. if( $userInfo['expire'] < time() ) return json_send(['code'=>'no_login','msg'=>'登录失效,请您重新登录','data'=>'登录失效']);
  54. // 是否是超管
  55. //$userInfo['is_super'] = is_super($userInfo['uid'],'admin') ? 1 : 0;
  56. // 追加入
  57. $request['access_token'] = $userInfo;
  58. }
  59. // 返回下一个闭包
  60. return $next($request);
  61. }
  62. }