AuthenticateBehavior.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: WEB端权限判断
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\common\behavior;
  8. use think\Cache;
  9. use think\Request;
  10. use think\Db;
  11. class AuthenticateBehavior
  12. {
  13. public function run(&$params)
  14. {
  15. /*防止跨域*/
  16. header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
  17. header('Access-Control-Allow-Credentials: true');
  18. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
  19. header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");
  20. $request = Request::instance();
  21. $m = strtolower($request->module());
  22. $c = strtolower($request->controller());
  23. $a = strtolower($request->action());
  24. //提交方式拦截
  25. $scan = new \com\Scan();
  26. $response = $scan->webscan_Check();
  27. $allow = $params['allow']; //登录用户可访问
  28. $permission = $params['permission']; //无限制
  29. /*获取头部信息*/
  30. $header = $request->header();
  31. $authKey = trim($header['authkey']);
  32. $paramArr = $request->param();
  33. $platform = $paramArr['platform'] ? '_'.$paramArr['platform'] : ''; //请求分类(mobile,ding)
  34. $cache = Cache::get('Auth_'.$authKey.$platform);
  35. $userInfo = $cache['userInfo'];
  36. if (in_array($a, $permission)) {
  37. return true;
  38. }
  39. if (empty($userInfo['id'])) {
  40. header('Content-Type:application/json; charset=utf-8');
  41. exit(json_encode(['code'=>101,'error'=>'请先登录']));
  42. }
  43. if ($userInfo['id'] == 1) {
  44. return true;
  45. }
  46. if (in_array($a, $allow)) {
  47. return true;
  48. }
  49. //管理员角色
  50. $adminTypes = adminGroupTypes($userInfo['id']);
  51. if (in_array(1,$adminTypes)) {
  52. return true;
  53. }
  54. //操作权限
  55. $res_per = checkPerByAction($m, $c, $a);
  56. if (!$res_per) {
  57. header('Content-Type:application/json; charset=utf-8');
  58. exit(json_encode(['code'=>102,'error'=>'无权操作']));
  59. }
  60. }
  61. }