ShopConfigService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace app\common\Service;
  3. use app\common\model\ShopConfig as ShopConfigModel;
  4. use app\common\Enum\ChannelEnum;
  5. use think\Cache;
  6. use think\Db;
  7. /**
  8. * 商城配置服务类
  9. */
  10. class ShopConfigService
  11. {
  12. /**
  13. * 获取配置组
  14. *
  15. * @param string $code
  16. * @param boolean $cache
  17. * @return array
  18. */
  19. public static function getConfigs($code, $cache = true)
  20. {
  21. // 从缓存中获取
  22. if ($cache) {
  23. $config = cache("config:{$code}");
  24. if (empty($config)) {
  25. $config = self::getConfigs($code, false);
  26. }
  27. }
  28. // 从数据库中查找
  29. if (!$cache) {
  30. $row = ShopConfigModel::where('code', $code)->find();
  31. if(!$row) return null;
  32. if($row['type'] !== 'group') {
  33. $config = $row->value;
  34. }else {
  35. $config = [];
  36. $list = ShopConfigModel::where('parent_code', $code)->select();
  37. foreach ($list as &$row) {
  38. if ($row['type'] === 'group') {
  39. $row->value = self::getConfigs($row->code, false);
  40. } else {
  41. cache("config:{$row->code}", $row->value);
  42. }
  43. $config[self::getShortCode($row->toArray())] = $row->value;
  44. }
  45. }
  46. // 设置配置缓存
  47. cache("config:{$code}", $config);
  48. }
  49. return $config;
  50. }
  51. /**
  52. * 获取单一配置项
  53. *
  54. * @param string $code
  55. * @param boolean $cache
  56. * @return mixed
  57. */
  58. public static function getConfigField($code, $cache = true)
  59. {
  60. // 从缓存中获取
  61. if ($cache) {
  62. $config = cache("config:{$code}");
  63. if (empty($config)) {
  64. $config = self::getConfigField($code, false);
  65. }
  66. }
  67. // 从数据库中查找
  68. if (!$cache) {
  69. $config = ShopConfigModel::where('code', $code)->value('value');
  70. // 设置配置缓存
  71. cache("config:{$code}", $config);
  72. }
  73. return $config;
  74. }
  75. private static function getShortCode($config)
  76. {
  77. if (!empty($config['parent_code'])) {
  78. return str_replace("{$config['parent_code']}.", "", $config['code']);
  79. }
  80. return $config['code'];
  81. }
  82. /**
  83. * 更新配置
  84. *
  85. * @param string $code
  86. * @param array $configParams
  87. * @return void
  88. */
  89. public static function setConfigs(string $code, array $configParams)
  90. {
  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 = ShopConfigModel::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. }