123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- 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",'<br/>', $v['options']);
- }
- RedisService::set($cacheKey, $result, 30);
- }
- return $result;
- }
- /**
- * 获取单个配置
- * @param $code
- * @param string $default 默认值
- * @return array|mixed|string
- */
- public function getConfigByCode($code, $default=''){
- $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? $datas : $default;
- }
- }
|