| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- // +----------------------------------------------------------------------
- // | EasyAdmin
- // +----------------------------------------------------------------------
- // | PHP交流群: 763822524
- // +----------------------------------------------------------------------
- // | 开源协议 https://mit-license.org
- // +----------------------------------------------------------------------
- // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
- // +----------------------------------------------------------------------
- namespace app\common\service;
- use app\common\model\SystemConfig;
- use utils\RedisCache;
- /**
- * 系统配置服务 by wes
- * Class SystemConfigService
- * @package app\common\service
- */
- class SystemConfigService
- {
- protected static $instance = null;
- protected $model = null;
- public function __construct()
- {
- $this->model = new SystemConfig();
- }
- /**
- * 静态化入口
- * @return static|null
- */
- public static function make()
- {
- if(!self::$instance){
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 按分组获取配置信息
- * @param $groupKey 分组
- * @return array|mixed
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getConfigByGroup($groupKey)
- {
- $cacheKey = "caches:config:group:{$groupKey}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $where = ['group'=> $groupKey];
- $data = $this->model->where($where)->column('id,name,group,value,remark','name');
- if($data){
- RedisCache::set($cacheKey, $data, rand(10,20));
- }
- return $data;
- }
- /**
- * 按配置名获取配置信息或者值
- * @param $name 配置名
- * @param int $type 返回类型:1-返回值,2-返回列信息
- * @param string $groupKey 分组
- * @return SystemConfig|array|mixed|\think\Model|null
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getConfigByName($name, $type=1, $groupKey='')
- {
- $cacheKey = "caches:config:info:{$name}_{$type}{$groupKey}";
- $data = RedisCache::get($cacheKey);
- if($data){
- return $data;
- }
- $where = ['name'=> $name];
- if($groupKey){
- $where['group'] = $groupKey;
- }
- if($type == 1){
- $data = $this->model->where($where)->value('value');
- }else{
- $data = $this->model->where($where)->field('id,name,group,value,remark')->find();
- }
- if($data){
- RedisCache::set($cacheKey, $data, rand(10,20));
- }
- return $data;
- }
- }
|