ConfigService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace services\common;
  3. use Yii;
  4. use yii\db\ActiveQuery;
  5. use yii\helpers\Json;
  6. use common\enums\AppEnum;
  7. use common\models\common\Config;
  8. use common\models\common\ConfigValue;
  9. /**
  10. * 配置管理
  11. *
  12. * Class ConfigService
  13. * @package services\common
  14. */
  15. class ConfigService
  16. {
  17. /**
  18. * @var array
  19. */
  20. protected $configCache = [];
  21. /**
  22. * 返回配置名称
  23. *
  24. * @param string $name 字段名称
  25. * @param bool $noCache true 不从缓存读取 false 从缓存读取
  26. * @param string $merchant_id
  27. * @return string|null
  28. */
  29. public function config($name, $noCache = false, $merchant_id = '')
  30. {
  31. $info = $this->configAll($noCache, $merchant_id);
  32. return isset($info[$name]) ? trim($info[$name]) : null;
  33. }
  34. /**
  35. * 返回配置名称
  36. *
  37. * @param bool $noCache true 不从缓存读取 false 从缓存读取
  38. * @return array|bool|mixed
  39. */
  40. public function configAll($noCache = false, $merchant_id = '')
  41. {
  42. $merchant_id = 0;
  43. $app_id = AppEnum::BACKEND;
  44. $info = $this->baseConfigAll($app_id, $merchant_id, $noCache);
  45. return !empty($info) ? $info : [];
  46. }
  47. /**
  48. * 返回总后台配置名称
  49. *
  50. * @param string $name 字段名称
  51. * @param bool $noCache true 不从缓存读取 false 从缓存读取
  52. * @param string $merchant_id
  53. * @return string|null
  54. */
  55. public function backendConfig($name, $noCache = false)
  56. {
  57. // 获取缓存信息
  58. $info = $this->baseConfigAll(AppEnum::BACKEND, 0, $noCache);
  59. return isset($info[$name]) ? trim($info[$name]) : null;
  60. }
  61. /**
  62. * 返回总后台配置
  63. *
  64. * @param bool $noCache true 不从缓存读取 false 从缓存读取
  65. * @return array|bool|mixed
  66. */
  67. public function backendConfigAll($noCache = false)
  68. {
  69. $info = $this->baseConfigAll(AppEnum::BACKEND, 0, $noCache);
  70. return !empty($info) ? $info : [];
  71. }
  72. /**
  73. * 获取当前商户配置
  74. *
  75. * @param $name
  76. * @param bool $noCache
  77. * @return string|null
  78. */
  79. public function merchantConfig($name, $noCache = false, $merchant_id = '')
  80. {
  81. !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
  82. !$merchant_id && $merchant_id = 1;
  83. // 获取缓存信息
  84. $info = $this->baseConfigAll(AppEnum::MERCHANT, $merchant_id, $noCache);
  85. return isset($info[$name]) ? trim($info[$name]) : null;
  86. }
  87. /**
  88. * 获取当前商户的全部配置
  89. *
  90. * @param bool $noCache
  91. * @return array|bool|mixed
  92. */
  93. public function merchantConfigAll($noCache = false, $merchant_id = '')
  94. {
  95. !$merchant_id && $merchant_id = Yii::$app->services->merchant->getId();
  96. !$merchant_id && $merchant_id = 1;
  97. $info = $this->baseConfigAll(AppEnum::MERCHANT, $merchant_id, $noCache);
  98. return !empty($info) ? $info : [];
  99. }
  100. /**
  101. * @param string $app_id
  102. * @param int $merchant_id
  103. * @param bool $noCache
  104. * @return array|mixed
  105. */
  106. protected function baseConfigAll($app_id, $merchant_id, $noCache)
  107. {
  108. $cacheKey = 'config:' . $app_id . ':' . $merchant_id;
  109. if ($noCache == false && !empty($this->config[$cacheKey])) {
  110. return $this->configCache[$cacheKey];
  111. }
  112. if ($noCache == true || !($this->configCache[$cacheKey] = Yii::$app->cache->get($cacheKey))) {
  113. $this->configCache[$cacheKey] = [];
  114. $config = $this->findByNames($app_id, $merchant_id);
  115. foreach ($config as $row) {
  116. $this->configCache[$cacheKey][$row['name']] = $row['value']['data'] ?? $row['default_value'];
  117. }
  118. Yii::$app->cache->set($cacheKey, $this->configCache[$cacheKey], 60);
  119. }
  120. return $this->configCache[$cacheKey];
  121. }
  122. /**
  123. * @param string $app_id
  124. * @param int $merchant_id
  125. * @param array $data
  126. * @return array|bool|mixed
  127. */
  128. public function updateAll(string $app_id, int $merchant_id, array $data)
  129. {
  130. $config = $this->findByNames($app_id, $merchant_id, array_keys($data));
  131. /** @var Config $item */
  132. foreach ($config as $item) {
  133. $val = $data[$item['name']] ?? '';
  134. /** @var ConfigValue $model */
  135. $model = $item->value ?? new ConfigValue();
  136. $model->merchant_id = $merchant_id;
  137. $model->config_id = $item->id;
  138. $model->app_id = $item->app_id;
  139. $model->data = is_array($val) ? Json::encode($val) : trim($val);
  140. $model->save();
  141. }
  142. if ($app_id === AppEnum::BACKEND) {
  143. return $this->backendConfigAll(true);
  144. }
  145. return $this->merchantConfigAll(true, $merchant_id);
  146. }
  147. /**
  148. * @param $name
  149. * @param $app_id
  150. * @return array|\yii\db\ActiveRecord|null
  151. */
  152. public function findSaveByName($name, $app_id, $data = [])
  153. {
  154. $config = $this->findByName($name, $app_id);
  155. if (empty($config)) {
  156. $model = new Config();
  157. $model = $model->loadDefaultValues();
  158. $model->attributes = $data;
  159. $model->save();
  160. }
  161. }
  162. /**
  163. * @param $name
  164. * @param $app_id
  165. * @return array|\yii\db\ActiveRecord|null
  166. */
  167. public function findByName($name, $app_id)
  168. {
  169. return Config::find()
  170. ->where(['name' => $name, 'app_id' => $app_id])
  171. ->one();
  172. }
  173. /**
  174. * @param $app_id
  175. * @param $merchant_id
  176. * @param $names
  177. * @return array|\yii\db\ActiveRecord[]
  178. */
  179. public function findByNames($app_id, $merchant_id, $names = [])
  180. {
  181. return Config::find()
  182. ->filterWhere(['in', 'name', $names])
  183. ->andWhere(['app_id' => $app_id])
  184. ->with([
  185. 'value' => function (ActiveQuery $query) use ($merchant_id, $app_id) {
  186. return $query->andWhere(['app_id' => $app_id, 'merchant_id' => $merchant_id]);
  187. }
  188. ])
  189. ->all();
  190. }
  191. }
粤ICP备19079148号