System.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 think\admin\Controller;
  17. use think\admin\model\SystemConfig;
  18. use think\admin\service\AdminService;
  19. use think\admin\service\SystemService;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * 系统运行控制管理
  23. * Class System
  24. * @package app\admin\controller\api
  25. */
  26. class System extends Controller
  27. {
  28. /**
  29. * 网站压缩发布
  30. * @login true
  31. */
  32. public function push()
  33. {
  34. if (AdminService::isSuper()) try {
  35. AdminService::clearCache();
  36. SystemService::pushRuntime();
  37. sysoplog('系统运维管理', '刷新创建路由缓存');
  38. $this->success('网站缓存加速成功!', 'javascript:location.reload()');
  39. } catch (HttpResponseException $exception) {
  40. throw $exception;
  41. } catch (\Exception $exception) {
  42. $this->error($exception->getMessage());
  43. } else {
  44. $this->error('只有超级管理员才能操作!');
  45. }
  46. }
  47. /**
  48. * 清理运行缓存
  49. * @login true
  50. */
  51. public function clear()
  52. {
  53. if (AdminService::isSuper()) try {
  54. AdminService::clearCache();
  55. SystemService::clearRuntime();
  56. sysoplog('系统运维管理', '清理网站日志缓存');
  57. $this->success('清空日志缓存成功!', 'javascript:location.reload()');
  58. } catch (HttpResponseException $exception) {
  59. throw $exception;
  60. } catch (\Exception $exception) {
  61. $this->error($exception->getMessage());
  62. } else {
  63. $this->error('只有超级管理员才能操作!');
  64. }
  65. }
  66. /**
  67. * 当前运行模式
  68. * @login true
  69. */
  70. public function debug()
  71. {
  72. if (AdminService::isSuper()) if (input('state')) {
  73. SystemService::setRuntime('product');
  74. sysoplog('系统运维管理', '开发模式切换为生产模式');
  75. $this->success('已切换为生产模式!', 'javascript:location.reload()');
  76. } else {
  77. SystemService::setRuntime('debug');
  78. sysoplog('系统运维管理', '生产模式切换为开发模式');
  79. $this->success('已切换为开发模式!', 'javascript:location.reload()');
  80. } else {
  81. $this->error('只有超级管理员才能操作!');
  82. }
  83. }
  84. /**
  85. * 修改富文本编辑器
  86. * @return void
  87. * @throws \think\db\exception\DataNotFoundException
  88. * @throws \think\db\exception\DbException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. */
  91. public function editor()
  92. {
  93. if (AdminService::isSuper()) {
  94. $editor = input('editor', 'auto');
  95. sysconf('base.editor', $editor);
  96. sysoplog('系统运维管理', "切换编辑器为{$editor}");
  97. $this->success('已切换后台编辑器!', 'javascript:location.reload()');
  98. } else {
  99. $this->error('只有超级管理员才能操作!');
  100. }
  101. }
  102. /**
  103. * 清理系统配置
  104. * @login true
  105. */
  106. public function config()
  107. {
  108. if (AdminService::isSuper()) try {
  109. [$tmpdata, $newdata] = [[], []];
  110. foreach (SystemConfig::mk()->order('type,name asc')->cursor() as $item) {
  111. $tmpdata[$item['type']][$item['name']] = $item['value'];
  112. }
  113. foreach ($tmpdata as $type => $items) foreach ($items as $name => $value) {
  114. $newdata[] = ['type' => $type, 'name' => $name, 'value' => $value];
  115. }
  116. $this->app->db->transaction(function () use ($newdata) {
  117. SystemConfig::mQuery()->empty()->insertAll($newdata);
  118. });
  119. $this->app->cache->delete('SystemConfig');
  120. sysoplog('系统运维管理', '清理系统配置参数');
  121. $this->success('清理系统配置成功!', 'javascript:location.reload()');
  122. } catch (HttpResponseException $exception) {
  123. throw $exception;
  124. } catch (\Exception $exception) {
  125. $this->error($exception->getMessage());
  126. } else {
  127. $this->error('只有超级管理员才能操作!');
  128. }
  129. }
  130. }