Config.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. $arr = json_decode($value, true);
  38. return $arr ?: [];
  39. } elseif ($row['type'] == 'switch') {
  40. return (bool)$value;
  41. } elseif ($row['type'] == 'editor') {
  42. return !$value ? '' : htmlspecialchars_decode($value);
  43. } elseif ($row['type'] == 'city') {
  44. if ($value == '') {
  45. return [];
  46. }
  47. if (!is_array($value)) {
  48. return explode(',', $value);
  49. }
  50. return $value;
  51. } else {
  52. return $value ?: '';
  53. }
  54. }
  55. public function setValueAttr($value, $row)
  56. {
  57. if (in_array($row['type'], $this->jsonDecodeType)) {
  58. return $value ? json_encode($value) : '';
  59. } elseif ($row['type'] == 'switch') {
  60. return $value ? '1' : '0';
  61. } elseif ($row['type'] == 'time') {
  62. return $value ? date('H:i:s', strtotime($value)) : '';
  63. } elseif ($row['type'] == 'city') {
  64. if ($value && is_array($value)) {
  65. return implode(',', $value);
  66. }
  67. return $value ?: '';
  68. } elseif (is_array($value)) {
  69. return implode(',', $value);
  70. }
  71. return $value;
  72. }
  73. public function getContentAttr($value, $row)
  74. {
  75. if (!isset($row['type'])) return '';
  76. if (in_array($row['type'], $this->needContent)) {
  77. $arr = json_decode($value, true);
  78. return $arr ?: [];
  79. } else {
  80. return '';
  81. }
  82. }
  83. public function getExtendAttr($value)
  84. {
  85. if ($value) {
  86. $arr = json_decode($value, true);
  87. if ($arr) {
  88. unset($arr['baInputExtend']);
  89. return $arr;
  90. }
  91. }
  92. return [];
  93. }
  94. public function getInputExtendAttr($value, $row)
  95. {
  96. if ($row && $row['extend']) {
  97. $arr = json_decode($row['extend'], true);
  98. if ($arr && isset($arr['baInputExtend'])) {
  99. return $arr['baInputExtend'];
  100. }
  101. }
  102. return [];
  103. }
  104. }