ExtensionConfigModel.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\System\Model;
  4. use think\Model;
  5. use think\model\type\Json;
  6. /**
  7. * Class SixShop\System\Model\ExtensionConfigModel
  8. *
  9. * @property array $value 配置值,JSON格式存储
  10. * @property int $id 主键
  11. * @property string $create_time 创建时间
  12. * @property string $extension_id 模块ID,关联extension表
  13. * @property string $key 配置项名称
  14. * @property string $title 配置名称
  15. * @property string $type 配置类型:input, radio, select等
  16. * @property string $update_time 更新时间
  17. */
  18. class ExtensionConfigModel extends Model
  19. {
  20. protected $name = 'extension_config';
  21. protected $pk = 'id';
  22. protected function getOptions(): array
  23. {
  24. return [
  25. 'type' => [
  26. 'value' => 'json'
  27. ],
  28. 'jsonAssoc' => true,
  29. ];
  30. }
  31. public function getValueAttr(Json $value, array $data)
  32. {
  33. $raw = $value->value();
  34. $firstOrSelf = function ($val) {
  35. if (is_array($val)) {
  36. if (empty($val)) {
  37. return '';
  38. }
  39. $vals = array_values($val);
  40. return $vals[0] ?? '';
  41. }
  42. return $val;
  43. };
  44. return match ($data['type']) {
  45. 'radio', 'select', 'elTreeSelect', 'input' => (string)$firstOrSelf($raw),
  46. 'switch' => (bool)$raw,
  47. 'timePicker', 'colorPicker', 'datePicker', 'fcEditor' => (fn($val) => (is_array($val) && count($val) == 1) ? (array_values($val)[0] ?? '') : $val)($raw),
  48. default => $raw,
  49. };
  50. }
  51. }