Runtime.php 3.7 KB

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