Auth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\middleware\admin;
  3. use app\service\admin\AuthServiceFacade;
  4. use app\service\admin\admin\UserServiceFacade;
  5. use laytp\BaseMiddleware;
  6. use laytp\library\Random;
  7. use laytp\library\Token;
  8. use think\Request;
  9. class Auth extends BaseMiddleware
  10. {
  11. /**
  12. * 执行中间件
  13. * @param Request $request
  14. * @param \Closure $next
  15. * @return mixed
  16. */
  17. public function handle(Request $request, \Closure $next)
  18. {
  19. // 检测脚本
  20. // $sysInfo = $this->w_getSysInfo();
  21. // if (isset($sysInfo['swoole_loader']) and isset($sysInfo['swoole_loader_version'])) {
  22. // if(!$sysInfo['swoole_loader']){
  23. // return $this->error('未安装脚本', 10402);
  24. // }
  25. // } else {
  26. // return $this->error('未安装脚本', 10402);
  27. // }
  28. // 兼容W7
  29. global $_W;
  30. // print_r($_W['setting']['copyright']);
  31. require IA_ROOT_WK . DS . 'config' . DS . 'config.php';
  32. if(SYS_TYPE == 'W7'){
  33. if (empty($_W['uniacid'])) {
  34. $uniacidKey = '__uniacid';
  35. $sessionKey = '__session';
  36. if(empty($_COOKIE[$configCookie['pre'] . $uniacidKey])) {
  37. return $this->error('没有获取到uniacid', 10403);
  38. }
  39. $_W['uniacid'] = $_COOKIE[$configCookie['pre'] . $uniacidKey];
  40. if (empty($_COOKIE[$configCookie['pre'] . $sessionKey])) {
  41. return $this->error('请重新从总平台登录', 10403);
  42. }
  43. }
  44. if(W7_TYPE=='W7R'){
  45. $_W['uniacid'] = 1;
  46. }
  47. } else {
  48. $_W['uniacid'] = 1;
  49. }
  50. if (AuthServiceFacade::needLogin()) {
  51. $initUser = UserServiceFacade::init($request->header('laytp-admin-token', $request->header('laytpAdminToken', $request->cookie('laytpAdminToken'))));
  52. // print_r($request->header('laytp-admin-token', $request->header('laytpAdminToken', $request->cookie('laytpAdminToken'))));
  53. if (!$initUser){
  54. if(SYS_TYPE == 'W7'){
  55. // if (empty($_W['uniacid'])){
  56. // return $this->error('登录信息无uniacid', 10403);
  57. // }
  58. // $loginUserInfo = \app\model\admin\User::where('username', '=', 'admin')->field(UserServiceFacade::getAllowFields())->findOrEmpty();
  59. // $userId = 1;
  60. // $token = Random::uuid();
  61. // $loginUserInfo['token'] = $token;
  62. // Token::set($token, $userId, 0);
  63. // print_r($token);
  64. // $initUser = UserServiceFacade::init($token);
  65. return $this->error('登录信息已过期', 10402);
  66. // print_r($initUser);
  67. } else {
  68. $this->error(UserServiceFacade::getError(), 10401);
  69. }
  70. }
  71. if (!UserServiceFacade::isLogin()) {
  72. if ($request->isAjax()) {
  73. if(SYS_TYPE == 'W7'){
  74. return $this->error('登录信息已过期', 10402);
  75. }else{
  76. return $this->error('登录信息已过期', 10401);
  77. }
  78. } else {
  79. return redirect('/admin/login.html');
  80. }
  81. }
  82. if (AuthServiceFacade::needAuth()) {
  83. $user = UserServiceFacade::getUser();
  84. // print_r($user);
  85. if ($user->is_super_manager !== 1) {
  86. $userId = $user->id;
  87. $plugin = defined('LT_PLUGIN') ? LT_PLUGIN : '';
  88. $controller = strtolower(str_replace("\\", ".", $request->controller()));
  89. if ($plugin) {
  90. $node = 'plugin/' . $plugin . '/' . $controller . '/' . $request->action();
  91. } else {
  92. $node = trim(app('http')->getName() . '/' . $controller . '/' . $request->action(),'/');
  93. }
  94. if (!AuthServiceFacade::hasAuth($userId, $node)) {
  95. return $this->error('无权请求:/' . $node);
  96. }
  97. }
  98. }
  99. }
  100. return $next($request);
  101. }
  102. public function w_getSysInfo() {
  103. global $env;
  104. $sysEnv = [];
  105. // Get content of phpinfo
  106. ob_start();
  107. phpinfo();
  108. $sysInfo = ob_get_contents();
  109. ob_end_clean();
  110. // Explode phpinfo content
  111. if ($env['php']['run_mode'] == 'cli') {
  112. $sysInfoList = explode('\n', $sysInfo);
  113. } else {
  114. $sysInfoList = explode('</tr>', $sysInfo);
  115. }
  116. foreach($sysInfoList as $sysInfoItem) {
  117. if (preg_match('/thread safety/i', $sysInfoItem)) {
  118. $sysEnv['thread_safety'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
  119. }
  120. if (preg_match('/swoole_loader support/i', $sysInfoItem)) {
  121. $sysEnv['swoole_loader'] = (preg_match('/(enabled|yes)/i', $sysInfoItem) != 0);
  122. }
  123. if (preg_match('/swoole_loader version/i', $sysInfoItem)) {
  124. preg_match('/\d+.\d+.\d+/s', $sysInfoItem, $match);
  125. $sysEnv['swoole_loader_version'] = isset($match[0]) ? $match[0] : false;
  126. }
  127. }
  128. //var_dump($sysEnv);die();
  129. return $sysEnv;
  130. }
  131. }