Login.php 2.9 KB

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