SystemConfigService.php 3.0 KB

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