Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\model;
  3. use think\Model;
  4. class Config extends Model
  5. {
  6. public static $cacheTag = 'sys_config';
  7. protected $append = [
  8. 'value',
  9. 'content',
  10. 'extend',
  11. 'input_extend',
  12. ];
  13. protected $jsonDecodeType = ['checkbox', 'array', 'selects'];
  14. protected $needContent = ['radio', 'checkbox', 'select', 'selects'];
  15. public static function onBeforeInsert($model)
  16. {
  17. if (!in_array($model->getData('type'), $model->needContent)) {
  18. $model->content = null;
  19. } else {
  20. $model->content = json_encode(str_attr_to_array($model->getData('content')));
  21. }
  22. if (is_array($model->rule)) {
  23. $model->rule = implode(',', $model->rule);
  24. }
  25. if ($model->getData('extend') || $model->getData('inputExtend')) {
  26. $extend = str_attr_to_array($model->getData('extend'));
  27. $inputExtend = str_attr_to_array($model->getData('inputExtend'));
  28. if ($inputExtend) $extend['baInputExtend'] = $inputExtend;
  29. if ($extend) $model->extend = json_encode($extend);
  30. }
  31. $model->allow_del = 1;
  32. }
  33. public function getValueAttr($value, $row)
  34. {
  35. if (!isset($row['type'])) return $value;
  36. if (in_array($row['type'], $this->jsonDecodeType)) {
  37. return empty($value) ? [] : json_decode($value, true);
  38. } elseif ($row['type'] == 'switch') {
  39. return (bool)$value;
  40. } elseif ($row['type'] == 'editor') {
  41. return !$value ? '' : htmlspecialchars_decode($value);
  42. } elseif ($row['type'] == 'remoteSelect' && strpos($value, ',')) {
  43. return explode(',', $value);
  44. } elseif ($row['type'] == 'city') {
  45. if ($value == '') {
  46. return [];
  47. }
  48. if (!is_array($value)) {
  49. return explode(',', $value);
  50. }
  51. return $value;
  52. } else {
  53. return $value ?: '';
  54. }
  55. }
  56. public function setValueAttr($value, $row)
  57. {
  58. if (in_array($row['type'], $this->jsonDecodeType)) {
  59. return $value ? json_encode($value) : '';
  60. } elseif ($row['type'] == 'switch') {
  61. return $value ? '1' : '0';
  62. } elseif ($row['type'] == 'time') {
  63. return $value ? date('H:i:s', strtotime($value)) : '';
  64. } elseif ($row['type'] == 'city') {
  65. if ($value && is_array($value)) {
  66. return implode(',', $value);
  67. }
  68. return $value ?: '';
  69. } elseif (is_array($value)) {
  70. return implode(',', $value);
  71. }
  72. return $value;
  73. }
  74. public function getContentAttr($value, $row)
  75. {
  76. if (!isset($row['type'])) return '';
  77. if (in_array($row['type'], $this->needContent)) {
  78. $arr = json_decode($value, true);
  79. return $arr ?: [];
  80. } else {
  81. return '';
  82. }
  83. }
  84. public function getExtendAttr($value)
  85. {
  86. if ($value) {
  87. $arr = json_decode($value, true);
  88. if ($arr) {
  89. unset($arr['baInputExtend']);
  90. return $arr;
  91. }
  92. }
  93. return [];
  94. }
  95. public function getInputExtendAttr($value, $row)
  96. {
  97. if ($row && $row['extend']) {
  98. $arr = json_decode($row['extend'], true);
  99. if ($arr && isset($arr['baInputExtend'])) {
  100. return $arr['baInputExtend'];
  101. }
  102. }
  103. return [];
  104. }
  105. }