IndexController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\backend\modules\siteSetting\controllers;
  3. use app\common\components\BaseController;
  4. use app\common\facades\SiteSetting;
  5. use app\common\facades\Setting;
  6. use Illuminate\Filesystem\Filesystem;
  7. use app\common\helpers\Url;
  8. class IndexController extends BaseController
  9. {
  10. public function index()
  11. {
  12. $setting = SiteSetting::get('base');
  13. return view('siteSetting.index', [
  14. 'setting' => json_encode($setting),
  15. ])->render();
  16. }
  17. public function queue()
  18. {
  19. $setting = SiteSetting::get('queue');
  20. return view('siteSetting.queue', [
  21. 'setting' => json_encode($setting),
  22. ])->render();
  23. }
  24. public function physicsPath()
  25. {
  26. $physics = request()->physics;
  27. if (request()->ajax() && $physics) {
  28. $old_url = addslashes($physics['old_url']);
  29. $setModel = \app\common\models\Setting::uniacid()->where("value", "like", "%{$old_url}%")->get();
  30. if (!empty($setModel)) {
  31. foreach ($setModel as $kk => $vv) {
  32. $set = Setting::get($vv['group'] . '.' . $vv['key']);
  33. if ($vv['type'] == 'string') {
  34. $set = str_replace($physics['old_url'], $physics['new_url'], $set);
  35. Setting::set($vv['group'] . '.' . $vv['key'], $set);
  36. } elseif ($vv['type'] == 'array') {
  37. foreach ($set as $key => $value) {
  38. $set[$key] = str_replace($physics['old_url'], $physics['new_url'], $value);
  39. }
  40. Setting::set($vv['group'] . '.' . $vv['key'], $set);
  41. }
  42. }
  43. \Artisan::call('config:cache');
  44. \Cache::flush();
  45. return $this->successJson(' 物理路径更新成功');
  46. } else {
  47. return $this->errorJson(' 没有对应的数据');
  48. }
  49. }
  50. return view('siteSetting.physics_path');
  51. }
  52. //redis配置 写入文件
  53. public function redisConfig()
  54. {
  55. if (config('app.APP_Framework', false) == 'platform') {
  56. $request = request()->redis;
  57. if (strtolower($request['password']) == 'null') {
  58. $pwd = '';
  59. } else {
  60. $pwd = $request['password'];
  61. }
  62. if ($request) {
  63. $str = '<?php
  64. $redis = array();
  65. $redis[\'host\'] = \'' . $request['host'] . '\';
  66. $redis[\'password\'] = \'' . $pwd . '\';
  67. $redis[\'port\'] = \'' . $request['port'] . '\';
  68. $redis[\'database\'] = \'' . $request['database'] . '\';
  69. $redis[\'cache_database\'] = \'' . $request['cache_database'] . '\';
  70. ';
  71. app(Filesystem::class)->put(base_path('database/redis.php'), $str); //写入配置文件
  72. \Setting::set('redis', $request);
  73. \Artisan::call('config:clear');
  74. \Artisan::call('cache:clear');
  75. return $this->successJson('修改成功,如没生效可尝试清理缓存后重试');
  76. }
  77. $redis = array();
  78. if (file_exists(base_path('database/redis.php'))) {
  79. include base_path('database/redis.php');
  80. }
  81. } elseif (config('app.APP_Framework', false) != 'platform') {
  82. //微擎版
  83. $request = request()->redis;
  84. if ($request) {
  85. try {
  86. app('redis')->select($request['database']);
  87. app('redis')->select($request['cache_database']);
  88. } catch (\Exception $e) {
  89. //redis报错,不给修改
  90. return $this->errorJson('修改失败,redis配置连接不成功,请检查填写的redis库');
  91. }
  92. $data = [
  93. 'REDIS_DEFAULT_DATABASE' => $request['database'],
  94. 'REDIS_CACHE_DATABASE' => $request['cache_database']
  95. ];
  96. $this->editEnvFile($data);
  97. \Artisan::call('config:clear');
  98. \Artisan::call('cache:clear');
  99. return $this->successJson('修改成功,如没生效可尝试清理缓存后重试');
  100. }
  101. $redis = [
  102. 'database' => config('app.redis_default_database'),
  103. 'cache_database' => config('app.redis_cache_database')
  104. ];
  105. }
  106. return view('siteSetting.redis_config', ['set' => json_encode($redis)]);
  107. }
  108. //MongoDB配置 写入文件
  109. public function mongoDBConfig()
  110. {
  111. $data = request()->mongoDB;
  112. if ($data) {
  113. $str = '<?php
  114. $mongoDB = array();
  115. $mongoDB[\'host\'] = \'' . $data['host'] . '\';
  116. $mongoDB[\'username\'] = \'' . $data['username'] . '\';
  117. $mongoDB[\'password\'] = \'' . $data['password'] . '\';
  118. $mongoDB[\'port\'] = \'' . $data['port'] . '\';
  119. $mongoDB[\'database\'] = \'' . $data['database'] . '\';
  120. ';
  121. app(Filesystem::class)->put(base_path('database/mongoDB.php'), $str); //写入配置文件
  122. //清理缓存
  123. \Artisan::call('config:clear');
  124. \Artisan::call('cache:clear');
  125. return $this->successJson('修改成功,如没生效可尝试清理缓存后重试');
  126. }
  127. $mongoDB = array();
  128. if (file_exists(base_path('database/mongoDB.php'))) {
  129. include base_path('database/mongoDB.php');
  130. }
  131. return view('siteSetting.mongoDB_config', ['set' => json_encode($mongoDB)]);
  132. }
  133. public function websocketConfig()
  134. {
  135. $setting = SiteSetting::get('websocket');
  136. $setting['is_open'] = $setting['is_open'] ?? 0;
  137. return view('siteSetting.websocket', [
  138. 'setting' => json_encode($setting),
  139. ])->render();
  140. }
  141. /**
  142. * 修改.env文件值
  143. * @param array $data
  144. * @return bool
  145. */
  146. public function editEnvFile($data = [])
  147. {
  148. $envPath = base_path() . DIRECTORY_SEPARATOR . '.env';
  149. $contentArray = collect(file($envPath, FILE_IGNORE_NEW_LINES));
  150. $inArr = [];
  151. $contentArray->transform(function ($item) use ($data, &$inArr) {
  152. foreach ($data as $key => $value) {
  153. if (str_contains($item, $key)) {
  154. $inArr[] = $key;
  155. return $key . '=' . $value;
  156. }
  157. }
  158. return $item;
  159. });
  160. foreach ($data as $key => $value) {
  161. if (!in_array($key, $inArr)) {
  162. $contentArray->push($key . '=' . $value);
  163. }
  164. }
  165. $content = implode($contentArray->toArray(), "\n");
  166. app(Filesystem::class)->put(base_path('.env'), $content); //写入配置文件
  167. return true;
  168. }
  169. }