// +---------------------------------------------------------------------- namespace App\Services; use App\Models\ConfigModel; /** * 配置管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ConfigService * @package App\Services */ class ConfigService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * ConfigService constructor. */ public function __construct() { $this->model = new ConfigModel(); } /** * 静态入口 * @return ConfigService|null */ public static function make(){ if(!self::$instance){ self::$instance = new ConfigService(); } return self::$instance; } /** * 获取配置列表 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function getList() { $param = request()->all(); // 查询条件 $map = []; // 配置分组ID $configgroupId = getter($param, "configgroupId", 0); if ($configgroupId) { $map[] = ['config_group_id', '=', $configgroupId]; } // 配置标题 $title = getter($param, "title"); if ($title) { $map[] = ['name', 'title', "%{$title}%"]; } $list = $this->model->getList($map, [['sort', 'asc']]); return message("操作成功", true, $list); } /** * 获取分组配置 * @param $groupId * @return mixed */ public function getConfigByGroup($groupId){ $cacheKey = "caches:config:groups:{$groupId}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1]) ->select('title','code','value') ->orderBy('sort','asc') ->get() ->keyBy('code'); $datas = $datas? $datas->toArray() : []; if($datas){ RedisService::set($cacheKey, $datas, 30); } return $datas; } /** * 获取配置选项值 * @param $groupId * @return array|mixed */ public function getConfigOptionByGroup($groupId){ $cacheKey = "caches:config:groups:value_{$groupId}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1]) ->select('title','code','type','value') ->orderBy('sort','asc') ->get() ->keyBy('code'); $datas = $datas? $datas->toArray() : []; $result = []; if($datas){ foreach ($datas as $k => $v){ $result[$k] = $v['type'] == 'image' && $v['value']? get_image_url($v['value']) : $v['value']; } RedisService::set($cacheKey, $result, 30); } return $result; } /** * 获取配置选项值 * @param $groupId * @return array|mixed */ public function getConfigOptionTextByGroup($groupId){ $cacheKey = "caches:config:groups:text_{$groupId}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model::where(['config_group_id'=> $groupId, 'status'=> 1]) ->select('title','code','options') ->orderBy('sort','asc') ->get() ->keyBy('code'); $datas = $datas? $datas->toArray() : []; $result = []; if($datas){ foreach ($datas as $k => $v){ $result[$k] = str_replace("\n",'
', $v['options']); } RedisService::set($cacheKey, $result, 30); } return $result; } /** * 获取单个配置 * @param $groupId * @return mixed */ public function getConfigByCode($code){ $cacheKey = "caches:config:code:{$code}"; $datas = RedisService::get($cacheKey); if($datas){ return $datas; } $datas = $this->model::where(['code'=> $code, 'status'=> 1]) ->orderBy('sort','asc') ->value('value'); if($datas){ RedisService::set($cacheKey, $datas, 30); } return $datas; } }