Config.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\admin\model;
  3. use Throwable;
  4. use think\Model;
  5. use think\facade\Cache;
  6. /**
  7. * 系统配置模型
  8. * @property mixed $content
  9. * @property mixed $rule
  10. * @property mixed $extend
  11. * @property mixed $allow_del
  12. */
  13. class Config extends Model
  14. {
  15. public static string $cacheTag = 'sys_config';
  16. protected $append = [
  17. 'value',
  18. 'content',
  19. 'extend',
  20. 'input_extend',
  21. ];
  22. protected array $jsonDecodeType = ['checkbox', 'array', 'selects'];
  23. protected array $needContent = ['radio', 'checkbox', 'select', 'selects'];
  24. /**
  25. * 入库前
  26. * @throws Throwable
  27. */
  28. public static function onBeforeInsert(Config $model): void
  29. {
  30. if (!in_array($model->getData('type'), $model->needContent)) {
  31. $model->content = null;
  32. } else {
  33. $model->content = json_encode(str_attr_to_array($model->getData('content')));
  34. }
  35. if (is_array($model->rule)) {
  36. $model->rule = implode(',', $model->rule);
  37. }
  38. if ($model->getData('extend') || $model->getData('inputExtend')) {
  39. $extend = str_attr_to_array($model->getData('extend'));
  40. $inputExtend = str_attr_to_array($model->getData('inputExtend'));
  41. if ($inputExtend) $extend['baInputExtend'] = $inputExtend;
  42. if ($extend) $model->extend = json_encode($extend);
  43. }
  44. $model->allow_del = 1;
  45. }
  46. /**
  47. * 写入后
  48. */
  49. public static function onAfterWrite(): void
  50. {
  51. // 清理配置缓存
  52. Cache::tag(self::$cacheTag)->clear();
  53. }
  54. public function getValueAttr($value, $row)
  55. {
  56. if (!isset($row['type']) || $value == '0') return $value;
  57. if (in_array($row['type'], $this->jsonDecodeType)) {
  58. return empty($value) ? [] : json_decode($value, true);
  59. } elseif ($row['type'] == 'switch') {
  60. return (bool)$value;
  61. } elseif ($row['type'] == 'editor') {
  62. return !$value ? '' : htmlspecialchars_decode($value);
  63. } elseif (in_array($row['type'], ['city', 'remoteSelects'])) {
  64. if (!$value) return [];
  65. if (!is_array($value)) return explode(',', $value);
  66. return $value;
  67. } else {
  68. return $value ?: '';
  69. }
  70. }
  71. public function setValueAttr($value, $row): string
  72. {
  73. if (in_array($row['type'], $this->jsonDecodeType)) {
  74. return $value ? json_encode($value) : '';
  75. } elseif ($row['type'] == 'switch') {
  76. return $value ? '1' : '0';
  77. } elseif ($row['type'] == 'time') {
  78. return $value ? date('H:i:s', strtotime($value)) : '';
  79. } elseif ($row['type'] == 'city') {
  80. if ($value && is_array($value)) {
  81. return implode(',', $value);
  82. }
  83. return $value ?: '';
  84. } elseif (is_array($value)) {
  85. return implode(',', $value);
  86. }
  87. return $value;
  88. }
  89. public function getContentAttr($value, $row)
  90. {
  91. if (!isset($row['type'])) return '';
  92. if (in_array($row['type'], $this->needContent)) {
  93. $arr = json_decode($value, true);
  94. return $arr ?: [];
  95. } else {
  96. return '';
  97. }
  98. }
  99. public function getExtendAttr($value)
  100. {
  101. if ($value) {
  102. $arr = json_decode($value, true);
  103. if ($arr) {
  104. unset($arr['baInputExtend']);
  105. return $arr;
  106. }
  107. }
  108. return [];
  109. }
  110. public function getInputExtendAttr($value, $row)
  111. {
  112. if ($row && $row['extend']) {
  113. $arr = json_decode($row['extend'], true);
  114. if ($arr && isset($arr['baInputExtend'])) {
  115. return $arr['baInputExtend'];
  116. }
  117. }
  118. return [];
  119. }
  120. }