Setting.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use app\model\SettingModel;
  5. use think\facade\Cache;
  6. use think\facade\Db;
  7. class Setting extends BaseController
  8. {
  9. function saveSetting(): \think\response\Json
  10. {
  11. $this->getAdmin();
  12. is_demo_mode(true);
  13. $list = $this->request->post('form');
  14. $tmp = [];
  15. foreach ($list as $key => $value) {
  16. $tmp[] = [
  17. 'keys' => $key,
  18. 'value' => $value
  19. ];
  20. }
  21. Db::table('setting')->replace()->insertAll($tmp);
  22. Cache::delete('webConfig');
  23. (new \app\controller\admin\Index(app()))->authorization();
  24. return $this->success('保存成功');
  25. }
  26. function refreshCache(): \think\response\Json
  27. {
  28. $this->getAdmin();
  29. Cache::delete('webConfig');
  30. return $this->success('刷新成功');
  31. }
  32. private function deleteDirectory($dir)
  33. {
  34. if (!is_dir($dir)) {
  35. return;
  36. }
  37. $files = scandir($dir);
  38. foreach ($files as $file) {
  39. if ($file != '.' && $file != '..') {
  40. if (is_dir("$dir/$file")) {
  41. $this->deleteDirectory("$dir/$file");
  42. } else {
  43. unlink("$dir/$file");
  44. }
  45. }
  46. }
  47. rmdir($dir);
  48. }
  49. function delRuntime(): \think\response\Json
  50. {
  51. $admin = $this->getAdmin();
  52. try
  53. {
  54. $dir = app()->getRuntimePath();
  55. $this->deleteDirectory($dir);
  56. return $this->success('删除成功');
  57. }catch (\Exception $e){
  58. return $this->error($e->getMessage());
  59. }
  60. }
  61. function getSetting(): \think\response\Json
  62. {
  63. $admin = $this->getAdmin();
  64. $role = $this->request->post('role', []);
  65. $info = SettingModel::Config();
  66. $tmp = [];
  67. $url = '';
  68. if (in_array('ext_name', $role)) {
  69. if (file_exists(public_path() . '/browserExt.zip')) {
  70. $url = '/browserExt.zip';
  71. }
  72. }
  73. if ($info) {
  74. if (count($role) > 0) {
  75. foreach ($info as $key => $val) {
  76. if (in_array($key, $role)) {
  77. $tmp[$key] = $val;
  78. }
  79. }
  80. }
  81. return json(['msg' => "ok", "data" => $tmp, 'success' => $this->auth, 'code' => 1, "url" => $url]);
  82. }
  83. return json(['msg' => 'ok', 'data' => false, 'success' => $this->auth, 'code' => 0, 'url' => $url]);
  84. }
  85. function mailTest(): \think\response\Json
  86. {
  87. $admin = $this->getAdmin();
  88. $email = $this->request->post("email");
  89. $config = $this->request->post("smtp", []);
  90. try {
  91. \Mail::testMail($email, $config);
  92. return $this->success('发送成功');
  93. } catch (\Exception $e) {
  94. return $this->error($e->getMessage());
  95. }
  96. }
  97. function delExt(): \think\response\Json
  98. {
  99. $this->getAdmin();
  100. if (file_exists(public_path() . '/browserExt.zip')) {
  101. unlink(public_path() . '/browserExt.zip');
  102. }
  103. return $this->success("ok");
  104. }
  105. }