SystemConfigService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use app\services\system\config\SystemConfigServices;
  13. use crmeb\utils\Arr;
  14. use think\facade\Config;
  15. use think\facade\Db;
  16. /** 获取系统配置服务类
  17. * Class SystemConfigService
  18. * @package service
  19. */
  20. class SystemConfigService
  21. {
  22. /**
  23. * 缓存前缀字符
  24. */
  25. const CACHE_SYSTEM = 'system_config';
  26. /**
  27. * 过期时间
  28. */
  29. const EXPIRE_TIME = 30 * 24 * 3600;
  30. /**
  31. * @var int
  32. */
  33. protected static $relationId = 0;
  34. /**
  35. * @var int
  36. */
  37. protected static $type = 0;
  38. /**
  39. * 获取配置缓存前缀
  40. * @return string
  41. */
  42. public static function getTag()
  43. {
  44. return Config::get('cache.stores.redis.tag_prefix') . 'cahce_' . self::CACHE_SYSTEM;
  45. }
  46. /**
  47. * @param int $storeId
  48. */
  49. public static function setStore(int $storeId)
  50. {
  51. self::$relationId = $storeId;
  52. self::$type = 1;
  53. }
  54. /**
  55. * @param int $supplier
  56. */
  57. public static function setSupplier(int $supplier)
  58. {
  59. self::$relationId = $supplier;
  60. self::$type = 2;
  61. }
  62. /**
  63. * 获取单个配置效率更高
  64. * @param $key
  65. * @param string $default
  66. * @param bool $isCaChe 是否获取缓存配置
  67. * @return bool|mixed|string
  68. */
  69. public static function get(string $key, $default = '', bool $isCaChe = false)
  70. {
  71. $cacheName = self::CACHE_SYSTEM . ':' . $key . (self::$type ? '_' . self::$type : '') . (self::$relationId ? '_' . self::$relationId : '');
  72. $type = self::$type;
  73. $relationId = self::$relationId;
  74. $callable = function () use ($key, $type, $relationId) {
  75. event('get.config');
  76. /** @var SystemConfigServices $service */
  77. $service = app()->make(SystemConfigServices::class);
  78. return $service->getConfigValue($key, $type, $relationId);
  79. };
  80. try {
  81. if ($isCaChe) {
  82. return $callable();
  83. }
  84. $value = CacheService::redisHandler(self::getTag())->remember($cacheName, $callable, self::EXPIRE_TIME);
  85. self::$relationId = 0;
  86. self::$type = 0;
  87. return $value;
  88. } catch (\Throwable $e) {
  89. return $default;
  90. }
  91. }
  92. /**
  93. * 获取多个配置
  94. * @param array $keys 示例 [['appid','1'],'appkey']
  95. * @param bool $isCaChe 是否获取缓存配置
  96. * @return array
  97. */
  98. public static function more(array $keys, bool $isCaChe = false)
  99. {
  100. $cacheName = self::CACHE_SYSTEM . ':' . md5(implode(',', $keys) . (self::$type ? '_' . self::$type : '') . (self::$relationId ? '_' . self::$relationId : ''));
  101. $type = self::$type;
  102. $relationId = self::$relationId;
  103. $callable = function () use ($keys, $type, $relationId) {
  104. /** @var SystemConfigServices $service */
  105. $service = app()->make(SystemConfigServices::class);
  106. return Arr::getDefaultValue($keys, $service->getConfigAll($keys, $type, $relationId));
  107. };
  108. try {
  109. if ($isCaChe)
  110. return $callable();
  111. $value = CacheService::redisHandler(self::getTag())->remember($cacheName, $callable, self::EXPIRE_TIME);
  112. self::$relationId = 0;
  113. self::$type = 0;
  114. return $value;
  115. } catch (\Throwable $e) {
  116. return Arr::getDefaultValue($keys);
  117. }
  118. }
  119. /**
  120. * 清空配置缓存
  121. * @return bool|void
  122. */
  123. public static function clear()
  124. {
  125. try {
  126. return CacheService::redisHandler(self::getTag())->clear();
  127. } catch (\Throwable $e) {
  128. \think\facade\Log::error('清空配置缓存失败:原因:' . $e->getMessage());
  129. return false;
  130. }
  131. }
  132. }