Plugs.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller\api;
  15. use think\admin\Controller;
  16. use think\admin\service\AdminService;
  17. use think\admin\service\SystemService;
  18. use think\exception\HttpResponseException;
  19. /**
  20. * 通用插件管理
  21. * Class Plugs
  22. * @package app\admin\controller\api
  23. */
  24. class Plugs extends Controller
  25. {
  26. /**
  27. * 系统图标选择器
  28. */
  29. public function icon()
  30. {
  31. $this->title = '图标选择器';
  32. $this->field = input('field', 'icon');
  33. $this->fetch(realpath(__DIR__ . '/../../view/api/icon.html'));
  34. }
  35. /**
  36. * 网站压缩发布
  37. * @login true
  38. */
  39. public function push()
  40. {
  41. try {
  42. if (AdminService::instance()->isSuper()) {
  43. SystemService::instance()->pushRuntime();
  44. $this->success('网站缓存加速成功!');
  45. } else {
  46. $this->error('只有超级管理员才能操作!');
  47. }
  48. } catch (HttpResponseException $exception) {
  49. throw $exception;
  50. } catch (\Exception $exception) {
  51. $this->error($exception->getMessage());
  52. }
  53. }
  54. /**
  55. * 清理运行缓存
  56. * @login true
  57. */
  58. public function clear()
  59. {
  60. try {
  61. if (AdminService::instance()->isSuper()) {
  62. SystemService::instance()->clearRuntime();
  63. $this->success('清理网站缓存成功!');
  64. } else {
  65. $this->error('只有超级管理员才能操作!');
  66. }
  67. } catch (HttpResponseException $exception) {
  68. throw $exception;
  69. } catch (\Exception $exception) {
  70. $this->error($exception->getMessage());
  71. }
  72. }
  73. /**
  74. * 数据变更日志
  75. * @login true
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function oplog()
  81. {
  82. if (AdminService::instance()->isSuper()) {
  83. $data = $this->_vali([
  84. 'state.in:0,1' => '状态值范围错误!',
  85. 'state.require' => '状态值不能为空!',
  86. ]);
  87. sysconf('base.oplog_state', $data['state']);
  88. $this->success('数据变更日志切换成功!');
  89. } else {
  90. $this->error('只有超级管理员才能操作!');
  91. }
  92. }
  93. /**
  94. * 当前运行模式
  95. * @login true
  96. */
  97. public function debug()
  98. {
  99. if (AdminService::instance()->isSuper()) {
  100. if (input('state')) {
  101. SystemService::instance()->productMode(true);
  102. $this->success('已切换为生产模式!');
  103. } else {
  104. SystemService::instance()->productMode(false);
  105. $this->success('已切换为开发模式!');
  106. }
  107. } else {
  108. $this->error('只有超级管理员才能操作!');
  109. }
  110. }
  111. /**
  112. * 优化数据库
  113. * @login true
  114. */
  115. public function optimize()
  116. {
  117. if (AdminService::instance()->isSuper()) {
  118. $this->_queue('优化数据库所有数据表', 'xadmin:database optimize', 0, [], 0, 0);
  119. } else {
  120. $this->error('只有超级管理员才能操作!');
  121. }
  122. }
  123. /**
  124. * 清理系统配置
  125. * @login true
  126. */
  127. public function clearConfig()
  128. {
  129. if (AdminService::instance()->isSuper()) try {
  130. $this->app->db->transaction(function () {
  131. [$tmpdata, $alldata] = [[], []];
  132. foreach ($this->app->db->name('SystemConfig')->cursor() as $item) {
  133. $tmpdata[$item['type']][$item['name']] = $item['value'];
  134. ksort($tmpdata[$item['type']]);
  135. }
  136. ksort($tmpdata);
  137. foreach ($tmpdata as $type => $items) foreach ($items as $name => $value) {
  138. $alldata[] = ['type' => $type, 'name' => $name, 'value' => $value];
  139. }
  140. $this->app->db->name('SystemConfig')->whereRaw('1=1')->delete();
  141. $this->app->db->name('SystemConfig')->insertAll($alldata);
  142. });
  143. $this->app->cache->delete('SystemConfig');
  144. $GLOBALS['oplogs'] = [];
  145. $this->success('清理系统配置成功!');
  146. } catch (HttpResponseException $exception) {
  147. throw $exception;
  148. } catch (\Exception $exception) {
  149. $this->error($exception->getMessage());
  150. } else {
  151. $this->error('只有超级管理员才能操作!');
  152. }
  153. }
  154. }