Runtime.php 4.5 KB

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