del(self::CACHE_KEY_ID.$id); //Host没有时取ID的 if(empty($host)){ $host = $this->where('id',$id)->value('host'); } //平台Id没有事取ID的 if(empty($platform_id)){ $platform_id = $this->where('id',$id)->value('platform_id'); } //删除Host $redis->del(self::CACHE_KEY_HOST.$host); $redis->del(self::CACHE_KEY_HOST_LIST); //删除平台ID $redis->del(self::CACHE_KEY_DEFAULT_PLATFORM.$platform_id); } public function delCacheByHosts(){ $redis = Redis::instance(); $redis->del(self::CACHE_KEY_HOST_LIST); } /** * 删除Ophost平台缓存 * @param null $platform_id */ public function delCacheByPlatformId($platform_id){ $redis = Redis::instance(); //删除平台ID $redis->del(self::CACHE_KEY_DEFAULT_PLATFORM.$platform_id); } //获取所有可用的业务域名 全部业务域名 public function getHosts(){ $redis = Redis::instance(); if($redis->exists(self::CACHE_KEY_HOST_LIST)){ $res = \GuzzleHttp\json_decode($redis->get(self::CACHE_KEY_HOST_LIST),true); }else{ $res = collection($this->column('host'))->toArray(); $redis->setex(self::CACHE_KEY_HOST_LIST,86400,json_encode($res)); } return $res; } public function getInfo($platform_id,$id=null){ if(empty($platform_id)) { return null; } $redis = Redis::instance(); if(empty($id)){ //获取当前平台下默认的业务域名信息 $modulename = request()->module(); if($modulename == 'admin' && PHP_SAPI != 'cli') { //如果是后台域名 $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find(); if(!$arr){ $arr = $this->where(['platform_id'=>$platform_id,'status'=>1])->find(); } if($arr){ $arr = $arr->toArray(); } }else{ $key = self::CACHE_KEY_DEFAULT_PLATFORM.$platform_id; if($redis->exists($key)){ $arr = $redis->hgetall($key); }else{ $arr = $this->where(['platform_id'=>$platform_id,'isdefault'=>'1','status'=>1])->find(); if(!$arr){ $arr = $this->where(['platform_id'=>$platform_id,'status'=>1])->find(); } if($arr){ $arr = $arr->toArray(); $redis->hmset($key,$arr); $redis->expire($key,86400); } } } }else{ //获取指定的域名信息 $modulename = request()->module(); if($modulename == 'admin' && PHP_SAPI != 'cli') { //如果是后台域名 $arr = $this->where('id',$id)->find(); if($arr){ $arr = $arr->toArray(); } }else{ $arr = $this->getInfoById($id); } } return $arr; } /** * 根据host获取业务域名信息 * @param $host * @return mixed */ public function getInfoByHost($host){ $redis = Redis::instance(); $key = self::CACHE_KEY_HOST.$host; $ophost = null; if($ophost = Cache::get($key)){ if(!is_array($ophost)){ $ophost = json_decode($ophost,true); } $time = date('Y-m-d H:i:s',time()); Log::write("Time: {$time} Read: ".json_encode($ophost,JSON_UNESCAPED_UNICODE),'FileCache'); return $ophost; }else{ if($redis->exists($key)){ $ophost = $redis->hgetall($key); Cache::set($key,$ophost,5); $time = date('Y-m-d H:i:s',time()); Log::write("Time: {$time} Set: ".json_encode($ophost,JSON_UNESCAPED_UNICODE),'FileCache'); }else{ if($ophost = $this->where('host',$host)->find()){ $ophost = $ophost->toArray(); $redis->hmset($key,$ophost); $redis->expire($key,3600); } } } return $ophost; } public function getInfoById($id){ $redis = Redis::instance(); $key = self::CACHE_KEY_ID.$id; $ophost = null; if (Cache::has($key)) { Log::info("获取缓存:$key filecache命中"); $ophost = Cache::get($key); } if (!$ophost) { if ($redis->exists($key)) { $ophost = $redis->hgetall($key); Log::info("获取缓存:$key redis命中"); Cache::set($key, $ophost, 10); } } if (!$ophost) { $ophost = $this->where('id', $id)->find(); if ($ophost) { Log::info("获取缓存:$key mysql命中"); $ophost = $ophost->toArray(); $redis->hmset($key, $ophost); $redis->expire($key, 3600); } } return $ophost; } public function getListByPlatformId($platform_id=null,$isReturnPlatformID = false){ if(!$platform_id){ return false; } $map['status'] = 1; $list = $this->whereIn('platform_id',$platform_id)->where($map)->select(); $ophostList = []; if($list){ foreach ($list as $v){ $platform_name = model('Platform')->where('id',$v['platform_id'])->value('name'); if($isReturnPlatformID){ array_push($ophostList,['id'=>$v['id'],'platform_id'=>$v['platform_id'],'text'=>$platform_name.'---'.$v['host']."--{$v['p_desc']}"]); }else{ $ophostList[$v['id']] = $platform_name.'---'.$v['host']."--{$v['p_desc']}"; } } } return $ophostList; } public function getHostListByPlatformId($platform_id=null){ if(!$platform_id){ return false; } $map['status'] = 1; $map['allow_changed'] = 1; $list = $this->whereIn('platform_id', explode(',', $platform_id))->where($map)->select(); Log::sql('[ ChangeDomain ] [ getHostListByPlatformId] ' . $this->getLastSql()); $ophostList = []; if($list){ foreach ($list as $v) { array_push($ophostList, ['ophost_id'=>$v['id'], 'ophost_host'=>$v['host']]); } } return $ophostList; } /** * 获取平台支付列表 * @param $platform_id * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public function getListFormatByPlatformId($platform_id = null){ $result = []; $map['status'] = 1; if($platform_id){ $map['platform_id'] = ['in',$platform_id]; } if($list = $this->where($map)->select()){ foreach($list as $val){ $platform_name = model('Platform')->where('id',$val['platform_id'])->value('name'); array_push($result,['id'=>$val['id'],'platform_id'=>$val['platform_id'],'text'=>$platform_name.'--'.$val['host'].'--'.$val['p_desc']]); } } return $result; } public function getIsdefaultList() { return ['0' => __('Isdefault 0'),'1' => __('Isdefault 1')]; } public function getStatusList() { return ['0' => __('Status 0'),'1' => __('Status 1')]; } public function getAllowChangedList() { return ['0' => __('AllowChanged0'),'1' => __('AllowChanged1')]; } public function getIsdefaultTextAttr($value, $data) { $value = $value ? $value : $data['isdefault']; $list = $this->getIsdefaultList(); return isset($list[$value]) ? $list[$value] : ''; } public function getStatusTextAttr($value, $data) { $value = $value ? $value : $data['status']; $list = $this->getStatusList(); return isset($list[$value]) ? $list[$value] : ''; } public function platform() { return $this->belongsTo('Platform', 'platform_id')->setEagerlyType(0); } }