Login.php 2.8 KB

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