SystemConfigService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace crmeb\services;
  3. use app\admin\model\system\SystemConfig;
  4. /**
  5. * 获取系统配置服务类
  6. * Class SystemConfigService
  7. * @package service
  8. */
  9. class SystemConfigService
  10. {
  11. protected static $configList = null;
  12. const CACHE_SYSTEM = 'system_config';
  13. /**
  14. * 初始化
  15. */
  16. protected static function init()
  17. {
  18. $confingFun = function () {
  19. return self::getAll();
  20. };
  21. try {
  22. self::$configList = CacheService::get(self::CACHE_SYSTEM, $confingFun);
  23. } catch (\Throwable $e) {
  24. try {
  25. self::$configList = $confingFun();
  26. } catch (\Exception $e) {
  27. self::$configList = [];
  28. }
  29. }
  30. }
  31. /**获取系统配置
  32. * @param $key
  33. * @return mixed|null
  34. */
  35. public static function config($key)
  36. {
  37. self::init();
  38. if (self::$configList === null) self::$configList = self::getAll();
  39. return self::$configList[$key] ?? null;
  40. }
  41. /**
  42. * 获取单个配置效率更高
  43. * @param $key
  44. * @param string $default
  45. * @param bool $isCaChe 是否获取缓存配置
  46. * @return bool|mixed|string
  47. */
  48. public static function get($key, $default = '', bool $isCaChe = false)
  49. {
  50. if ($isCaChe) {
  51. try {
  52. return SystemConfig::getConfigValue($key);
  53. } catch (\Throwable $e) {
  54. return $default;
  55. }
  56. }
  57. self::init();
  58. return self::$configList[$key] ?? $default;
  59. }
  60. /**
  61. * 获取多个配置
  62. * @param array $keys 示例 [['appid','1'],'appkey']
  63. * @param bool $isCaChe 是否获取缓存配置
  64. * @return array
  65. */
  66. public static function more(array $keys, bool $isCaChe = false)
  67. {
  68. self::init();
  69. $callable = function () use ($keys) {
  70. try {
  71. $list = SystemConfig::getMore($keys);
  72. return self::getDefaultValue($keys, $list);
  73. } catch (\Exception $e) {
  74. return self::getDefaultValue($keys);
  75. }
  76. };
  77. if ($isCaChe)
  78. return $callable();
  79. try {
  80. return self::getDefaultValue($keys, self::$configList);
  81. } catch (\Throwable $e) {
  82. return $callable();
  83. }
  84. }
  85. /**
  86. * 获取默认配置
  87. * @param array $keys
  88. * @return array
  89. */
  90. public static function getDefaultValue(array $keys, array $configList = [])
  91. {
  92. $value = [];
  93. foreach ($keys as $val) {
  94. if (is_array($val)) {
  95. $k = $val[0] ?? '';
  96. $v = $val[1] ?? '';
  97. } else {
  98. $k = $val;
  99. $v = '';
  100. }
  101. $value[$k] = $configList[$k] ?? $v;
  102. }
  103. return $value;
  104. }
  105. /**获取全部配置不缓存
  106. * @return array
  107. */
  108. public static function getAll()
  109. {
  110. return SystemConfig::getAllConfig() ?: [];
  111. }
  112. }