PublicController.php 8.3 KB

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