123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace app\common\model;
- use app\common\library\Redis;
- use think\Cache;
- use think\Log;
- use think\Model;
- class Ophost extends Model
- {
- /**
- * 三方平台默认业务域名 + 平台ID
- */
- const CACHE_KEY_DEFAULT_PLATFORM = 'OPH:D:';
- /**
- * 微信业务域名host列表
- */
- const CACHE_KEY_HOST_LIST = 'OPHOSTLIST';
- /**
- * 业务域名缓存 + 业务域名ID
- */
- const CACHE_KEY_ID = 'OPH:';
- /**
- * 业务域名缓存 + 业务域名host
- * @var
- */
- const CACHE_KEY_HOST = 'OPH:H:';
- // 表名
- protected $table = 'ophost';
-
- // 自动写入时间戳字段
- protected $autoWriteTimestamp = false;
- // 定义时间戳字段名
- protected $createTime = false;
- protected $updateTime = false;
-
- // 追加属性
- protected $append = [
- 'isdefault_text',
- 'status_text'
- ];
- /**
- * 删除全部缓存
- * @param $id
- * @param null $host
- * @param null $platform_id
- */
- public function delCacheAll($id,$host = null,$platform_id = null){
- $redis = Redis::instance();
- //删除ID
- $redis->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);
- }
- }
|