SystemConfigService.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. /** 获取系统配置服务类
  15. * Class SystemConfigService
  16. * @package service
  17. */
  18. class SystemConfigService
  19. {
  20. const CACHE_SYSTEM = 'system_config';
  21. /**
  22. * 获取单个配置效率更高
  23. * @param string $key
  24. * @param $default
  25. * @param bool $isCaChe 是否获取缓存配置
  26. * @return bool|mixed|string
  27. */
  28. public static function get(string $key, $default = '', bool $isCaChe = true)
  29. {
  30. $callable = function () use ($key) {
  31. return app()->make(SystemConfigServices::class)->getConfigValue($key);
  32. };
  33. try {
  34. if ($isCaChe) {
  35. return CacheService::remember(self::CACHE_SYSTEM . '_' . $key, $callable);
  36. }
  37. return $callable();
  38. } catch (\Throwable $e) {
  39. return $default;
  40. }
  41. }
  42. /**
  43. * 获取多个配置
  44. * @param array $keys 示例 [['appid','1'],'appkey']
  45. * @param bool $isCaChe 是否获取缓存配置
  46. * @return array
  47. */
  48. public static function more(array $keys, bool $isCaChe = true)
  49. {
  50. $callable = function () use ($keys) {
  51. return Arr::getDefaultValue($keys, app()->make(SystemConfigServices::class)->getConfigAll($keys));
  52. };
  53. try {
  54. if ($isCaChe){
  55. return CacheService::remember(self::CACHE_SYSTEM . '_' . md5(implode(',', $keys)), $callable);
  56. }
  57. return $callable();
  58. } catch (\Throwable $e) {
  59. return Arr::getDefaultValue($keys);
  60. }
  61. }
  62. }