SystemConfigService.php 3.0 KB

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