Config.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\admin\model\shopro;
  3. class Config extends Common
  4. {
  5. /**
  6. * 主键
  7. */
  8. protected $pk = 'code';
  9. protected $name = 'shopro_config';
  10. protected $autoWriteTimestamp = false;
  11. /**
  12. * 获取配置组
  13. *
  14. * @param string $code
  15. * @param boolean $cache
  16. * @return array
  17. */
  18. public static function getConfigs($code, $cache = true)
  19. {
  20. // 从缓存中获取
  21. if ($cache) {
  22. $config = operate_disabled(false) ? cache("config:{$code}") : null;
  23. if (empty($config)) {
  24. $config = self::getConfigs($code, false);
  25. }
  26. }
  27. // 从数据库中查找
  28. if (!$cache) {
  29. $row = self::where('code', $code)->find();
  30. if(!$row) return null;
  31. if($row['type'] !== 'group') {
  32. $config = $row->value;
  33. }else {
  34. $config = [];
  35. $list = self::where('parent_code', $code)->select();
  36. foreach ($list as &$row) {
  37. if ($row['type'] === 'group') {
  38. $row->value = self::getConfigs($row->code, false);
  39. } else {
  40. cache("config:{$row->code}", $row->value);
  41. }
  42. $config[self::getShortCode($row)] = $row->value;
  43. }
  44. }
  45. // 设置配置缓存
  46. cache("config:{$code}", $config);
  47. }
  48. return $config;
  49. }
  50. /**
  51. * 获取单一配置项
  52. *
  53. * @param string $code
  54. * @param boolean $cache
  55. * @return mixed
  56. */
  57. public static function getConfigField($code, $cache = true)
  58. {
  59. // 从缓存中获取
  60. if ($cache) {
  61. $config = cache("config:{$code}");
  62. if (empty($config)) {
  63. $config = self::getConfigField($code, false);
  64. }
  65. }
  66. // 从数据库中查找
  67. if (!$cache) {
  68. $config = self::where('code', $code)->value('value');
  69. // 设置配置缓存
  70. cache("config:{$code}", $config);
  71. }
  72. return $config;
  73. }
  74. private static function getShortCode($config)
  75. {
  76. if (!empty($config['parent_code'])) {
  77. return str_replace("{$config['parent_code']}.", "", $config['code']);
  78. }
  79. return $config['code'];
  80. }
  81. /**
  82. * 更新配置
  83. *
  84. * @param string $code
  85. * @param array $configParams
  86. * @return void
  87. */
  88. public static function setConfigs(string $code, array $configParams)
  89. {
  90. operate_filter();
  91. foreach ($configParams as $configKey => $configValue) {
  92. self::setConfigField($code . '.' . $configKey, $configValue);
  93. }
  94. self::getConfigs(explode('.', $code)[0], false);
  95. return self::getConfigs($code);
  96. }
  97. /**
  98. * 更新配置项
  99. */
  100. private static function setConfigField($code, $value)
  101. {
  102. $config = self::where('code', $code)->find();
  103. if ($config) {
  104. if ($config['type'] === 'group') {
  105. foreach ($value as $k => $v) {
  106. self::setConfigField($code . '.' . $k, $v);
  107. }
  108. } else {
  109. $config->value = $value;
  110. $config->save();
  111. }
  112. }
  113. }
  114. /**
  115. * 修改器 数据的保存格式
  116. *
  117. * @param string|array $value
  118. * @param array $data
  119. * @return string
  120. */
  121. public function setValueAttr($value, $data)
  122. {
  123. switch ($data['type']) {
  124. case 'array':
  125. $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
  126. break;
  127. }
  128. return $value;
  129. }
  130. /**
  131. * 获取器,选项
  132. *
  133. * @param string|array $value
  134. * @param array $data
  135. * @return array
  136. */
  137. public function getStoreRangeAttr($value, $data)
  138. {
  139. return $this->attrFormatJson($value, $data, 'store_range');
  140. }
  141. /**
  142. * 获取器,返回的格式
  143. *
  144. * @param string|array $value
  145. * @param array $data
  146. * @return array
  147. */
  148. public function getValueAttr($value, $data)
  149. {
  150. $value = $value ?: ($data['value'] ?? null);
  151. switch ($data['type']) {
  152. case 'array':
  153. $value = $this->attrFormatJson($value, $data, 'value', true);
  154. break;
  155. case 'boolean':
  156. $value = intval($value) ? 1 : 0;
  157. break;
  158. case 'int':
  159. $value = intval($value);
  160. break;
  161. case 'float':
  162. $value = floatval($value);
  163. break;
  164. }
  165. return config_show($value, $data);
  166. }
  167. }