| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- 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: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;
- }
- }
|