// +---------------------------------------------------------------------- namespace App\Services; use App\Models\ConfigModel; use wxkxklmyt\Scws; /** * 配置管理-服务类 * @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, $default = 0) { $cacheKey = "caches:config:code:{$code}"; $datas = RedisService::get($cacheKey); if ($datas) { return $datas ? $datas : $default; } $datas = $this->model::where(['code' => $code, 'status' => 1]) ->orderBy('sort', 'asc') ->value('value'); if ($datas) { RedisService::set($cacheKey, $datas, 30); } return $datas ? $datas : $default; } /** * 获取客服回复内容 * @param $keyword * @return array|mixed */ public function getContentByKw($keyword) { $cacheKey = "caches:config:info:kw_" . md5($keyword); $data = RedisService::get($cacheKey); if ($data) { return $data; } $scws = new Scws(); $kws = $scws->scws($keyword, 3, false); $data = $this->model->where(['config_group_id' => 9, 'status' => 1, 'mark' => 1])->where(function ($query) use ($kws, $keyword) { if (count($kws) > 1) { foreach ($kws as $kw) { $kw = trim($kw); $query->where('title', 'like', "%{$kw}%") ->orWhere('options', 'like', "%{$kw}%"); } } else { $query->where('title', 'like', "%{$keyword}%") ->orWhere('options', 'like', "%{$keyword}%"); } })->whereNotNull('value')->orderByRaw("RAND()")->value('value'); if($data){ RedisService::set($cacheKey, $data, rand(3,5)); } return $data; } }