Config.php 2.2 KB

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