Config.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\admin\controller\system;
  3. use app\admin\model\SystemConfig;
  4. use app\admin\service\TriggerService;
  5. use app\common\controller\AdminController;
  6. use app\admin\service\annotation\ControllerAnnotation;
  7. use app\admin\service\annotation\NodeAnnotation;
  8. use app\Request;
  9. use think\App;
  10. use think\facade\Cache;
  11. use think\response\Json;
  12. #[ControllerAnnotation(title: '系统配置管理')]
  13. class Config extends AdminController
  14. {
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. self::$model = SystemConfig::class;
  19. $this->assign('upload_types', config('admin.upload_types'));
  20. $this->assign('editor_types', config('admin.editor_types'));
  21. }
  22. #[NodeAnnotation(title: '列表', auth: true)]
  23. public function index(Request $request): Json|string
  24. {
  25. return $this->fetch();
  26. }
  27. #[NodeAnnotation(title: '保存', auth: true)]
  28. public function save(Request $request): void
  29. {
  30. $this->checkPostRequest();
  31. $post = $request->post();
  32. $notAddFields = ['_token', 'file', 'group'];
  33. try {
  34. $group = $post['group'] ?? '';
  35. if (empty($group)) $this->error('保存失败');
  36. if ($group == 'upload') {
  37. $upload_types = config('admin.upload_types');
  38. // 兼容旧版本
  39. self::$model::where('name', 'upload_allow_type')->update(['value' => implode(',', array_keys($upload_types))]);
  40. }
  41. foreach ($post as $key => $val) {
  42. if (in_array($key, $notAddFields)) continue;
  43. if (self::$model::where('name', $key)->count()) {
  44. self::$model::where('name', $key)->update(['value' => $val,]);
  45. }else {
  46. self::$model::create(
  47. [
  48. 'name' => $key,
  49. 'value' => $val,
  50. 'group' => $group,
  51. ]);
  52. }
  53. if (Cache::has($key)) Cache::set($key, $val);
  54. }
  55. TriggerService::updateMenu();
  56. TriggerService::updateSysConfig();
  57. }catch (\Exception $e) {
  58. $this->error('保存失败' . $e->getMessage());
  59. }
  60. $this->success('保存成功');
  61. }
  62. }