1
0

Auth.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace app\middleware;
  3. use app\model\Employee;
  4. use app\model\Grant;
  5. use app\model\GrantedAuth;
  6. use app\model\Org;
  7. use app\model\Permission;
  8. use think\exception\HttpException;
  9. use think\facade\Session;
  10. class Auth
  11. {
  12. public function handle($request, \Closure $next)
  13. {
  14. // 获取控制器
  15. $controller = lcfirst($request->controller());
  16. $action = lcfirst($request->action());
  17. // 如果是登陆相关操作 wisdom智慧屏演示
  18. if ($controller == 'login' || $controller == 'wisdom') return $next($request);
  19. // 微信绑定页面
  20. if ($controller == 'index' && $action == 'wechat') return $next($request);
  21. // 判断是否登陆
  22. $employee = session('employee');
  23. $domain = request()->domain();
  24. $url = strpos($domain,"zqxg.cc")!==false ? url('manage/login/index') : url('login/index');
  25. if (empty($employee)) return redirect($url);
  26. // 如果是多企业页面
  27. if ($controller == 'index' && $action == 'company') return $next($request);
  28. // 如果上传文件
  29. if ($action == 'osscallback') return $next($request);
  30. // 如果是多企业
  31. if (is_string($employee)) return redirect(url('index/company'));
  32. $sessionId = cache('employee_' . $employee['phone']);
  33. if(!empty($sessionId) && $sessionId != Session::getId()){
  34. Session::clear();
  35. return response('<script>alert("您的账号已在其他地方登录");location.href="'.url('login/index').'"</script>');
  36. }
  37. $line = Employee::where([['id', '=', $employee->id], ['grant_id', '<>', '0']])->count();
  38. if (!$line) {
  39. $employee = null;
  40. Session::clear();
  41. return redirect(url('login/index'));
  42. }
  43. // 获取用户权限
  44. // $grant = cache('grant_' . $employee->grand_id);
  45. // if (is_null($grant)) {
  46. $permissionIds = Grant::where('id', $employee->grant_id)->value('permission');
  47. $permissionIds = is_null($permissionIds) ? [] : $permissionIds;
  48. $permission = Permission::where([['id', 'in', $permissionIds], ['uri', '<>', '']])->column('uri,relation');
  49. $relation = explode(',', implode(',', array_column($permission, 'relation')));
  50. $grant = array_merge(array_column($permission, 'uri'), $relation);
  51. // cache('grant_' . $employee->grand_id, $grant);
  52. // }
  53. // 获取用户组织
  54. // $org = [$employee->org_id];
  55. // if ($employee->is_manager) {
  56. // $org = cache('org_manager_' . $employee->org_id);
  57. // if (is_null($org)) {
  58. // $org = orgSubIds($employee->org_id);
  59. //$org = (new Org())->getChildOrg($employee->org_id);
  60. // cache('org_manager_' . $employee->org_id, $org);
  61. // }
  62. // }
  63. $org = orgSubIds($employee->root_id);
  64. // 设置请求信息
  65. $request->employee = $employee;
  66. $request->grant = $grant;
  67. $request->org = $org;
  68. // 不验证权限页面
  69. if ($controller == 'index' || $controller == 'backup' || $controller=='ueditor') return $next($request);
  70. // 如果是查询获取企业人员
  71. if ($controller == 'statistics' && $action == 'get_person') return $next($request);
  72. if ($controller == 'org' && $action == 'employee') return $next($request);
  73. // 关联企业权限
  74. // $request->granted = (new GrantedAuth())->getIdsByAuth($controller . '_' . $action, $employee['root_id']);
  75. // 判断是否有权限
  76. $grant = array_unique($grant);
  77. if (!in_array($controller . '/' . $action, $grant)) {
  78. if ($request->isAjax()) {
  79. return json(['code' => 1, 'msg' => '无权限']);
  80. }
  81. throw new HttpException(404, '无权限');
  82. }
  83. return $next($request);
  84. }
  85. }