Login.php 3.0 KB

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