PublicController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller;
  12. use app\Request;
  13. use app\services\system\attachment\SystemAttachmentServices;
  14. use app\services\system\SystemRouteServices;
  15. use crmeb\services\CacheService;
  16. use think\facade\Env;
  17. use think\Response;
  18. use think\facade\Db;
  19. class PublicController
  20. {
  21. /**
  22. * 下载文件
  23. * @param string $key
  24. * @return Response|\think\response\File
  25. */
  26. public function download(Request $request, string $key = '')
  27. {
  28. if ($key == '') {
  29. $key = $request->getMore([
  30. ['key', ''],
  31. ], true);
  32. }
  33. if (!$key) {
  34. return Response::create()->code(500);
  35. }
  36. $fileName = CacheService::get($key);
  37. if (is_array($fileName) && isset($fileName['path']) && isset($fileName['fileName']) && $fileName['path'] && $fileName['fileName'] && file_exists($fileName['path'])) {
  38. CacheService::delete($key);
  39. return download($fileName['path'], $fileName['fileName']);
  40. }
  41. return Response::create()->code(500);
  42. }
  43. /**
  44. * 获取workerman请求域名
  45. * @return mixed
  46. */
  47. public function getWorkerManUrl()
  48. {
  49. return app('json')->success(getWorkerManUrl());
  50. }
  51. /**
  52. * 扫码上传
  53. * @param Request $request
  54. * @param int $upload_type
  55. * @param int $type
  56. * @return Response
  57. * @author 吴汐
  58. * @email 442384644@qq.com
  59. * @date 2023/06/13
  60. */
  61. public function scanUpload(Request $request, $upload_type = 0, $type = 0)
  62. {
  63. [$file, $uploadToken, $pid] = $request->postMore([
  64. ['file', 'file'],
  65. ['uploadToken', ''],
  66. ['pid', 0]
  67. ], true);
  68. $service = app()->make(SystemAttachmentServices::class);
  69. if (CacheService::get('scan_upload') != $uploadToken) {
  70. return app('json')->fail(410086);
  71. }
  72. $service->upload((int)$pid, $file, $upload_type, $type, '', $uploadToken);
  73. return app('json')->success(100032);
  74. }
  75. public function import(Request $request)
  76. {
  77. $filePath = $request->param('file_path', '');
  78. if (empty($filePath)) {
  79. return app('json')->fail(12894);
  80. }
  81. app()->make(SystemRouteServices::class)->import($filePath);
  82. return app('json')->success(100010);
  83. }
  84. /**
  85. * 服务器信息
  86. * @return \think\Response
  87. * @author wuhaotian
  88. * @email 442384644@qq.com
  89. * @date 2024/9/24
  90. */
  91. public function getSystemInfo()
  92. {
  93. $info['server'] = [
  94. ['name' => '服务器系统', 'require' => '类UNIX', 'value' => PHP_OS],
  95. ['name' => 'WEB环境', 'require' => 'Apache/Nginx/IIS', 'value' => $_SERVER['SERVER_SOFTWARE']],
  96. ];
  97. $gd_info = function_exists('gd_info') ? gd_info() : array();
  98. $info['environment'] = [
  99. ['name' => 'PHP版本', 'require' => '7.1-7.4', 'value' => phpversion()],
  100. ['name' => 'MySql版本', 'require' => '5.6-8.0', 'value' => Db::query("SELECT VERSION()")[0]['VERSION()']],
  101. ['name' => 'MySqli', 'require' => '开启', 'value' => function_exists('mysqli_connect')],
  102. ['name' => 'Openssl', 'require' => '开启', 'value' => function_exists('openssl_encrypt')],
  103. ['name' => 'Session', 'require' => '开启', 'value' => function_exists('session_start')],
  104. ['name' => 'Safe_Mode', 'require' => '开启', 'value' => !ini_get('safe_mode')],
  105. ['name' => 'GD', 'require' => '开启', 'value' => !empty($gd_info['GD Version'])],
  106. ['name' => 'Curl', 'require' => '开启', 'value' => function_exists('curl_init')],
  107. ['name' => 'Bcmath', 'require' => '开启', 'value' => function_exists('bcadd')],
  108. ['name' => 'Upload', 'require' => '开启', 'value' => (bool)ini_get('file_uploads')],
  109. ];
  110. $info['permissions'] = [
  111. ['name' => 'backup', 'require' => '读写', 'value' => is_readable(root_path('backup')) && is_writable(root_path('backup'))],
  112. ['name' => 'public', 'require' => '读写', 'value' => is_readable(root_path('public')) && is_writable(root_path('public'))],
  113. ['name' => 'runtime', 'require' => '读写', 'value' => is_readable(root_path('runtime')) && is_writable(root_path('runtime'))],
  114. ['name' => '.env', 'require' => '读写', 'value' => is_readable(root_path() . '.env') && is_writable(root_path() . '.env')],
  115. ['name' => '.version', 'require' => '读写', 'value' => is_readable(root_path() . '.version') && is_writable(root_path() . '.version')],
  116. ['name' => '.constant', 'require' => '读写', 'value' => is_readable(root_path() . '.constant') && is_writable(root_path() . '.constant')],
  117. ];
  118. if (function_exists('exec')) {
  119. $workermanOutput = $timerOutput = $queueOutput = [];
  120. // exec("ps aux | grep 'php think workerman' | grep -v grep", $workermanOutput);
  121. $targetPort = config('workerman.chat.port');
  122. $thinkPath = root_path(); // think 文件的绝对路径
  123. $checkService = function($service) {
  124. if($service === 'queue'){
  125. $command = 'queue:listen';
  126. // 执行 ps 命令查找队列进程
  127. exec("ps aux | grep '{$command}' | grep -v grep", $output);
  128. // 若输出不为空,说明进程存在
  129. return !empty($output);
  130. } else {
  131. $pidFile = root_path('runtime') . $service . '.pid';
  132. // 优先检查PID文件
  133. if (!file_exists($pidFile)) {
  134. return false;
  135. }
  136. // 如果PID文件存在,尝试获取进程状态
  137. $pid = trim(file_get_contents($pidFile));
  138. if ($pid && is_numeric($pid)) {
  139. if (function_exists('posix_kill') && posix_kill($pid, 0)) {
  140. return true;
  141. }
  142. // 备用检查方法
  143. if (function_exists('exec')) {
  144. $output = [];
  145. exec("ps -ef | grep " . escapeshellarg($pid) . " | grep -v grep", $output);
  146. // 判断是否有非 grep 进程的输出
  147. return !empty($output);
  148. }
  149. }
  150. }
  151. };
  152. $info['process'] = [
  153. ['name' => '长链接', 'require' => '开启', 'value' => $checkService('workerman')],
  154. ['name' => '定时任务', 'require' => '开启', 'value' => $checkService('timer')],
  155. ['name' => '消息队列', 'require' => '开启', 'value' => $checkService('queue')],
  156. ];
  157. } else {
  158. $info['process'] = [
  159. ['name' => '长链接', 'require' => '开启', 'value' => file_exists(root_path('runtime') . 'workerman.pid')],
  160. ['name' => '定时任务', 'require' => '开启', 'value' => file_exists(root_path('runtime') . 'timer.pid')],
  161. ['name' => '消息队列', 'require' => '开启', 'value' => file_exists(root_path('runtime') . '.queue')],
  162. ];
  163. }
  164. return app('json')->success($info);
  165. }
  166. public function customAdminJs()
  167. {
  168. return sys_config('custom_admin_js', '');
  169. }
  170. }