SystemConfigService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\common\service;
  3. use app\common\model\SystemConfig;
  4. use utils\RedisCache;
  5. /**
  6. * 系统配置服务 by wes
  7. * Class SystemConfigService
  8. * @package app\common\service
  9. */
  10. class SystemConfigService
  11. {
  12. protected static $instance = null;
  13. protected $model = null;
  14. public function __construct()
  15. {
  16. $this->model = new SystemConfig();
  17. }
  18. /**
  19. * 静态化入口
  20. * @return static|null
  21. */
  22. public static function make()
  23. {
  24. if(!self::$instance){
  25. self::$instance = new static();
  26. }
  27. return self::$instance;
  28. }
  29. /**
  30. * 按分组获取配置信息
  31. * @param $groupKey 分组
  32. * @return array|mixed
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function getConfigByGroup($groupKey)
  38. {
  39. $cacheKey = "caches:sysconfig:group:{$groupKey}";
  40. $data = RedisCache::get($cacheKey);
  41. if($data){
  42. return $data;
  43. }
  44. $where = ['group'=> $groupKey];
  45. $data = $this->model->where($where)->column('id,name,group,value,remark','name');
  46. if($data){
  47. RedisCache::set($cacheKey, $data, 7200);
  48. }
  49. return $data;
  50. }
  51. /**
  52. * 按配置名获取配置信息或者值
  53. * @param $name 配置名
  54. * @param int $type 返回类型:1-返回值,2-返回列信息
  55. * @param string $groupKey 分组
  56. * @return SystemConfig|array|mixed|\think\Model|null
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function getConfigByName($name, $type=1, $groupKey='')
  62. {
  63. $cacheKey = "caches:sysconfig:info:{$name}_{$type}{$groupKey}";
  64. $data = RedisCache::get($cacheKey);
  65. if(!RedisCache::exists($cacheKey)) {
  66. $where = ['name' => $name];
  67. if ($groupKey) {
  68. $where['group'] = $groupKey;
  69. }
  70. if ($type == 1) {
  71. $data = $this->model->where($where)->value('value');
  72. } else {
  73. $data = $this->model->where($where)->field('id,name,group,value,remark')->find();
  74. $data = $data? $data->toArray() : [];
  75. }
  76. if ($data) {
  77. RedisCache::set($cacheKey, $data, 3 * 24 * 3600);
  78. }
  79. }
  80. return $data;
  81. }
  82. /**
  83. * 按配置名获取配置信息或者值
  84. * @param array $names 配置名多个数组
  85. * @param int $type 返回类型:1-返回值,2-返回列信息
  86. * @param string $groupKey 分组
  87. * @return SystemConfig|array|mixed|\think\Model|null
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function getConfigByNames(array $names, $type=1, $groupKey='')
  93. {
  94. $cacheKey = "caches:sysconfig:info:{$type}_{$groupKey}_".md5(json_encode($names));
  95. $data = RedisCache::get($cacheKey);
  96. if($data){
  97. return $data;
  98. }
  99. $where = [];
  100. if($groupKey){
  101. $where['group'] = $groupKey;
  102. }
  103. if($type == 1){
  104. $data = $this->model->whereIn('name',$names)->where($where)->column('value','name');
  105. }else{
  106. $data = $this->model->whereIn('name',$names)->where($where)->column('id,name,group,value,remark','name');
  107. }
  108. if($data){
  109. RedisCache::set($cacheKey, $data, 3*24*3600);
  110. }
  111. return $data;
  112. }
  113. }