Config.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. use think\facade\Cache;
  5. use app\admin\model\Config as adminConfigModel;
  6. class Config extends Model
  7. {
  8. /**
  9. * 添加系统配置分组
  10. */
  11. public static function addConfigGroup(string $key, string $value): bool
  12. {
  13. return self::addArrayItem('config_group', $key, $value);
  14. }
  15. /**
  16. * 删除系统配置分组
  17. */
  18. public static function removeConfigGroup(string $key): bool
  19. {
  20. return self::removeArrayItem('config_group', $key);
  21. }
  22. /**
  23. * 添加系统快捷配置入口
  24. */
  25. public static function addQuickEntrance(string $key, string $value): bool
  26. {
  27. return self::addArrayItem('config_quick_entrance', $key, $value);
  28. }
  29. /**
  30. * 删除系统快捷配置入口
  31. */
  32. public static function removeQuickEntrance(string $key): bool
  33. {
  34. return self::removeArrayItem('config_quick_entrance', $key);
  35. }
  36. /**
  37. * 为Array类型的配置项添加元素
  38. */
  39. public static function addArrayItem(string $name, string $key, string $value): bool
  40. {
  41. $configRow = adminConfigModel::where('name', $name)->find();
  42. foreach ($configRow->value as $item) {
  43. if ($item['key'] == $key) {
  44. return false;
  45. }
  46. }
  47. $configRow->value = array_merge($configRow->value, [['key' => $key, 'value' => $value]]);
  48. $configRow->save();
  49. Cache::tag(adminConfigModel::$cacheTag)->clear();
  50. return true;
  51. }
  52. /**
  53. * 删除Array类型配置项的一个元素
  54. */
  55. public static function removeArrayItem(string $name, string $key): bool
  56. {
  57. $configRow = adminConfigModel::where('name', $name)->find();
  58. $configRowValue = $configRow->value;
  59. foreach ($configRowValue as $iKey => $item) {
  60. if ($item['key'] == $key) {
  61. unset($configRowValue[$iKey]);
  62. }
  63. }
  64. $configRow->value = $configRowValue;
  65. $configRow->save();
  66. Cache::tag(adminConfigModel::$cacheTag)->clear();
  67. return true;
  68. }
  69. }