Login.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Middleware\Manager;
  3. use App\Facades\Servers\Encrypts\AccessToken;
  4. use Closure;
  5. use Illuminate\Http\Request;
  6. class Login
  7. {
  8. // 无需验证的路径
  9. protected $except = [
  10. 'manager/process/sub_notice',
  11. 'manager/login/index',
  12. 'manager/login/mobile',
  13. 'manager/login/email',
  14. 'manager/login/send_code',
  15. 'manager/login/mobile_code',
  16. 'manager/citys/list',
  17. 'manager/login/wechat',
  18. 'manager/citys/list_zoning',
  19. ];
  20. //默认配置
  21. protected $_config = [
  22. 'auth_on' => true, // 认证开关
  23. 'auth_type' => 1, // 认证方式,1为实时认证;2为登录认证。
  24. 'auth_group' => 'auth_group', // 用户组数据表名
  25. 'auth_group_access' => 'auth_group_access', // 用户-用户组关系表
  26. 'auth_rule' => 'auth_rule' // 权限规则表
  27. ];
  28. /**
  29. * $prefix表前缀
  30. */
  31. public function __construct()
  32. {
  33. // 判断配置
  34. if (config('AUTH_CONFIG')) {
  35. //可设置配置项 AUTH_CONFIG, 此配置项为数组。
  36. $this->_config = array_merge($this->_config, config('AUTH_CONFIG'));
  37. }
  38. }
  39. /**
  40. * Handle an incoming request.
  41. *
  42. * @param \Illuminate\Http\Request $request
  43. * @param \Closure $next
  44. * @return mixed
  45. */
  46. public function handle(Request $request, Closure $next)
  47. {
  48. // 当前路径
  49. $path = ltrim($request->getPathInfo(), '/');
  50. // 判断是否需要验证登录
  51. if (!in_array($path, $this->except)) {
  52. // 获取登录结果
  53. $token = (string) $request->input('access_token_manager', '');
  54. // 解码
  55. $userInfo = AccessToken::decode($token);
  56. // 判断登录时效
  57. if (isset($userInfo['error'])) return json_send(['code' => 'no_login', 'msg' => '请您登录', 'data' => $userInfo['error']]);
  58. if ($userInfo['type'] != 'manager' || $userInfo['expire'] < time()) return json_send(['code' => 'no_login', 'msg' => '请您登录', 'data' => '登录失效']);
  59. // 获取用户信息
  60. if($userInfo['is_admin'] == 0){
  61. $EmployeeModel = new \App\Models\Manager\Personnel\Employee();
  62. $Employee = $EmployeeModel->where('id', $userInfo['uid'])->first();
  63. if (!$Employee) return json_send(['code' => 'no_login', 'msg' => '记录不存在','data'=>'']);
  64. if ($Employee->status == 1) return json_send(['code' => 'no_login', 'msg' => '账号已被禁用','data'=>'']);
  65. }
  66. // 是否是超管
  67. $userInfo['is_super'] = is_super($userInfo['uid'],$userInfo['is_admin'], 'manager') ? 1 : 0;
  68. // 追加入
  69. $request['access_token'] = $userInfo;
  70. }
  71. // 返回下一个闭包
  72. return $next($request);
  73. }
  74. }