123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <?php
- namespace app\service;
- use laytp\library\UploadDomain;
- use laytp\traits\Error;
- use think\facade\Cache;
- use think\facade\Config;
- /**
- * 系统配置服务器实现者
- * Class Auth
- * @package app\service
- */
- class Conf
- {
- use Error;
- // 是否使用redis,如果不想使用redis,修改此处为false即可
- protected $useRedis = true;
- // 数据库连接句柄
- protected $db = null;
- //判断是否配置了redis
- protected function hasRedis(){
- $redisConf = Config::get('cache');
- if(isset($redisConf['stores']['redis']['type']) && $this->useRedis){
- return true;
- }
- return false;
- }
- /**
- * 通过一个完整的key,获取配置信息
- * @param $wholeKey
- * @param $defaultValue
- * @return bool|mixed|string
- */
- public function get($wholeKey, $defaultValue='')
- {
- global $_W,$_GPC;
- if(!empty($_GPC['uniacid'])){
- $uniacid = $_GPC['uniacid'];
- }else{
- $uniacid = $_W['uniacid'];
- }
- if($this->hasRedis()){
- $redis = Cache::store('redis')->handler();
- if($redis){
- $value = $redis->hget($wholeKey, 'value');
- $formType = $redis->hget($wholeKey, 'form_type');
- if($formType == 'array') return json_decode($value, JSON_UNESCAPED_UNICODE);
- return $value ? $value : $defaultValue;
- }
- }
- list($group, $key) = $this->getGroupKey($wholeKey);
- $conf = \app\model\Conf::where(['group'=>$group, 'key'=>$key,'uniacid'=>$uniacid])->findOrEmpty()->toArray();
- if($conf){
- $value = $conf['value'];
- $formType = $conf['form_type'];
- if($formType == 'array') return json_decode($value, JSON_UNESCAPED_UNICODE);
- if($formType == 'upload'){
- $fileInfo = UploadDomain::multiJoin($value);
- $return[$key] = $value;
- if($fileInfo){
- $return[$key.'_path'] = $fileInfo['path'];
- $return[$key.'_filename'] = $fileInfo['filename'];
- }else{
- $return[$key.'_path'] = '';
- $return[$key.'_filename'] = '';
- }
- return $return;
- }
- return $value ? $value : $defaultValue;
- }else{
- return '';
- }
- }
- /**
- * 通过一个完整的key,设置配置信息
- * @param $wholeKey
- * @param $value
- * @return bool
- */
- public function set($wholeKey, $value)
- {
- global $_W,$_GPC;
- if(!empty($_GPC['uniacid'])){
- $uniacid = $_GPC['uniacid'];
- }else{
- $uniacid = $_W['uniacid'];
- }
- if(is_array($value)){
- $value = json_encode($value, JSON_UNESCAPED_UNICODE);
- }
- list($group, $key) = $this->getGroupKey($wholeKey);
- $id = \app\model\Conf::where(['group'=>$group, 'key'=>$key,'uniacid'=>$uniacid])->value('id');
- if($id){
- \app\model\Conf::where('id', '=', $id)->save(['group' => $group, 'key' => $key,'uniacid'=>$uniacid]);
- }else{
- \app\model\Conf::insert(['group' => $group, 'key' => $key, 'value' => $value,'uniacid'=>$uniacid]);
- }
- if($this->hasRedis()){
- $redis = Cache::store('redis')->handler();
- $redis->set($wholeKey, $value);
- }
- return true;
- }
- public function del($group, $key)
- {
- \app\model\Conf::where(['group' => $group, 'key' => $key])->delete();
- if($this->hasRedis()){
- $redis = Cache::store('redis')->handler();
- $redis->del($group . $key);
- }
- return ;
- }
- /**
- * 通过数组,设置配置信息
- * @param $array
- * @return bool
- */
- public function groupSet($array)
- {
- global $_W;
- foreach($array as $item){
- $item['value'] = is_array($item['value']) ? json_encode($item['value'], JSON_UNESCAPED_UNICODE) : $item['value'];
- $id = \app\model\Conf::where(['group'=>$item['group'], 'key'=>$item['key'],'uniacid' => $_W['uniacid']])->value('id');
- if($id){
- \app\model\Conf::where('id', '=', $id)->save($item);
- }else{
- \app\model\Conf::create($item);
- }
- if($this->hasRedis()){
- $redis = Cache::store('redis')->handler();
- $hashKey = $item['group'] . '.' . $item['key'];
- $redis->hset($hashKey, 'group', $item['group']);
- $redis->hset($hashKey, 'key', $item['key']);
- $redis->hset($hashKey, 'value', $item['value']);
- $redis->hset($hashKey, 'form_type', $item['form_type']);
- }
- }
- return true;
- }
- /**
- * 通过配置分组名称,获取整个分组的信息
- * @param $group
- * @param $onlyMysql boolean 是否仅从数据库取配置
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function groupGet($group, $onlyMysql=false)
- {
- global $_W,$_GPC;
- if(!empty($_GPC['uniacid'])){
- $where = ['group'=>$group,'uniacid' => $_GPC['uniacid']];
- }else{
- $where = ['group'=>$group,'uniacid' => $_W['uniacid']];
- }
- $return = [];
- $items = [];
- if(!$onlyMysql){
- if($this->hasRedis()){
- $redis = Cache::store('redis')->handler();
- $keys = $redis->keys($group.'*');
- foreach($keys as $key){
- $items[$key] = $redis->hGetAll($key);
- }
- if(!$items){
- $items = \app\model\Conf::where($where)->select()->toArray();
- }
- }else{
- $items = \app\model\Conf::where($where)->select()->toArray();
- }
- }else{
- $items = \app\model\Conf::where($where)->select()->toArray();
- }
- foreach ($items as $k => $v) {
- if ($v['form_type'] === 'array') {
- $array = json_decode($v['value'], true);
- if(!$array){
- $return[$v['key']] = [""=>""];
- }else{
- $return[$v['key']] =$array;
- }
- } elseif($v['form_type'] === 'upload') {
- $fileInfo = UploadDomain::multiJoin($v['value']);
- if($fileInfo){
- $return[$v['key']] = $fileInfo['id'];
- $return[$v['key'].'_path'] = $fileInfo['path'];
- $return[$v['key'].'_filename'] = $fileInfo['filename'];
- }else{
- $return[$v['key']] = '';
- $return[$v['key'].'_path'] = '';
- $return[$v['key'].'_filename'] = '';
- }
- } else {
- $return[$v['key']] = $v['value'];
- }
- }
- return $return;
- }
- /**
- * 通过完整的key,获取到分组名称和key值
- * @param $wholeKey
- * @return array|boolean
- */
- protected function getGroupKey($wholeKey)
- {
- $arr = explode('.', $wholeKey);
- if(!$arr || count($arr) == 1){
- $this->setError('请输入一个完整的key,一个完整的key必须包含至少一个.号');
- return false;
- }
- $key = $arr[count($arr) - 1];
- $group = substr($wholeKey, 0 , strrpos($wholeKey, '.'));
- return [$group, $key];
- }
- }
|