SystemConfigController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\controller\system;
  8. use plugin\saiadmin\basic\BaseController;
  9. use plugin\saiadmin\app\logic\system\SystemConfigLogic;
  10. use plugin\saiadmin\app\logic\system\SystemConfigGroupLogic;
  11. use plugin\saiadmin\app\validate\system\SystemConfigValidate;
  12. use support\Cache;
  13. use support\Request;
  14. use support\Response;
  15. /**
  16. * 配置项数据控制器
  17. */
  18. class SystemConfigController extends BaseController
  19. {
  20. /**
  21. * 构造
  22. */
  23. public function __construct()
  24. {
  25. $this->logic = new SystemConfigLogic();
  26. $this->validate = new SystemConfigValidate;
  27. parent::__construct();
  28. }
  29. /**
  30. * 数据列表
  31. * @param Request $request
  32. * @return Response
  33. */
  34. public function index(Request $request) : Response
  35. {
  36. $where = $request->more([
  37. ['group_id', ''],
  38. ['name', ''],
  39. ['key', ''],
  40. ]);
  41. $query = $this->logic->search($where);
  42. $data = $this->logic->getAll($query);
  43. return $this->success($data);
  44. }
  45. /**
  46. * 修改配置内容
  47. * @param Request $request
  48. * @return Response
  49. */
  50. public function batchUpdate(Request $request): Response
  51. {
  52. $group_id = $request->post('group_id');
  53. $config = $request->post('config');
  54. $groupLogic = new SystemConfigGroupLogic();
  55. $group = $groupLogic->where('id', $group_id)->findOrEmpty();
  56. if ($group->isEmpty()) {
  57. $this->fail('配置分组查找失败');
  58. }
  59. $saveData = [];
  60. foreach ($config as $key => $value) {
  61. $saveData[] = [
  62. 'id' => $value['id'],
  63. 'sort' => $value['sort'],
  64. 'name' => $value['name'],
  65. 'key' => $value['key'],
  66. 'value' => $value['value']
  67. ];
  68. }
  69. $this->logic->saveAll($saveData);
  70. Cache::set('cfg_'.$group->code, $saveData);
  71. return $this->success('操作成功');
  72. }
  73. /**
  74. * 数据改变后执行
  75. * @param $type
  76. * @param $args
  77. * @return void
  78. */
  79. protected function afterChange($type, $args): void
  80. {
  81. if (in_array($type, ['save', 'update'])) {
  82. $groupLogic = new SystemConfigGroupLogic();
  83. $group = $groupLogic->findOrEmpty(request()->input('group_id'));
  84. if (!$group->isEmpty()) {
  85. Cache::delete('cfg_' . $group['code']);
  86. }
  87. }
  88. }
  89. }