ConfigController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace plugin\admin\app\controller;
  3. use plugin\admin\app\common\Util;
  4. use plugin\admin\app\model\Option;
  5. use support\exception\BusinessException;
  6. use support\Request;
  7. use support\Response;
  8. use Throwable;
  9. /**
  10. * 系统设置
  11. */
  12. class ConfigController extends Base
  13. {
  14. /**
  15. * 不需要验证权限的方法
  16. * @var string[]
  17. */
  18. protected $noNeedAuth = ['get'];
  19. /**
  20. * 账户设置
  21. * @return Response
  22. * @throws Throwable
  23. */
  24. public function index(): Response
  25. {
  26. return raw_view('config/index');
  27. }
  28. /**
  29. * 获取配置
  30. * @return Response
  31. */
  32. public function get(): Response
  33. {
  34. return json($this->getByDefault());
  35. }
  36. /**
  37. * 基于配置文件获取默认权限
  38. * @return mixed
  39. */
  40. protected function getByDefault()
  41. {
  42. $name = 'system_config';
  43. $config = Option::where('name', $name)->value('value');
  44. if (empty($config)) {
  45. $config = file_get_contents(base_path('plugin/admin/public/config/pear.config.json'));
  46. if ($config) {
  47. $option = new Option();
  48. $option->name = $name;
  49. $option->value = $config;
  50. $option->save();
  51. }
  52. }
  53. return json_decode($config, true);
  54. }
  55. /**
  56. * 更改
  57. * @param Request $request
  58. * @return Response
  59. * @throws BusinessException
  60. */
  61. public function update(Request $request): Response
  62. {
  63. $post = $request->post();
  64. $config = $this->getByDefault();
  65. $data = [];
  66. foreach ($post as $section => $items) {
  67. if (!isset($config[$section])) {
  68. continue;
  69. }
  70. switch ($section) {
  71. case 'logo':
  72. $data[$section]['title'] = htmlspecialchars($items['title'] ?? '');
  73. $data[$section]['image'] = Util::filterUrlPath($items['image'] ?? '');
  74. $data[$section]['icp'] = htmlspecialchars($items['icp'] ?? '');
  75. $data[$section]['beian'] = htmlspecialchars($items['beian'] ?? '');
  76. $data[$section]['footer_txt'] = htmlspecialchars($items['footer_txt'] ?? '');
  77. break;
  78. case 'menu':
  79. $data[$section]['data'] = Util::filterUrlPath($items['data'] ?? '');
  80. $data[$section]['accordion'] = !empty($items['accordion']);
  81. $data[$section]['collapse'] = !empty($items['collapse']);
  82. $data[$section]['control'] = !empty($items['control']);
  83. $data[$section]['controlWidth'] = (int)($items['controlWidth'] ?? 2000);
  84. $data[$section]['select'] = (int)$items['select'] ?? 0;
  85. $data[$section]['async'] = true;
  86. break;
  87. case 'tab':
  88. $data[$section]['enable'] = true;
  89. $data[$section]['keepState'] = !empty($items['keepState']);
  90. $data[$section]['preload'] = !empty($items['preload']);
  91. $data[$section]['session'] = !empty($items['session']);
  92. $data[$section]['max'] = Util::filterNum($items['max'] ?? '30');
  93. $data[$section]['index']['id'] = Util::filterNum($items['index']['id'] ?? '0');
  94. $data[$section]['index']['href'] = Util::filterUrlPath($items['index']['href'] ?? '');
  95. $data[$section]['index']['title'] = htmlspecialchars($items['index']['title'] ?? '首页');
  96. break;
  97. case 'theme':
  98. $data[$section]['defaultColor'] = Util::filterNum($items['defaultColor'] ?? '2');
  99. $data[$section]['defaultMenu'] = $items['defaultMenu'] ?? '' == 'dark-theme' ? 'dark-theme' : 'light-theme';
  100. $data[$section]['defaultHeader'] = $items['defaultHeader'] ?? '' == 'dark-theme' ? 'dark-theme' : 'light-theme';
  101. $data[$section]['allowCustom'] = !empty($items['allowCustom']);
  102. $data[$section]['banner'] = !empty($items['banner']);
  103. break;
  104. case 'colors':
  105. foreach ($config['colors'] as $index => $item) {
  106. if (!isset($items[$index])) {
  107. $config['colors'][$index] = $item;
  108. continue;
  109. }
  110. $data_item = $items[$index];
  111. $data[$section][$index]['id'] = $index + 1;
  112. $data[$section][$index]['color'] = $this->filterColor($data_item['color'] ?? '');
  113. $data[$section][$index]['second'] = $this->filterColor($data_item['second'] ?? '');
  114. }
  115. break;
  116. }
  117. }
  118. $config = array_merge($config, $data);
  119. $name = 'system_config';
  120. Option::where('name', $name)->update([
  121. 'value' => json_encode($config)
  122. ]);
  123. return $this->json(0);
  124. }
  125. /**
  126. * 颜色检查
  127. * @param string $color
  128. * @return string
  129. * @throws BusinessException
  130. */
  131. protected function filterColor(string $color): string
  132. {
  133. if (!preg_match('/\#[a-zA-Z]6/', $color)) {
  134. throw new BusinessException('参数错误');
  135. }
  136. return $color;
  137. }
  138. }