SystemConfigService.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\service;
  12. use app\common\model\SystemConfig;
  13. use utils\RedisCache;
  14. /**
  15. * 系统配置服务 by wes
  16. * Class SystemConfigService
  17. * @package app\common\service
  18. */
  19. class SystemConfigService
  20. {
  21. protected static $instance = null;
  22. protected $model = null;
  23. public function __construct()
  24. {
  25. $this->model = new SystemConfig();
  26. }
  27. /**
  28. * 静态化入口
  29. * @return static|null
  30. */
  31. public static function make()
  32. {
  33. if(!self::$instance){
  34. self::$instance = new static();
  35. }
  36. return self::$instance;
  37. }
  38. /**
  39. * 按分组获取配置信息
  40. * @param $groupKey 分组
  41. * @return array|mixed
  42. * @throws \think\db\exception\DataNotFoundException
  43. * @throws \think\db\exception\DbException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. */
  46. public function getConfigByGroup($groupKey)
  47. {
  48. $cacheKey = "caches:config:group:{$groupKey}";
  49. $data = RedisCache::get($cacheKey);
  50. if($data){
  51. return $data;
  52. }
  53. $where = ['group'=> $groupKey];
  54. $data = $this->model->where($where)->column('id,name,group,value,remark','name');
  55. if($data){
  56. RedisCache::set($cacheKey, $data, rand(10,20));
  57. }
  58. return $data;
  59. }
  60. /**
  61. * 按配置名获取配置信息或者值
  62. * @param $name 配置名
  63. * @param int $type 返回类型:1-返回值,2-返回列信息
  64. * @param string $groupKey 分组
  65. * @return SystemConfig|array|mixed|\think\Model|null
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function getConfigByName($name, $type=1, $groupKey='')
  71. {
  72. $cacheKey = "caches:config:info:{$name}_{$type}{$groupKey}";
  73. $data = RedisCache::get($cacheKey);
  74. if($data){
  75. return $data;
  76. }
  77. $where = ['name'=> $name];
  78. if($groupKey){
  79. $where['group'] = $groupKey;
  80. }
  81. if($type == 1){
  82. $data = $this->model->where($where)->value('value');
  83. }else{
  84. $data = $this->model->where($where)->field('id,name,group,value,remark')->find();
  85. }
  86. if($data){
  87. RedisCache::set($cacheKey, $data, rand(10,20));
  88. }
  89. return $data;
  90. }
  91. }