EmpcrmAuth.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\middleware;
  3. use app\model\Employee;
  4. use app\model\Setting;
  5. use think\facade\Session;
  6. use think\exception\HttpException;
  7. use think\facade\View;
  8. /**
  9. * 主要用于用户后台登陆以及权限验证
  10. */
  11. class EmpcrmAuth
  12. {
  13. public function handle($request, \Closure $next)
  14. {
  15. $controller = lcfirst($request->controller());
  16. $action = lcfirst($request->action());
  17. if (strtolower($controller) == 'login') return $next($request);
  18. // 检测是否登陆
  19. $empcrm = session('empcrm');
  20. if (empty($empcrm)) return redirect(url('login/index'));
  21. // 如果是多企业页面
  22. if ($controller == 'index' && $action == 'company') return $next($request);
  23. // 如果上传文件
  24. if ($action == 'osscallback') return $next($request);
  25. // 如果是多企业
  26. if (is_string($empcrm)) return redirect(url('index/company'));
  27. $sessionId = cache('empcrm_' . $empcrm['phone']);
  28. if (!empty($sessionId) && $sessionId != Session::getId()) {
  29. Session::clear();
  30. if (request()->isAjax()) {
  31. return json(['code' => 403, 'data' => [], 'msg' => '您的账号已在其他地方登录']);
  32. } else {
  33. return response('<script>alert("您的账号已在其他地方登录");location.href="' . url('login/index') . '"</script>');
  34. }
  35. }
  36. // 防止后台切换部门 及 离职
  37. $find_emp = Employee::where('id', '=', $empcrm['id'])->findOrEmpty();
  38. if ($find_emp['state'] != '在职') {
  39. Session::delete('empcrm');
  40. Session::delete('empcrm_module');
  41. if (request()->isAjax()) {
  42. return json(['code' => 403, 'data' => [], 'msg' => '您的账号已不在职']);
  43. } else {
  44. return response('<script>alert("您的账号已不在职");location.href="' . url('login/index') . '"</script>');
  45. }
  46. }
  47. $empcrm_module = session('empcrm_module');
  48. //如果为空是首次登陆
  49. $disabled = false;
  50. if (empty($empcrm_module)) {
  51. // 判断是进入哪个模块,网销or社群
  52. if ($find_emp['empcrm_disable'] && $find_emp['community_disable']) {
  53. $disabled = true;
  54. } elseif ($find_emp['empcrm_disable']) {
  55. // 网销禁入 进入社群
  56. $empcrm_module = 2;
  57. session('empcrm_module', 2); // 1、网销 2、社群
  58. } elseif ($find_emp['community_disable']) {
  59. // 进入网销
  60. $empcrm_module = 1;
  61. session('empcrm_module', 1); // 1、网销 2、社群
  62. } else {
  63. // 都有默认进入社群
  64. $empcrm_module = 2;
  65. session('empcrm_module', 2); // 1、网销 2、社群
  66. }
  67. } else {
  68. // 当前模块
  69. $empcrm_module = session('empcrm_module');
  70. }
  71. if ($disabled) {
  72. // 不准进入程序
  73. Session::delete('empcrm');
  74. Session::delete('empcrm_module');
  75. if (request()->isAjax()) {
  76. return json(['code' => 403, 'data' => [], 'msg' => '账号无权限']);
  77. } else {
  78. return response('<script>alert("账号无权限");location.href="' . url('login/index') . '"</script>');
  79. }
  80. }
  81. if ($find_emp['org_id'] != $empcrm['org_id']) {
  82. Session::delete('empcrm');
  83. Session::delete('empcrm_module');
  84. if (request()->isAjax()) {
  85. return json(['code' => 403, 'data' => [], 'msg' => '您的账号已切换部门,请重新登陆']);
  86. } else {
  87. return response('<script>alert("您的账号已切换部门");location.href="' . url('login/index') . '"</script>');
  88. }
  89. }
  90. // 未跟进颜色提醒
  91. $tip_where[] = ['root_id', '=', $find_emp['root_id']];
  92. $tip_where[] = ['name', '=', 'no_follow_tips'];
  93. $info_tips = Setting::where($tip_where)->findOrEmpty();
  94. $need_tips = 0;
  95. if (!$info_tips->isEmpty()) {
  96. $need_tips = (int)$info_tips['content'];
  97. }
  98. View::assign('need_follow_tips', $need_tips);
  99. if ($empcrm_module == 1) {
  100. $grant = ['index/switch_model', 'empcrm/add_wetch', 'empcrm/fetchpoolitem', 'empcrm/pollcus_detail', 'empcrm/sign_savecust', 'empcrm/signcus_detail', 'empcrm/resources_sign', 'empcrm/save_introduce', 'empcrm/daily_detail', 'empcrm/customer_detail', 'empcrm/reserveDesigner', 'empcrm/get_person', 'empcrm/get_all_portrait_field', 'empcrm/validation', 'empcrm/poolFieldsSet', 'empcrm/model_new_report', 'empcrm/followup', 'empcrm/index', 'empcrm/poolFieldsList', 'index/index', 'empcrm/search_list', 'empcrm/daily', 'empcrm/new_daily_detail', 'empcrm/addlog', 'empcrm/addVisitlog', 'empcrm/list', 'empcrm/edit_customer', 'empcrm/add_customer', 'empcrm/model_report', 'empcrm/resources_list', 'empcrm/repay_list', 'empcrm/stay_confirm_list', 'empcrm/confirm_visit_list', 'empcrm/jiaoding_list', 'empcrm/assignment_list', 'managerEmp/shareList', 'empcrm/pool_list', 'managerEmp/proofread', 'empcrm/achment_list', 'empcrm/emp_daily', 'empcrm/invalid_report', 'empcrm/pool_list', 'index/company', 'index/welcome', 'index/logout', 'empcrm/bind', 'index/switch_shop', 'empcrm/kb', 'empcrm/fish_list', 'empcrm/fish_pool_fields_list', 'empcrm/fish_validation', 'empcrm/create_bridge_call', 'empcrm/fish_update_data', 'empcrm/tencent_list', 'empcrm/fish_list_total', 'empcrm/tencent_list_total', 'empcrm/fish_total', 'empcrm/fish_detail', 'managerEmp/get_person', 'empcrm/checkCustomer', 'empcrm/create_bridge_call_customer', 'managerEmp/get_error_customers', 'validCheck/apply', 'validCheck/apply_list', 'empcrm/pool_list_export'];
  101. if ($empcrm->is_manager) {
  102. $mangrant = ['managerEmp/recovery_customers', 'managerEmp/get_child_orgs', 'managerEmp/year_report', 'managerEmp/designer_report', 'empcrm/generalOpenData', 'empcrm/generalOpen', 'managerEmp/customer_statistics_list', 'empcrm/generalStatisticsData', 'empcrm/generalStatistics', 'managerEmp/account_report', 'managerEmp/edit_extension', 'managerEmp/account_detail', 'managerEmp/add_extension', 'managerEmp/extension_report', 'empcrm/customerTransfer', 'managerEmp/distribution_resources', 'managerEmp/get_persons', 'managerEmp/get_orgs', 'managerEmp/distribution', 'managerEmp/get_customers', 'managerEmp/get_uncustomers', 'managerEmp/read_new', 'managerEmp/delete_new', 'managerEmp/add', 'managerEmp/importLog', 'empcrm/statistics', 'empcrm/statistics_more', 'managerEmp/resource', 'managerEmp/importing', 'empcrm/jiance', 'empcrm/sel_wuxiao', 'managerEmp/seL_emplist', 'empcrm/fish_validation_view', 'managerEmp/seL_emplist', 'empcrm/fish_validation_again', 'managerEmp/account_detail_fish', 'statistics2/source', 'statistics2/sale', 'statistics2/designer', 'statistics2/sourceDetail', 'statistics2/designerDepartment', 'managerEmp/deleteExtension', 'validCheck/list', 'validCheck/check'];
  103. $grant = array_merge($grant, $mangrant);
  104. }
  105. // 判断是否有权限
  106. $grant = array_unique($grant);
  107. if (!in_array($controller . '/' . $action, $grant)) {
  108. if ($request->isAjax()) {
  109. return json(['code' => 1, 'msg' => '无权限']);
  110. }
  111. throw new HttpException(404, '无权限');
  112. }
  113. $root_id = $empcrm->root_id;
  114. //馨居尚,臻品软装,菡萏怡景装饰
  115. $xinjushang = 0;
  116. $empcrm['xinjushang'] = $xinjushang;
  117. View::assign('xinjushang', $xinjushang); //用于模板页面对馨居尚的判断
  118. // 进入的模块
  119. $empcrm['empcrm_module'] = 1;
  120. $request->empcrm = $empcrm;
  121. return $next($request);
  122. } elseif ($empcrm_module == 2) {
  123. // 更新社群的登陆时间
  124. Employee::where('id', '=', $empcrm['id'])->save(['community_login_time' => date('Y-m-d H:i:s')]);
  125. $grant = [];
  126. if ($empcrm->is_manager) {
  127. $mangrant = [];
  128. $grant = array_merge($grant, $mangrant);
  129. }
  130. // 判断是否有权限
  131. $grant = array_unique($grant);
  132. // if (!in_array($controller . '/' . $action, $grant)) {
  133. // if ($request->isAjax()) {
  134. // return json(['code' => 1, 'msg' => '无权限']);
  135. // }
  136. // throw new HttpException(404, '无权限');
  137. // }
  138. $root_id = $empcrm->root_id;
  139. //馨居尚,臻品软装,菡萏怡景装饰
  140. $xinjushang = 0;
  141. $empcrm['xinjushang'] = $xinjushang;
  142. View::assign('xinjushang', $xinjushang); //用于模板页面对馨居尚的判断
  143. // 进入的模块
  144. $empcrm['empcrm_module'] = 2;
  145. $request->empcrm = $empcrm;
  146. return $next($request);
  147. } else {
  148. Session::delete('empcrm');
  149. Session::delete('empcrm_module');
  150. if (request()->isAjax()) {
  151. return json(['code' => 403, 'data' => [], 'msg' => '请重新登陆']);
  152. } else {
  153. return response('<script>alert("请重新登陆");location.href="' . url('login/index') . '"</script>');
  154. }
  155. }
  156. }
  157. }