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:sysconfig: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, 7200); } 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:sysconfig:info:{$name}_{$type}{$groupKey}"; $data = RedisCache::get($cacheKey); if(!RedisCache::exists($cacheKey)) { $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(); $data = $data? $data->toArray() : []; } if ($data) { RedisCache::set($cacheKey, $data, 3 * 24 * 3600); } } return $data; } /** * 按配置名获取配置信息或者值 * @param array $names 配置名多个数组 * @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 getConfigByNames(array $names, $type=1, $groupKey='') { $cacheKey = "caches:sysconfig:info:{$type}_{$groupKey}_".md5(json_encode($names)); $data = RedisCache::get($cacheKey); if($data){ return $data; } $where = []; if($groupKey){ $where['group'] = $groupKey; } if($type == 1){ $data = $this->model->whereIn('name',$names)->where($where)->column('value','name'); }else{ $data = $this->model->whereIn('name',$names)->where($where)->column('id,name,group,value,remark','name'); } if($data){ RedisCache::set($cacheKey, $data, 3*24*3600); } return $data; } }