Login.php 2.7 KB

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