Runtime.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller\api;
  16. use Exception;
  17. use think\admin\Controller;
  18. use think\admin\model\SystemConfig;
  19. use think\admin\service\AdminService;
  20. use think\admin\service\SystemService;
  21. use think\exception\HttpResponseException;
  22. /**
  23. * 系统运行控制管理
  24. * Class Runtime
  25. * @package app\admin\controller\api
  26. */
  27. class Runtime extends Controller
  28. {
  29. /**
  30. * 网站压缩发布
  31. * @login true
  32. */
  33. public function push()
  34. {
  35. if (AdminService::instance()->isSuper()) try {
  36. AdminService::instance()->clearCache();
  37. SystemService::instance()->pushRuntime();
  38. sysoplog('系统运维管理', '刷新并创建网站路由缓存');
  39. $this->success('网站缓存加速成功!', 'javascript:location.reload()');
  40. } catch (HttpResponseException $exception) {
  41. throw $exception;
  42. } catch (Exception $exception) {
  43. $this->error($exception->getMessage());
  44. } else {
  45. $this->error('只有超级管理员才能操作!');
  46. }
  47. }
  48. /**
  49. * 清理运行缓存
  50. * @login true
  51. */
  52. public function clear()
  53. {
  54. if (AdminService::instance()->isSuper()) try {
  55. AdminService::instance()->clearCache();
  56. SystemService::instance()->clearRuntime();
  57. sysoplog('系统运维管理', '清理网站日志及缓存数据');
  58. $this->success('清空缓存日志成功!', 'javascript:location.reload()');
  59. } catch (HttpResponseException $exception) {
  60. throw $exception;
  61. } catch (Exception $exception) {
  62. $this->error($exception->getMessage());
  63. } else {
  64. $this->error('只有超级管理员才能操作!');
  65. }
  66. }
  67. /**
  68. * 当前运行模式
  69. * @login true
  70. */
  71. public function debug()
  72. {
  73. if (AdminService::instance()->isSuper()) if (input('state')) {
  74. SystemService::instance()->setRuntime('product');
  75. sysoplog('系统运维管理', '由开发模式切换为生产模式');
  76. $this->success('已切换为生产模式!', 'javascript:location.reload()');
  77. } else {
  78. SystemService::instance()->setRuntime('debug');
  79. sysoplog('系统运维管理', '由生产模式切换为开发模式');
  80. $this->success('已切换为开发模式!', 'javascript:location.reload()');
  81. } else {
  82. $this->error('只有超级管理员才能操作!');
  83. }
  84. }
  85. /**
  86. * 清理系统配置
  87. * @login true
  88. */
  89. public function config()
  90. {
  91. if (AdminService::instance()->isSuper()) try {
  92. [$tmpdata, $newdata] = [[], []];
  93. foreach (SystemConfig::mk()->order('type,name asc')->cursor() as $item) {
  94. $tmpdata[$item['type']][$item['name']] = $item['value'];
  95. }
  96. foreach ($tmpdata as $type => $items) foreach ($items as $name => $value) {
  97. $newdata[] = ['type' => $type, 'name' => $name, 'value' => $value];
  98. }
  99. $this->app->db->transaction(function () use ($newdata) {
  100. SystemConfig::mQuery()->empty()->insertAll($newdata);
  101. });
  102. $this->app->cache->delete('SystemConfig');
  103. sysoplog('系统运维管理', '清理系统参数配置成功');
  104. $this->success('清理系统配置成功!', 'javascript:location.reload()');
  105. } catch (HttpResponseException $exception) {
  106. throw $exception;
  107. } catch (Exception $exception) {
  108. $this->error($exception->getMessage());
  109. } else {
  110. $this->error('只有超级管理员才能操作!');
  111. }
  112. }
  113. }