Login.php 2.9 KB

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