ThemeController.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\ThemeService;
  5. use Illuminate\Support\Facades\File;
  6. use Illuminate\Support\Facades\Artisan;
  7. use Illuminate\Http\Request;
  8. class ThemeController extends Controller
  9. {
  10. private $themes;
  11. private $path;
  12. public function __construct()
  13. {
  14. $this->path = $path = public_path('theme/');
  15. $this->themes = array_map(function ($item) use ($path) {
  16. return str_replace($path, '', $item);
  17. }, glob($path . '*'));
  18. }
  19. public function getThemes()
  20. {
  21. $themeConfigs = [];
  22. foreach ($this->themes as $theme) {
  23. $themeConfigFile = $this->path . "{$theme}/config.json";
  24. if (!File::exists($themeConfigFile)) continue;
  25. $themeConfig = json_decode(File::get($themeConfigFile), true);
  26. if (!isset($themeConfig['configs']) || !is_array($themeConfig)) continue;
  27. $themeConfigs[$theme] = $themeConfig;
  28. if (config("theme.{$theme}")) continue;
  29. $themeService = new ThemeService($theme);
  30. $themeService->init();
  31. }
  32. return response([
  33. 'data' => [
  34. 'themes' => $themeConfigs,
  35. 'active' => config('v2board.frontend_theme', 'v2board')
  36. ]
  37. ]);
  38. }
  39. public function getThemeConfig(Request $request)
  40. {
  41. $payload = $request->validate([
  42. 'name' => 'required|in:' . join(',', $this->themes)
  43. ]);
  44. return response([
  45. 'data' => config("theme.{$payload['name']}")
  46. ]);
  47. }
  48. public function saveThemeConfig(Request $request)
  49. {
  50. $payload = $request->validate([
  51. 'name' => 'required|in:' . join(',', $this->themes),
  52. 'config' => 'required'
  53. ]);
  54. $payload['config'] = json_decode(base64_decode($payload['config']), true);
  55. if (!$payload['config'] || !is_array($payload['config'])) abort(500, '参数有误');
  56. $themeConfigFile = public_path("theme/{$payload['name']}/config.json");
  57. if (!File::exists($themeConfigFile)) abort(500, '主题不存在');
  58. $themeConfig = json_decode(File::get($themeConfigFile), true);
  59. if (!isset($themeConfig['configs']) || !is_array($themeConfig)) abort(500, '主题配置文件有误');
  60. $validateFields = array_column($themeConfig['configs'], 'field_name');
  61. $config = [];
  62. foreach ($validateFields as $validateField) {
  63. $config[$validateField] = isset($payload['config'][$validateField]) ? $payload['config'][$validateField] : '';
  64. }
  65. File::ensureDirectoryExists(base_path() . '/config/theme/');
  66. $data = var_export($config, 1);
  67. if (!File::put(base_path() . "/config/theme/{$payload['name']}.php", "<?php\n return $data ;")) {
  68. abort(500, '修改失败');
  69. }
  70. try {
  71. Artisan::call('config:cache');
  72. // sleep(2);
  73. } catch (\Exception $e) {
  74. abort(500, '保存失败');
  75. }
  76. return response([
  77. 'data' => $config
  78. ]);
  79. }
  80. }