SystemService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\sys;
  12. use app\job\sys\CheckJob;
  13. use app\service\core\sys\CoreConfigService;
  14. use core\base\BaseAdminService;
  15. use core\exception\CommonException;
  16. use think\facade\Db;
  17. use Throwable;
  18. /**
  19. * 系统信息服务层
  20. * Class SystemService
  21. * @package app\service\admin\sys
  22. */
  23. class SystemService extends BaseAdminService
  24. {
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * 获取版权信息(网站整体,不按照站点设置)
  31. * @return array
  32. */
  33. public function getInfo()
  34. {
  35. return [
  36. 'os' => PHP_OS,
  37. 'environment' => $_SERVER[ 'SERVER_SOFTWARE' ],
  38. 'php_v' => PHP_VERSION,
  39. 'version' => config('version')
  40. ];
  41. }
  42. /**
  43. * 获取域名配置
  44. */
  45. public function getUrl()
  46. {
  47. $wap_domain = !empty(env("system.wap_domain")) ? preg_replace('#/$#', '', env("system.wap_domain")) : request()->domain();
  48. $web_domain = !empty(env("system.web_domain")) ? preg_replace('#/$#', '', env("system.web_domain")) : request()->domain();
  49. return [
  50. 'wap_domain' => env("system.wap_domain"),
  51. 'wap_url' => $wap_domain . "/wap" ,
  52. 'web_url' => $web_domain . "/web",
  53. ];
  54. }
  55. /**
  56. * 获取系统信息
  57. * @return array
  58. */
  59. public function getSystemInfo()
  60. {
  61. $server = [];
  62. $server[] = [ "name" => get_lang('dict_setting.server_system'), "server" => PHP_OS ];
  63. $server[] = [ "name" => get_lang('dict_setting.server_setting'), "server" => PHP_SAPI ];
  64. $server[] = [ "name" => get_lang('dict_setting.php_version'), "server" => PHP_VERSION];
  65. //环境权限
  66. $system_variables = [];
  67. //pdo
  68. $pdo = extension_loaded('pdo') && extension_loaded('pdo_mysql');
  69. $system_variables[] = [ "name" => "pdo", "need" => get_lang('dict_setting.php_authority_ask'), "status" => $pdo ];
  70. //curl
  71. $curl = extension_loaded('curl') && function_exists('curl_init');
  72. $system_variables[] = [ "name" => "curl", "need" => get_lang('dict_setting.php_authority_ask'), "status" => $curl ];
  73. //openssl
  74. $openssl = extension_loaded('openssl');
  75. $system_variables[] = [ "name" => "openssl", "need" => get_lang('dict_setting.php_authority_ask'), "status" => $openssl ];
  76. //gd
  77. $gd = extension_loaded('gd');
  78. $system_variables[] = [ "name" => "GD库", "need" => get_lang('dict_setting.php_authority_ask'), "status" => $gd ];
  79. //fileinfo
  80. $fileinfo = extension_loaded('fileinfo');
  81. $system_variables[] = [ "name" => "fileinfo", "need" => get_lang('dict_setting.php_authority_ask'), "status" => $fileinfo ];
  82. //目录权限
  83. $root_path = str_replace("\\", DIRECTORY_SEPARATOR, dirname(__FILE__, 5));
  84. $root_path = str_replace("../", DIRECTORY_SEPARATOR, $root_path);
  85. $dirs_list = [
  86. [ "path" => $root_path . DIRECTORY_SEPARATOR . 'runtime' . DIRECTORY_SEPARATOR, "need" => get_lang('dict_setting.file_authority_ask'), "path_name" => "/runtime", "name" => "runtime" ],
  87. [ "path" => $root_path . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'upload' . DIRECTORY_SEPARATOR, "need" => get_lang('dict_setting.file_authority_ask'), "path_name" => "/public/upload", "name" => "upload" ],
  88. ];
  89. //目录 可读 可写检测
  90. foreach ($dirs_list as $k => $v) {
  91. $is_readable = is_readable($v[ "path" ]);
  92. $is_write = is_write($v[ "path" ]);
  93. if ($is_readable && $is_write) {
  94. $dirs_list[ $k ][ "status" ] = true;
  95. } else {
  96. $dirs_list[ $k ][ "status" ] = false;
  97. }
  98. }
  99. $system_variables = array_merge($system_variables, $dirs_list);
  100. //获取环境版本
  101. $server_version = [];
  102. $row = (array) Db::query("select VERSION() as verson");
  103. $server_version[] = [ "name" => get_lang('dict_setting.php_version'), "demand" => get_lang('dict_setting.php_ask'), "server" => PHP_VERSION];
  104. $server_version[] = [ "name" => get_lang('dict_setting.mysql_version'), "demand" => get_lang('dict_setting.mysql_ask'), "server" => $row[ 0 ][ 'verson' ] ];
  105. // 进程
  106. $process[] = [ "name" => "php think queue:listen", "need" => get_lang('dict_setting.php_authority_ask'), "status" => ( new SystemService() )->checkJob() ];
  107. return [ "server" => $server, "dirs_list" => $dirs_list, 'system_variables' => $system_variables, 'server_version' => $server_version, 'process' => $process ];
  108. }
  109. /**
  110. * 清理缓存
  111. */
  112. public function schemaCache()
  113. {
  114. if (is_dir(dirname($_SERVER[ 'DOCUMENT_ROOT' ]) . '/runtime/schema')) {
  115. rmdirs(dirname($_SERVER[ 'DOCUMENT_ROOT' ]) . '/runtime/schema');
  116. }
  117. return 'CLEAR_MYSQL_CACHE_SUCCESS';
  118. }
  119. /**
  120. *校验消息队列是否正常运行
  121. * @return bool
  122. */
  123. public function checkJob()
  124. {
  125. $secret = uniqid('', true);
  126. $file = root_path('runtime') . $secret . '.job';
  127. try {
  128. CheckJob::dispatch([ 'file' => $file ]);
  129. } catch ( Throwable $e) {
  130. return false;
  131. }
  132. // $timeout = 0;
  133. // while($timeout < 5){
  134. // if (file_exists($file)) {
  135. // @unlink($file);
  136. // return true;
  137. // }
  138. // $timeout++;
  139. // sleep(1);
  140. // }
  141. sleep(5);
  142. if (file_exists($file)) {
  143. @unlink($file);
  144. return true;
  145. }
  146. return false;
  147. }
  148. /**
  149. * 校验计划任务是否正常运行
  150. * @return bool
  151. */
  152. public function checkSchedule()
  153. {
  154. $file = root_path('runtime') . '.schedule';
  155. if (file_exists($file)) {
  156. $time = file_get_contents($file);
  157. if (!empty($time) && abs($time - time()) < 90) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. /**
  164. * 设置布局
  165. * @param string $key
  166. */
  167. public function setLayout(string $key) {
  168. $layouts = array_column(event('SiteLayout'), 'key');
  169. if (!in_array($key, $layouts)) throw new CommonException('LAYOUT_NOT_EXIST');
  170. (new CoreConfigService())->setConfig($this->site_id, 'SITE_LAYOUT', [ 'key' => $key ]);
  171. return true;
  172. }
  173. }