SystemConfigLogic.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\logic\system;
  8. use plugin\saiadmin\app\cache\ConfigCache;
  9. use plugin\saiadmin\app\model\system\SystemConfig;
  10. use plugin\saiadmin\app\model\system\SystemConfigGroup;
  11. use plugin\saiadmin\basic\think\BaseLogic;
  12. use plugin\saiadmin\exception\ApiException;
  13. use plugin\saiadmin\utils\Helper;
  14. /**
  15. * 参数配置逻辑层
  16. */
  17. class SystemConfigLogic extends BaseLogic
  18. {
  19. /**
  20. * 构造函数
  21. */
  22. public function __construct()
  23. {
  24. $this->model = new SystemConfig();
  25. }
  26. /**
  27. * 添加数据
  28. * @param mixed $data
  29. * @return mixed
  30. */
  31. public function add($data): mixed
  32. {
  33. $result = $this->model->create($data);
  34. $group = SystemConfigGroup::find($data['group_id']);
  35. ConfigCache::clearConfig($group->code);
  36. return $result;
  37. }
  38. /**
  39. * 编辑数据
  40. * @param mixed $id
  41. * @param mixed $data
  42. * @return bool
  43. */
  44. public function edit($id, $data): bool
  45. {
  46. $result = parent::edit($id, $data);
  47. $group = SystemConfigGroup::find($data['group_id']);
  48. ConfigCache::clearConfig($group->code);
  49. return $result;
  50. }
  51. /**
  52. * 批量更新
  53. * @param mixed $group_id
  54. * @param mixed $config
  55. * @return bool
  56. */
  57. public function batchUpdate($group_id, $config): bool
  58. {
  59. $group = SystemConfigGroup::find($group_id);
  60. if (!$group) {
  61. throw new ApiException('配置组未找到');
  62. }
  63. $saveData = [];
  64. foreach ($config as $key => $value) {
  65. $saveData[] = [
  66. 'id' => $value['id'],
  67. 'group_id' => $group_id,
  68. 'name' => $value['name'],
  69. 'key' => $value['key'],
  70. 'value' => $value['value']
  71. ];
  72. }
  73. // upsert: 根据 id 更新,如果不存在则插入
  74. $this->model->saveAll($saveData);
  75. ConfigCache::clearConfig($group->code);
  76. return true;
  77. }
  78. /**
  79. * 获取配置数据
  80. * @param mixed $code
  81. * @return array
  82. */
  83. public function getData($code): array
  84. {
  85. $group = SystemConfigGroup::where('code', $code)->findOrEmpty();
  86. if (empty($group)) {
  87. return [];
  88. }
  89. $config = SystemConfig::where('group_id', $group['id'])->select()->toArray();
  90. return $config;
  91. }
  92. /**
  93. * 获取配置组
  94. */
  95. public function getGroup($config): array
  96. {
  97. return ConfigCache::getConfig($config);
  98. }
  99. }