SystemConfigService.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/23
  6. */
  7. namespace service;
  8. use app\admin\model\system\SystemConfig;
  9. use service\CacheService as Cache;
  10. /** 获取系统配置服务类
  11. * Class SystemConfigService
  12. * @package service
  13. */
  14. class SystemConfigService
  15. {
  16. protected static $configList = null;
  17. /**获取系统配置
  18. * @param $key
  19. * @return mixed|null
  20. */
  21. public static function config($key)
  22. {
  23. if(self::$configList === null) self::$configList = self::getAll();
  24. return isset(self::$configList[$key]) ? self::$configList[$key] : null;
  25. }
  26. /**获取单个配置效率更高
  27. * @param $key
  28. * @return bool|mixed
  29. */
  30. public static function get($key)
  31. {
  32. $cacheName = 'config_'.$key;
  33. $config = Cache::get($cacheName);
  34. if($config) return $config;
  35. $config = SystemConfig::getValue($key);
  36. Cache::set($cacheName,$config);
  37. return $config;
  38. }
  39. /** 获取多个配置
  40. * @param $keys ',' 隔开
  41. * @return array
  42. */
  43. public static function more($keys,$update = false)
  44. {
  45. $keys = is_array($keys) ? implode(',',$keys) : $keys;
  46. $cacheName = 'more_'.$keys;
  47. $config = Cache::get($cacheName);
  48. if($config && !$update) return $config;
  49. $config = SystemConfig::getMore($keys);
  50. if(!$config) exception('对应的配置不存在!');
  51. Cache::set($cacheName,$config);
  52. return $config;
  53. }
  54. /**获取全部配置
  55. * @return array
  56. */
  57. public static function getAll()
  58. {
  59. $cacheName = 'config_all';
  60. $config = Cache::get($cacheName);
  61. if($config) return $config;
  62. $config = SystemConfig::getAllConfig()?:[];
  63. if(!$config) exception('对应的配置不存在!');
  64. Cache::set($cacheName,$config);
  65. return $config;
  66. }
  67. }