thinkphp 自封装redis操作类

<?php
namespace tongji;
use think\Db;

// 键名:
// 1.{shop}:{2022-07-09}:{45c48cce2e2d7fbdea1afc51c7c6ad26}
// 2.{shop}:{45c48cce2e2d7fbdea1afc51c7c6ad26_adinfo}
// 3.{shop}:{2022-07-09}:{45c48cce2e2d7fbdea1afc51c7c6ad26_jump}

class Record
{
    protected $siteKey;      //站点key
    protected $ttl;
    protected $redis;

    public function __construct($dt=null,$id)
    {
     
        $md5=$id ;
        $this->md5key =$id;
          
        $this->ttl=3*24*60*60;//三天
        $this->redis = new \Redis();
        if(!$this->redis->connect('127.0.0.1', 6379)) throw new  Exception("redis连接失败");
        $this->redis->auth('xxxxx') ;
        $this->redis->select(14); // 选择数据库
        $fix='shop';
        if(!$dt){
            $dt=date('Y-m-d');
        }
           
          $this->siteKey="{{$fix}}:{{$dt}}:{{$md5}}";//访问列表    设置几个key键名常量 并不写入读取操作
          $this->siteJumpKey="{{$fix}}:{{$dt}}:{{$md5}_jump}";//访问count统计次数
          $this->groupinfokeyname="{{$fix}}:{{$md5}_groupinfo}";
    //      $this->siteTestKey="{{$fix}}:{{$dt}}:{{$md5}_test}";
       
        $this->adinfokey="{{$fix}}:{{$md5}_adinfo}";
    }
    
    
    
    
    public function getAK($key){
        $re = $this->redis->get($key);
         
        if (!$re){//不存在 则写入redis
            return false;
        }else{
            return $re;
        }
    }
    
    public function setAk($key,$val,$timeout=null){
        if($timeout==null){
            $this->redis->set($key,$val);
        }else{
            $this->redis->set($key,$val,$timeout);
        }
    }
    
    
    
      public function get_login_session_key($openid){ 
        $key="{shop}:{session_key}:{session_key_".$openid."}";
       // echo $openid;die;
        $re = $this->redis->get($key);
        // dump($re);
        if (!$re){//不存在 则写入redis
            return false;
        }else{
            return $re;
        }
    }
    
    
        public function get_app(){ 
        $key="{shop}:{appid}:{appid_".$this->md5key."}";
       // echo $openid;die;
        $re = $this->redis->get($key);
        // dump($re);
        if (!$re){//不存在 则写入redis
        $this->set_app();
            return Db::name('wxapp')->where("appid",$this->md5key)->find();
        }else{
            return json_decode($re,true);
        }
    }
    
     public function set_app(){ 
        $key="{shop}:{appid}:{appid_".$this->md5key."}";
       // echo $openid;die;
       $val=Db::name('wxapp')->where("appid",$this->md5key)->find();  //项目信息
        $re = $this->redis->set($key,json_encode($val));
     
    }
    
    public function get_appsecret($appid){ 
        $key="{shop}:{appid_key}:{appid_".$appid."}";
       // echo $openid;die;
        $re = $this->redis->get($key);
        // dump($re);
        if (!$re){//不存在 则写入redis
        $this->set_appsecret($appid);
            return Db::name('wxapp')->where("appid",$appid)->value('appsecret');
        }else{
            return $re;
        }
    }
    
     public function set_appsecret($appid){ 
        $key="{shop}:{appid_key}:{appid_".$appid."}";
       // echo $openid;die;
       $val=Db::name('wxapp')->where("appid",$appid)->value('appsecret');  //项目信息
        $re = $this->redis->set($key,$val);
     
    }
    
    public function set_login_session_key($openid,$session_key,$timeout=200000){//20万秒 别超过三天
        $key="{shop}:{session_key}:{session_key_".$openid."}";
        $this->redis->set($key,$session_key,$timeout);
        
    }
    
     
    
    
      
    //likai  getsite_capturename  获取redis里面的截屏事件的cookie变量名
     public function getsite_capturename($valuename)
    {
        $key = 'Settingvalue'.$valuename;
        $re = $this->redis->get($key);
         
        if (!$re){//不存在 则写入redis
            $re = Db::name("config")->where("name='".$valuename."'")->value("value") ;
            $this->redis->set($key,$re);
        }
         return $re;
    }
    
     //likai  getsite_capturename  获取redis里面的截屏cookie过滤总开关
     public function getsite_eable_capture()
    {
        $key = 'SettingvalueEableCapture';
        $re = $this->redis->get($key);
         
        if (!$re){//不存在 则写入redis
            $re = Db::name("config")->where("name='site_EableCapture'")->value("value") ;
            $this->redis->set($key,$re);
        }
         return $re;
    }
    
    
     //likai  getsite_capturename  获取redis里面的截屏cookie过滤总开关
     public function resetsite_eable_capture()
    {
        $key = 'SettingvalueEableCapture';
        $re = $this->redis->del($key);
        $re = Db::name("config")->where("name='site_EableCapture'")->value("value") ;
        $this->redis->set($key,$re);
        return true;
    }
    
    likai checkbssid 检查bssid是否存在黑名单
    //  public function checkbssid($bssid)
    // {
    //     $key = 'Settingvaluebssidlist';
    //     $re = $this->allListRedis($key);
     
    //     if (!$re){//不存在 则写入redis
    //       $re= $this->resetbssidlist();
            
    //     }
        
    //     foreach($re as$k=>$v){
    //         if($bssid==$v){
    //             return true;
    //         }
    //     }
    //     // var_dump($re) ;
    //      return false;
    // }
    
    // //likai resetbssidlist 重新设置bssid列表缓存
    //  public function resetbssidlist()
    // {
    //     $key = 'Settingvaluebssidlist';
    //     $this->redis->del($key);
        
    //      $arr=Db::name("bssidlist")->distinct(true)->field("BSSID")->select(); // 头条这套系统 我单独写了个bssid黑名单表 
    //       $newarr= $this->unique_2d_array_by_key( $arr ,"BSSID"   );
    //   // dump($arr);
    //     foreach($newarr as $k=>$v){
    //         if(strlen($v['BSSID'])>14){
    //             $this->redis->lPush($key,json_encode($v['BSSID'],true));
    //         }
              
    //     }
          
    //     $re = $this->allListRedis($key); //list类型读取
        
    //      return $re;
    // }
    
    
public function checkbssid2($bssid)
    {
        $key = '{shop}:{black_bssid_list}';
        if (!$this ->redis->exists($key.":{ISSET}")){//不存在 则写入redis
             //echo "ISSET没有";
            $this->resetbssidlist2();
        }else{
            //echo "ISSET有,继续查找键名:".$key.":{".$bssid."}";
            if ($this ->redis->exists($key.":{".$bssid."}")){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
    
    
     
    
    public function resetbssidlist2() {
        $keyPattern = '{shop}:{black_bssid_list}*'; // 使用 * 作为通配符
        $keys = $this->redis->keys($keyPattern);    // 获取所有匹配的键
 
        // 删除匹配的键
        foreach ($keys as $key) {
            $this->redis->del($key);
        }
    
        $key = '{shop}:{black_bssid_list}';
        $this->redis->lPush($key.":{ISSET}", "");
        $arr = Db::name("bssidlist")->distinct(true)->field("BSSID")->select(); // 这里进行去重操作
        $newarr = $this->unique_2d_array_by_key($arr, "BSSID");
        foreach ($newarr as $k => $v) {
            if (strlen($v['BSSID']) > 5) {
                $this->redis->lPush($key.":{".$v['BSSID']."}", "");
            }
        }
    }
    
 
    
    
    
    ///
    
     //ip黑名单//
       //likai checkbssid 检查ip是否存在黑名单列表中
    //  public function checkblackiplist($ip)
    // {
        
    //     $key = 'Settingvalueblackiplist';
    //     $re = $this ->redis->lRange($key,0,-1);
     
    //     if (!$re){//不存在 则写入redis
    //       $re= $this->resetblackiplist();
    //     }
    //      $sip=explode(".",$ip);
          
    //     foreach($re as$k=>$v){
    //         $dip= explode(".",$v);
            
    //      if (array_key_exists(0,$dip) && array_key_exists(1,$dip) && array_key_exists(2,$dip) ){
    //             if( $dip[0].$dip[1].$dip[2].$dip[3] == $sip[0].$sip[1].$sip[2].$dip[3]  ){
    //               return true;
    //              }
    //          } 
                    
            
    //     }
    //      return false;
    // }
    
    
     
    
    //likai resetbssidlist 重新设置ip黑名单列表缓存
    //  public function resetblackiplist()
    // {
    //   $key = 'Settingvalueblackiplist';
    //     $this->redis->del($key);
       
    //   $arr=Db::name("ipblacklist")->distinct(true)->field("ip")->select(); //这里进行去重操作
    //     $newarr= $this->unique_2d_array_by_key(  $arr ,"ip"   );
    //       foreach($newarr as $k=>$v){
    //         if(strlen($v['ip'])>5){
    //              $this->redis->lPush($key,$v['ip']);
    //         }
    //     }

    //      $re = $this ->redis->lRange($key,0,-1);//list类型读取
    //      return $re;
    // }
    
    
    
    
    public function checkblackiplist2($ip)
    {
        $key = '{shop}:{black_ip_list}';
            
      //  $keys = $this->redis->keys($key . '*'); // 获取所有以 {shop}:{black_ip_list} 开头的键名
      //  dump($keys);die;
        // if (!empty($keys)) {
        //     $this->redis->del($keys); // 删除所有匹配的键名
        // }
    
        if (!$this ->redis->exists($key.":{ISSET}")){//不存在 则写入redis
            // echo "ISSET没有";
            $this->resetblackiplist2();
        }else{
            //echo "ISSET有,继续查找键名:".$key.":{".$ip."}";
            if ($this ->redis->exists($key.":{".$ip."}")){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
    
    
     
    
    //likai resetbssidlist 重新设置ip黑名单列表缓存
     public function resetblackiplist2() {
        $keyPattern = '{shop}:{black_ip_list}*'; // 使用 * 作为通配符
        $keys = $this->redis->keys($keyPattern);  // 获取所有以 {shop}:{black_ip_list} 开头的键名
    
        // 删除匹配的键
        foreach ($keys as $keyToDelete) {
            $this->redis->del($keyToDelete);
        }
    
        $key = '{shop}:{black_ip_list}';
        $this->redis->lPush($key . ":{ISSET}", "");
    
        $arr = Db::name("ipblacklist")->distinct(true)->field("ip")->select(); // 进行去重操作
        $newarr = $this->unique_2d_array_by_key($arr, "ip");
        foreach ($newarr as $k => $v) {
            if (strlen($v['ip']) > 5) {
                $this->redis->lPush($key . ":{" . $v['ip'] . "}", "");
            }
        }
    }

    
    
    
    
    public function checkwhiteiplist2($ip)
    {
        $key = '{shop}:{white_ip_list}';
            
        //$keys = $this->redis->keys($key . '*'); // 获取所有以 {shop}:{white_ip_list} 开头的键名
      //  dump($keys);die;
        // if (!empty($keys)) {
        //     $this->redis->del($keys); // 删除所有匹配的键名
        // }
    
        if (!$this ->redis->exists($key.":{ISSET}")){//不存在 则写入redis
            // echo "ISSET没有";
            $this->resetwhiteiplist2();
        }else{
            //echo "ISSET有,继续查找键名:".$key.":{".$ip."}";
            if ($this ->redis->exists($key.":{".$ip."}")){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }
    
    
     
    
    //likai resetbssidlist 重新设置ip黑名单列表缓存
     public function resetwhiteiplist2() {
        $keyPattern = '{shop}:{white_ip_list}*'; // 使用 * 作为通配符
        $keys = $this->redis->keys($keyPattern);  // 获取所有以 {shop}:{white_ip_list} 开头的键名
    
        // 删除匹配的键
        foreach ($keys as $keyToDelete) {
            $this->redis->del($keyToDelete);
        }
    
        $key = '{shop}:{white_ip_list}';
        $this->redis->lPush($key . ":{ISSET}", "");
    
        $arr = Db::name("ipwhitelist")->distinct(true)->field("ip")->select(); // 进行去重操作
        $newarr = $this->unique_2d_array_by_key($arr, "ip");
        foreach ($newarr as $k => $v) {
            if (strlen($v['ip']) > 5) {
                $this->redis->lPush($key . ":{" . $v['ip'] . "}", "");
            }
        }
    }

    
    
    
    
    
    
     public function resetshopcache()
    {
        $key = '{shop}';
        $keys = $this->redis->keys($key . '*'); // 获取所有以 {shop}:{black_ip_list} 开头的键名
        if (!empty($keys)) {
            $this->redis->del($keys); // 删除所有匹配的键名
        }
    
       echo "已清理";
    }
    
    
    
     /**
 * 1.删除二维数组中相同项的数据,一般用于数据库查询结果中相同记录的去重操作
 * 2.重置一下二维数组的索引
 * @param array $_2d_array 二维数组,类似:
 *     $tmpArr = array(
 *         array('id' => 1, 'value' => '15046f5de5bb708e'),
 *         array('id' => 1, 'value' => '15046f5de5bb708e'),
 *     );
 * @param string $unique_key 表示上述数组的 "id" 键,或者 "value" 键
 * @return mixed
 */
   public function unique_2d_array_by_key($_2d_array, $unique_key)
    {
        $tmp_key[] = array();
        foreach ($_2d_array as $key => &$item) {
            if (is_array($item) && isset($item[$unique_key])) {
                if (in_array($item[$unique_key], $tmp_key)) {
                    unset($_2d_array[$key]);
                } else {
                    $tmp_key[] = $item[$unique_key];
                }
            }
        }
        //重置一下二维数组的索引
        return array_slice($_2d_array, 0, count($_2d_array), false);
    }
    
    
    
    
    
    
    
    
    
     /*
     * 查询列表所有数据
     * */
    public function allListRedis($key){
        $newArr = [];
        $list = $this ->redis->lRange($key,0,-1); //查询所有
        foreach ($list as $val){
            array_push($newArr,json_decode($val,true));
        }
        return $newArr;
    }
    
    
    
    
    
    
    
    
    
     // 读取adpage配置
    public function getadinfo()
    {
        $arr=$this->redis->hGetAll($this->adinfokey);
     
          //先判断是否存在redis缓存
        if(!$arr ){
            $arr = Db::name('adpage')->where("md5id='".$this->md5key."'")->find();   
            if($arr){
                $this->setadinfo($arr);
            }else{
                   return false;
            }
        } 
        return $arr;
    }
     // 设置adpage配置
    public function setadinfo($adinfo)
    {
        $this->redis->hMset($this->adinfokey,$adinfo);
        $this->redis->expire($this->adinfokey,$this->ttl);
    }
     // 删除adpage配置
    public function deladinfo()
    {
        $this->redis->del($this->adinfokey);
        $this->redis->del($this->siteJumpKey);
        $this->redis->del($this->siteKey);
    }
    
    
    
    
    
    
    
    
     // 读取项目首页数据
    public function getgroupinfo($goodsid=0,$is_sh=0)
    {

       
        if($goodsid>0){//既有goodsid 也有md5key的 说明之前进来过
             $arr = Db::name('goods')->where("id='".$goodsid."'")->find();  //项目信息
           //  unset($arr['md5key']);
           $wxapp=[];
           $appinfo=[];
            
        //   var_dump($arr);die;
           if($this->md5key){
                 $wxapp = Db::name('wxappgroup')->where("md5key='".$this->md5key."'")->find();  //项目信息
                 $appinfo=Db::name('wxapp')->where("appid='".$wxapp['appid']."'")->find();  //项目信息
           }
      
            //  var_dump($appinfo);die;
            if($arr && !$appinfo && !$wxapp){
                
            //  var_dump(111);die;
                 $arr['tmplIds']=json_encode([]); 

                
                $arr['headimg']=json_encode(explode(",",$arr['headimg']));
                $arr['ldheadimg']=json_encode(explode(",",$arr['ldheadimg']));
                 
                 
                 
                  $ld_videolist_arr=explode(",",$arr['ld_video']);
                foreach($ld_videolist_arr as $k=>$v){
                    $ld_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];
                }
                
                $sh_videolist_arr=explode(",",$arr['sh_video']);
                foreach($sh_videolist_arr as $k=>$v){
                   $sh_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];
                }
                $arr['ld_video']=$arr['ld_video']==""?json_encode([]):  json_encode($ld_videolist_arr);
                $arr['sh_video']=$arr['sh_video']==""?json_encode([]):  json_encode($sh_videolist_arr);
                
                
                $indeximglistarr=explode(",",$arr['indeximglist']);
                $indexresimg=[];
                foreach($indeximglistarr as$k2=>$v2){
                   $indexresimg[]=cdnurl($v2,true); 
                }
                $arr['indeximglist']=implode(",",$indexresimg);
                $arr['imglist']=json_encode(explode(",",$arr['imglist']));
              
                /落地页详情图处理
                 
                   
                $arr['ldimglist']=json_encode(explode(",",$arr['ldimglist']));
                
                $skulist=Db::name("goods_sku")->where("md5key = '".$arr['md5key']."'")->field("id,name,price,original_price,image,disabled")->select();
                // foreach($skulist as$kim=>$vim){
                //   $skulist[$kim]['image']=cdnurl($vim['image'],true);
                // }
                $arr['skulist']=json_encode($skulist);
                
                
                // $ldskulist=Db::name("goods_sku")->where("md5key = '".$arr['md5key']."'")->field("id,name,price,original_price,image,disabled")->select();
                // foreach($ldskulist as$ldkim=>$ldvim){
                //   $ldskulist[$ldkim]['image']=cdnurl($ldvim['image'],true);
                // }
                $arr['ldskulist']=$arr['skulist'];
                 
                 
                 
                 $arr['comment']="";
                 if($arr['comment_group']>0){
                     $comment=Db::name("wxvirtualcomment")->where("group_id",$arr['comment_group'])->order("thumbsup desc")->select();
                     foreach($comment as$kc=>$vc){
                       $comment[$kc]['headerimg']=cdnurl($vc['headerimg'],true);
                       $comment[$kc]['date']="1天前";
                       
                       $cmarr=explode(',',$vc['images']);
                    //   foreach($cmarr as$cmk=>$cmv){
                    //       $cmarr[$cmk]=cdnurl(trim($cmv),true);
                    //   }
                       
                        $comment[$kc]['images']=$cmarr;
                    }
                    $arr['comment']=json_encode($comment); 
                 } 
               
                
                
                $arr['wdj']="";
                if($arr['wdj_group']>0){
                    $wdj=Db::name("wxwdj")->where("group_id",$arr['wdj_group'])->order("viewnum desc")->select();
                     foreach($wdj as$wc=>$wc1){
                           $wdj[$wc]['anum']=Db::name("wxwdja")->where("wxwdj_id",$wc1['id'])->count();
                        unset($wdj[$wc]['id']);
                    }
                    $arr['wdj']=json_encode($wdj);  
                }
                
                
                
                $arr['kfimg']=cdnurl($arr['kfimg'],true);
                
                //强制改成只能在线支付1
                $arr['paytype']=1;
                $arr['yscont']=Db::name('wxapp')->where("appid",'ks701154168685025754')->value('yinsi_content');
                // $this->setgroupinfo($arr);
            //  file_put_contents("md5key".date("Y-m-d H:i:s_",time()).rand(10000,9999999).".txt",
            //         $arr['md5key'] ."_".$this->md5key  , 
            //         FILE_APPEND);
            // var_dump($arr);die;
       
            return $arr;
                
            }
           // dump($appinfo);die;
            if($arr && $appinfo && $wxapp){
                $arr['appid']=$appinfo['appid']; 
                $arr['appsecret']=$appinfo['appsecret'];
                $arr['original_id']=$appinfo['original_id'];
                $arr['mch_id']=$appinfo['mch_id'];
                $arr['mch_key']=$appinfo['mch_key'];
                $arr['certpem']=$appinfo['certpem'];
                $arr['keypem']=$appinfo['keypem'];
                $arr['create_coupon_merchant']=$appinfo['create_coupon_merchant'];
                $arr['send_coupon_merchant']=$appinfo['send_coupon_merchant'];
                $arr['serialNo']=$appinfo['serialNo'];
                // $arr['aaaaaaa']=$appinfo['aaaaaaa'];
                // $arr['aaaaaaa']=$appinfo['aaaaaaa'];
                
               $tmplIds=[];
                if($appinfo['isdingyue']){
                    if($appinfo['daifukuan']){
                    $tmplIds[]=$appinfo['daifukuan'];
                    }
                    if($appinfo['yifahuo']){
                        $tmplIds[]=$appinfo['yifahuo'];
                    }
                    if($appinfo['yizhifu']){
                        $tmplIds[]=$appinfo['yizhifu'];
                    }
                }
                $arr['tmplIds']=json_encode($tmplIds);
                
                $arr['headimg']=json_encode(explode(",",$arr['headimg']));
                $arr['ldheadimg']=json_encode(explode(",",$arr['ldheadimg']));
                
                
                // $imglistarr=explode(",",$arr['imglist']);
                // $resimg=[];
                // foreach($imglistarr as$k=>$v){
                //   $resimg[]='http://'.$_SERVER['HTTP_HOST'].$v; 
                // }
                
                $indeximglistarr=explode(",",$arr['indeximglist']);
                $indexresimg=[];
                foreach($indeximglistarr as$k2=>$v2){
                   $indexresimg[]=cdnurl($v2,true); 
                }
                $arr['indeximglist']=implode(",",$indexresimg);
                $arr['imglist']=json_encode(explode(",",$arr['imglist']));
              //$arr['hotkeywords']=json_decode($arr['hotkeywords'],true);
                /落地页详情图处理
                 $arr['ldimglist']=json_encode(explode(",",$arr['ldimglist']));
                //  echo( $arr['md5key']);die;
                
                
                
                
                
                $ld_videolist_arr=explode(",",$arr['ld_video']);
                foreach($ld_videolist_arr as $k=>$v){
                    $ld_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];
                }
                
                $sh_videolist_arr=explode(",",$arr['sh_video']);
                foreach($sh_videolist_arr as $k=>$v){
                   $sh_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];
                }
                 $arr['ld_video']=$arr['ld_video']==""?json_encode([]):  json_encode($ld_videolist_arr);
                $arr['sh_video']=$arr['sh_video']==""?json_encode([]):  json_encode($sh_videolist_arr);
                
                
                // $arr['sh_video']=json_encode(explode(",",$arr['sh_video']));
                // $arr['ld_video']=json_encode(explode(",",$arr['ld_video']));
                
                 
                
                
                
                
                $skulist=Db::name("goods_sku")->where("md5key = '".$arr['md5key']."'")->field("id,name,price,original_price,image,disabled")->select();
                // foreach($skulist as$kim=>$vim){
                //   $skulist[$kim]['image']=cdnurl($vim['image'],true);
                // }
                $arr['skulist']=json_encode($skulist);
                
                
                // $ldskulist=Db::name("goods_sku")->where("md5key = '".$arr['md5key']."'")->field("id,name,price,original_price,image,disabled")->select();
                // foreach($ldskulist as$ldkim=>$ldvim){
                //   $ldskulist[$ldkim]['image']=cdnurl($ldvim['image'],true);
                // }
                $arr['ldskulist']=$arr['skulist'];
                 
                
                  
                 $arr['comment']="";
                 if($arr['comment_group']>0){
                     $comment=Db::name("wxvirtualcomment")->where("group_id",$arr['comment_group'])->order("thumbsup desc")->select();
                     foreach($comment as$kc=>$vc){
                       $comment[$kc]['headerimg']=cdnurl($vc['headerimg'],true);
                       $comment[$kc]['date']="1天前";
                       
                       $cmarr=explode(',',$vc['images']);
                    //   foreach($cmarr as$cmk=>$cmv){
                    //       $cmarr[$cmk]=cdnurl(trim($cmv),true);
                    //   }
                       
                        $comment[$kc]['images']=$cmarr;
                    }
                    $arr['comment']=json_encode($comment); 
                 } 
               
                
                
                $arr['wdj']="";
                if($arr['wdj_group']>0){
                    $wdj=Db::name("wxwdj")->where("group_id",$arr['wdj_group'])->order("viewnum desc")->select();
                     foreach($wdj as$wc=>$wc1){
                        $wdj[$wc]['anum']=Db::name("wxwdja")->where("wxwdj_id",$wc1['id'])->count();
                        unset($wdj[$wc]['id']);
                    }
                    $arr['wdj']=json_encode($wdj);  
                }
                
                
                $arr['kfimg']=cdnurl($arr['kfimg'],true);
                
                // $this->setgroupinfo($arr);
            //  file_put_contents("md5key".date("Y-m-d H:i:s_",time()).rand(10000,9999999).".txt",
            //         $arr['md5key'] ."_".$this->md5key  , 
            //         FILE_APPEND);
            
            $arr['md5key']=$this->md5key; //将两个分组信息进行融合  这些字段用我的,其他的用王的   
            $arr['ad_type']=$wxapp['ad_type'];
            $arr['kpld']=$wxapp['kpld'];
            $arr['kpsh']=$wxapp['kpsh'];
            $arr['exarea']=$wxapp['exarea'];
            $arr['visitlimit']=$wxapp['visitlimit'];
            $arr['blackstatus']=$wxapp['blackstatus'];
            $arr['zzIsShow']=$wxapp['zzIsShow'];
            $arr['zzIsShowPath']=$wxapp['zzIsShowPath']; 
             $arr['zzIsShowPath1']=$wxapp['zzIsShowPath1'];
            $arr['zzIsShowPath2']=$wxapp['zzIsShowPath2'];
            $arr['zzIsShowPath3']=$wxapp['zzIsShowPath3'];
            $arr['zzIsShowPath4']=$wxapp['zzIsShowPath4'];
            $arr['zzIsShowPath5']=$wxapp['zzIsShowPath5'];
               $arr['zzIsShowConfig']=$this->getzz($wxapp);
                $arr['toutiao_event_type']=$wxapp['toutiao_event_type'];
               $arr['baidu_report_type']=$wxapp['baidu_report_type'];
                $arr['baidu_token']=$wxapp['baidu_token'];
               $arr['kuaishou_event_type']=$wxapp['kuaishou_event_type'];
               $arr['gdt_report_type']=$wxapp['gdt_report_type'];
        
            $arr['bbIsShow']=$wxapp['bbIsShow'];
             $arr['paytype']=$wxapp['paytype'];
            $arr['coupon_amount']=  $wxapp['coupon_amount'] ; 
            $arr['limit_amount']=$wxapp['limit_amount']; 
            $arr['stock_id']=$wxapp['stock_id'];
            
            
            return $arr;
                
            }else{
                   return false;
            }
           // dump(input());die;
        } ///非公共商品 取数据:
        
        
        if($is_sh==0){//广告商品 落地页 分支
       
        $arr=$this->redis->hGetAll($this->groupinfokeyname);
        
        if(!$arr){//&& $is_sh==1
            // dump("B".$is_sh);
          
           //  unset($arr['md5key']);
          $wxapp=[];
          $appinfo=[];
            
            
         $group = Db::name('wxappgroup')->where("md5key='".$this->md5key."'")->find();  //老项目信息
          
         if(!$group){
             return false;
         }
           $arr = Db::name('goods')->where("id='".$group['ld_goodsid']."'")->find();  //非公共商品 ,落地商品信息
           
          
          //dump($is_sh);
          $appinfo=Db::name('wxapp')->where("appid='".$group['appid']."'")->find();  //小程序信息
          //dump($appinfo);die;
          
            if($arr && $appinfo){
               $arr['appid']=$appinfo['appid']; 
               $arr['appsecret']=$appinfo['appsecret'];
               $arr['original_id']=$appinfo['original_id'];
               $arr['yscont']=$appinfo['yinsi_content'];
               $arr['mch_id']=$appinfo['mch_id'];
               $arr['mch_key']=$appinfo['mch_key'];
               $arr['certpem']=$appinfo['certpem'];
               $arr['keypem']=$appinfo['keypem'];
               $arr['create_coupon_merchant']=$appinfo['create_coupon_merchant'];
               $arr['send_coupon_merchant']=$appinfo['send_coupon_merchant'];
               $arr['serialNo']=$appinfo['serialNo'];
            $gmd5key=  $arr['md5key']; //group表有个md5key   goods表也有个。。别重复了。
            $arr['md5key']=$this->md5key; //将两个分组信息进行融合  这些字段用我的,其他的用王的   
            $arr['ad_type']=$group['ad_type'];
            $arr['kpld']=$group['kpld'];
            $arr['kpsh']=$group['kpsh'];
            $arr['exarea']=$group['exarea'];
            $arr['visitlimit']=$group['visitlimit'];
            $arr['blackstatus']=$group['blackstatus'];
            $arr['zzIsShow']=$group['zzIsShow'];
            
                $arr['toutiao_event_type']=$group['toutiao_event_type'];
               $arr['baidu_report_type']=$group['baidu_report_type'];
                $arr['baidu_token']=$group['baidu_token'];
               $arr['kuaishou_event_type']=$group['kuaishou_event_type'];
               $arr['gdt_report_type']=$group['gdt_report_type'];
        
            $arr['zzIsShowPath']=$group['zzIsShowPath'];
            $arr['zzIsShowPath1']=$group['zzIsShowPath1'];
            $arr['zzIsShowPath2']=$group['zzIsShowPath2'];
            $arr['zzIsShowPath3']=$group['zzIsShowPath3'];
            $arr['zzIsShowPath4']=$group['zzIsShowPath4'];
            $arr['zzIsShowPath5']=$group['zzIsShowPath5'];
           $arr['zzIsShowConfig']=$this->getzz($group);
            //dump(input());die;
    
            $arr['bbIsShow']=$group['bbIsShow'];
            $arr['coupon_amount']=    $group['coupon_amount'] ; 
            $arr['limit_amount']=$group['limit_amount']; 
            $arr['stock_id']=$group['stock_id'];
            $arr['paytype']=$group['paytype'];
            $arr['subreport']=$group['subreport'];
            $arr['profit_sharing']=$group['profit_sharing'];
            
            
                
                $tmplIds=[];
                if($appinfo['isdingyue']){
                    if($appinfo['daifukuan']){
                    $tmplIds[]=$appinfo['daifukuan'];
                    }
                    if($appinfo['yifahuo']){
                        $tmplIds[]=$appinfo['yifahuo'];
                    }
                    if($appinfo['yizhifu']){
                        $tmplIds[]=$appinfo['yizhifu'];
                    }
                }
                
                $arr['tmplIds']=json_encode($tmplIds);
               
                $arr['headimg']=json_encode(explode(",",$arr['headimg']));
                $arr['ldheadimg']=json_encode(explode(",",$arr['ldheadimg']));
                
                $ld_videolist_arr=explode(",",$arr['ld_video']);
                foreach($ld_videolist_arr as $k=>$v){
                    if(trim($v)!=""){
                       $ld_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];  
                    }
                }
                
                $sh_videolist_arr=explode(",",$arr['sh_video']);
                foreach($sh_videolist_arr as $k=>$v){
                     if(trim($v)!=""){
                       $sh_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];
                     }
                }
                $arr['ld_video']=$arr['ld_video']==""?json_encode([]):  json_encode($ld_videolist_arr);
                $arr['sh_video']=$arr['sh_video']==""?json_encode([]):  json_encode($sh_videolist_arr);
                
                $arr['imglist']=json_encode(explode(",",$arr['imglist']));
                
                $indeximglistarr=explode(",",$arr['indeximglist']);
                $indexresimg=[];
                foreach($indeximglistarr as$k2=>$v2){
                   $indexresimg[]=cdnurl($v2,true); 
                }
                $arr['indeximglist']=implode(",",$indexresimg);
                   
                $arr['ldimglist']=json_encode(explode(",",$arr['ldimglist']));
                 
                $skulist=Db::name("goods_sku")->where("md5key = '".$gmd5key."'")->field("id,name,price,original_price,image,disabled")->select();
                // foreach($skulist as$kim=>$vim){
                //   $skulist[$kim]['image']=cdnurl($vim['image'],true);
                // }
                
                $arr['skulist']=json_encode($skulist);
                
                // $ldskulist=$skulist;
                $arr['ldskulist']= $arr['skulist'];
                
                
                 $arr['comment']="";
                 if($arr['comment_group']>0){
                     $comment=Db::name("wxvirtualcomment")->where("group_id",$arr['comment_group'])->order("thumbsup desc")->select();
                    //  var_dump($comment);die;
                     foreach($comment as$kc=>$vc){
                       $comment[$kc]['headerimg']=$vc['headerimg'];
                       $comment[$kc]['date']="1天前";
                       
                       $cmarr=explode(',',$vc['images']);
                    //   foreach($cmarr as$cmk=>$cmv){
                    //       $cmarr[$cmk]=cdnurl(trim($cmv),true);
                    //   }
                       
                        $comment[$kc]['images']=$cmarr;
                    }
                    // var_dump($comment);die;
                    $arr['comment']=json_encode($comment); 
                 } 
               
                
                
                $arr['wdj']="";
                if($arr['wdj_group']>0){
                    $wdj=Db::name("wxwdj")->where("group_id",$arr['wdj_group'])->order("viewnum desc")->select();
                     foreach($wdj as$wc=>$wc1){
                         $wdj[$wc]['anum']=Db::name("wxwdja")->where("wxwdj_id",$wc1['id'])->count();
                        unset($wdj[$wc]['id']);
                    }
                    $arr['wdj']=json_encode($wdj);  
                }
                
                
                $arr['kfimg']=cdnurl($arr['kfimg'],true);
                 $arr['comment1']=$arr['comment'];
                //  dump($arr);die;    
                $this->setgroupinfo($arr);
                 
            }else{
                   return false;
            }
        } 
         }else{//输出审核商品内容
          
          //&& $is_sh==1
          // dump("B".$is_sh);die;
          //  unset($arr['md5key']);
          $wxapp=[];
          $appinfo=[];
            
           
          $group = Db::name('wxappgroup')->where("md5key='".$this->md5key."'")->find();  //老项目信息
          if(!$group){
             return false;
         }
           //dump($group['ld_goodsid']);die;
           // echo $this->md5key;die;
          $arr = Db::name('goods')->where("id='".$group['sh_goodsid']."'")->find();  //非公共商品 ,落地商品信息
          //dump($is_sh);
          $appinfo=Db::name('wxapp')->where("appid='".$group['appid']."'")->find();  //小程序信息
          
          
            if($arr && $appinfo){
                $arr['appid']=$appinfo['appid']; 
               $arr['appsecret']=$appinfo['appsecret'];
               $arr['original_id']=$appinfo['original_id'];
               $arr['mch_id']=$appinfo['mch_id'];
               $arr['mch_key']=$appinfo['mch_key'];
               $arr['certpem']=$appinfo['certpem'];
               $arr['keypem']=$appinfo['keypem'];
               $arr['create_coupon_merchant']=$appinfo['create_coupon_merchant'];
               $arr['send_coupon_merchant']=$appinfo['send_coupon_merchant'];
               $arr['serialNo']=$appinfo['serialNo'];
            $gmd5key=  $arr['md5key']; //group表有个md5key   goods表也有个。。别重复了。
            $arr['md5key']=$this->md5key; //将两个分组信息进行融合  这些字段用我的,其他的用王的   
            $arr['ad_type']=$group['ad_type'];
            $arr['kpld']=$group['kpld'];
            $arr['kpsh']=$group['kpsh'];
            $arr['exarea']=$group['exarea'];
            $arr['visitlimit']=$group['visitlimit'];
            $arr['blackstatus']=$group['blackstatus'];
            $arr['zzIsShow']=$group['zzIsShow'];
            $arr['zzIsShowPath']=$group['zzIsShowPath']; 
            $arr['bbIsShow']=$group['bbIsShow'];
            $arr['coupon_amount']=    $group['coupon_amount'] ; 
            $arr['limit_amount']=$group['limit_amount']; 
            $arr['stock_id']=$group['stock_id'];
             $arr['paytype']=$group['paytype'];
            $arr['subreport']=$group['subreport'];
            $arr['profit_sharing']=$group['profit_sharing'];
            
            
            $arr['zzIsShowPath']=$group['zzIsShowPath'];
            $arr['zzIsShowPath1']=$group['zzIsShowPath1'];
            $arr['zzIsShowPath2']=$group['zzIsShowPath2'];
            $arr['zzIsShowPath3']=$group['zzIsShowPath3'];
            $arr['zzIsShowPath4']=$group['zzIsShowPath4'];
            $arr['zzIsShowPath5']=$group['zzIsShowPath5'];
           $arr['zzIsShowConfig']=json_encode([]);//给返回一个空数组
            
            
                $tmplIds=[];
                if($appinfo['isdingyue']){
                    if($appinfo['daifukuan']){
                    $tmplIds[]=$appinfo['daifukuan'];
                    }
                    if($appinfo['yifahuo']){
                        $tmplIds[]=$appinfo['yifahuo'];
                    }
                    if($appinfo['yizhifu']){
                        $tmplIds[]=$appinfo['yizhifu'];
                    }
                }
                
                $arr['tmplIds']=json_encode($tmplIds);
               
                $arr['headimg']=json_encode(explode(",",$arr['headimg']));
                $arr['ldheadimg']=json_encode(explode(",",$arr['ldheadimg']));
                
                $ld_videolist_arr=explode(",",$arr['ld_video']);
                foreach($ld_videolist_arr as $k=>$v){
                    if(trim($v)!=""){
                       $ld_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];  
                    }
                }
                
                $sh_videolist_arr=explode(",",$arr['sh_video']);
                foreach($sh_videolist_arr as $k=>$v){
                     if(trim($v)!=""){
                       $sh_videolist_arr[$k]=['url'=>cdnurl($v,true)  ,'type'=>'mp4'];
                     }
                }
                $arr['ld_video']=$arr['ld_video']==""?json_encode([]):  json_encode($ld_videolist_arr);
                $arr['sh_video']=$arr['sh_video']==""?json_encode([]):  json_encode($sh_videolist_arr);
                
                $arr['imglist']=json_encode(explode(",",$arr['imglist']));
                
                $indeximglistarr=explode(",",$arr['indeximglist']);
                $indexresimg=[];
                foreach($indeximglistarr as$k2=>$v2){
                   $indexresimg[]=cdnurl($v2,true); 
                }
                $arr['indeximglist']=implode(",",$indexresimg);
                   
                $arr['ldimglist']=json_encode(explode(",",$arr['ldimglist']));
                 
                $skulist=Db::name("goods_sku")->where("md5key = '".$gmd5key."'")->field("id,name,price,original_price,image,disabled")->select();
                // foreach($skulist as$kim=>$vim){
                //   $skulist[$kim]['image']=cdnurl($vim['image'],true);
                // }
                
                $arr['skulist']=json_encode($skulist);
                
                // $ldskulist=$skulist;
                $arr['ldskulist']=$arr['skulist'];
                
                 
                 $arr['comment']="";
                 if($arr['comment_group']>0){
                     $comment=Db::name("wxvirtualcomment")->where("group_id",$arr['comment_group'])->order("thumbsup desc")->select();
                     foreach($comment as$kc=>$vc){
                       $comment[$kc]['headerimg']=cdnurl($vc['headerimg'],true);
                       $comment[$kc]['date']="1天前";
                       
                       $cmarr=explode(',',$vc['images']);
                    //   foreach($cmarr as$cmk=>$cmv){
                    //       $cmarr[$cmk]=cdnurl(trim($cmv),true);
                    //   }
                       
                        $comment[$kc]['images']=$cmarr;
                    }
                    $arr['comment']=json_encode($comment); 
                 } 
               
                
                
                $arr['wdj']="";
                if($arr['wdj_group']>0){
                    $wdj=Db::name("wxwdj")->where("group_id",$arr['wdj_group'])->order("viewnum desc")->select();
                     foreach($wdj as$wc=>$wc1){
                        $wdj[$wc]['anum']=Db::name("wxwdja")->where("wxwdj_id",$wc1['id'])->count();
                        unset($wdj[$wc]['id']);
                    }
                    $arr['wdj']=json_encode($wdj);  
                }
                
                
                
                $arr['kfimg']=cdnurl($arr['kfimg'],true);
                
               // $this->setgroupinfo($arr);//审核页面内容就别记缓存了,单独从数据库查出来返回
                
            }else{
                   return false;
            }
        
      
      }//输出审核商品内容结束
        
        
        
        // dump($arr);die;
         
        return $arr;
    }
    
    public function getzz($group){
        $return=[];
        if($group['zzIsShowPath']){
            $return[]=Db::name('redpocket')->where('image',$group['zzIsShowPath'])->find();
        }
        if($group['zzIsShowPath1']){
             $return[]=Db::name('redpocket')->where('image',$group['zzIsShowPath1'])->find();
        }
        if($group['zzIsShowPath2']){
            $return[]=Db::name('redpocket')->where('image',$group['zzIsShowPath2'])->find();
        }
        if($group['zzIsShowPath3']){
           $return[]=Db::name('redpocket')->where('image',$group['zzIsShowPath3'])->find();
        }
        if($group['zzIsShowPath4']){
           $return[]=Db::name('redpocket')->where('image',$group['zzIsShowPath4'])->find();
        }
       
       
       
       
        
        
         return json_encode($return);
    }
    
     // 设置项目所有数据
    public function setgroupinfo($arr)
    {
        
        $this->redis->hMset($this->groupinfokeyname,$arr);
        $this->redis->expire($this->groupinfokeyname,$this->ttl);
    }
    
    
    
    // 删除所有项目缓存
    public function delallgroup($miniproginfo=null)
    {
        $alllist=Db::name("wxappgroup")->field("md5key")->select();
        foreach($alllist as$k=>$v ){
             $key = '{shop}:{'.$v['md5key'].'_groupinfo}';
              $this->redis->del($key);
        }
        
    }
    
    
    


    // 记录访问明细
    public function writePv($result)
    {
        // 累加pv次数
         if($result['jump']=='是'){
            $this->redis->incr($this->siteJumpKey);
         }
           // echo json_encode($result);
        // 记录pv明细
        $this->redis->lPush($this->siteKey,json_encode($result));
        $this->redis->expire($this->siteKey,$this->ttl);
    }
    
    
     // 记录APP启动明细
    public function writePvOnLaunch($result)
    {
        // echo json_encode($result);
        unset($result['QUERY_STRING']);
       // echo $this->siteKey."Launch";
        $this->redis->lPush($this->siteKey."Launch",json_encode($result)); 
        $this->redis->expire($this->siteKey."Launch",$this->ttl);
    }
    
     // 记录被拦截记录
    public function insertintercept($result)
    {
        // echo json_encode($result);
        // unset($result['QUERY_STRING']);
        // $this->redis->lPush($this->siteKey."Insertintercept",json_encode($result));
        // $this->redis->expire($this->siteKey."Insertintercept",$this->ttl);
    }
   
   
   
   
   
    
     // 记录getdata记录
    public function InsertGetDataLog($result)
    {
        //  unset($result['QUERY_STRING']);
        $this->redis->lPush($this->siteKey.":{InsertGetDataLog}",json_encode($result));
        $this->redis->expire($this->siteKey.":{InsertGetDataLog}",$this->ttl);
        
        
        if(isset($result['visitcount'])){
            if($result['visitcount']<2){
               $this->redis->incr($this->siteKey.':{GetNum}'); 
               $this->redis->expire($this->siteKey.':{GetNum}',$this->ttl);
               
                if($result['is_sh']==1){
                      $this->redis->incr($this->siteKey.':{ShenHe}');
                      $this->redis->expire($this->siteKey.':{ShenHe}',$this->ttl);
                }else{
                      $this->redis->incr($this->siteKey.':{LuoDi}');
                      $this->redis->expire($this->siteKey.':{LuoDi}',$this->ttl);
                } 
            }
         
        }
       
       
        
    }
       //获取apps获取数据总数
    public function getdatalogscount(){
        //echo $this->siteKey."InsertGetDataLog";die;
        return $this->redis->lLen($this->siteKey."InsertGetDataLog");
    }
     //获取app获取数据
    public function getdatalogs($offset,$nums)
    {
       // echo $this->siteKey."InsertGetDataLog";die;
        return $this->redis->lRange($this->siteKey."InsertGetDataLog",$offset,$offset+$nums);
    }
    
    
    
       //获取apps获取数据总数
    public function getdatalogscounttimeline(){
        //echo $this->siteKey."InsertGetDataLog";die;
        return $this->redis->lLen($this->siteKey."InsertGetDataLog");
    }
     //获取app获取数据
    public function getdatalogstimeline($offset,$nums)
    {
       // echo $this->siteKey."InsertGetDataLog";die;
        return $this->redis->lRange($this->siteKey."InsertGetDataLog",$offset,$offset+$nums);
    }
    
    
    
    

    //获取站点pv总数
    public function getPvCount(){
       // echo "<br>".$this->siteKey."<br>";
        return $this->redis->lLen($this->siteKey);
    }
    
    //获取站点跳转总数
    public function getJumpCount(){
       // echo $this->siteJumpKey;
       $count=$this->redis->get($this->siteJumpKey);
        return $count?$count:0;
    }
    
    

    //获取pv数据详情
    public function getPvs($offset,$nums)
    {
       // echo $this->siteKey." ".$offset." ".($offset+$nums);
        return $this->redis->lRange($this->siteKey,$offset,$offset+$nums);
    }
    
    
    
    
      //获取app启动总数
    public function getLaunchCount(){
        return $this->redis->lLen($this->siteKey."Launch");
    }
      //获取getdata总数
    public function getGetDataLogCount(){
        return $this->redis->lLen($this->siteKey."InsertGetDataLog");
    }
      //获取getdatatimeline总数
    public function getGetDataLogCounttimeline(){
        return $this->redis->lLen($this->siteKey."InsertGetDataLog");
    }
    //获取getdata去重后总数 
    public function GetNumCount(){
       return $this->redis->get($this->siteKey."GetNum");  
    }
    
    //获取getdata去重 落地页 数
    public function get_Luodi_Count(){
      // echo $this->siteKey.":{visitor_list}";
        $re=$this->redis->zCard($this->siteKey.":{visitor_list}");
        //dump($re); echo "<br>";
         if($re){
          return $re;  
         }else{
             return 0;
         }
         
         
        //  $re=$this->redis->get($this->siteKey."Luodi");
        //  if($re){
        //   return $re;  
        //  }else{
        //   return 0;
        //  }
    }
    
    //获取getdata去重 落地页 数
    public function get_ShenHe_Count(){
         $re=$this->redis->get($this->siteKey."ShenHe");
         if($re){
           return $re;  
         }else{
           return 0;
         }
    }
    
     //获取app启动 分页
    public function getLaunchs($offset,$nums)
    {
        return $this->redis->lRange($this->siteKey."Launch",$offset,$offset+$nums);
    }
    
     
    
        
      //获取审核页当日拦截总数
    public function getinsertinterceptCount(){
       
        return $this->redis->lLen($this->siteKey."Insertintercept");
    }
    
     //获取审核页当日拦截列表
    public function getinsertintercept($offset,$nums)
    {  
        //echo $this->siteKey;die;
        return $this->redis->lRange($this->siteKey."Insertintercept",$offset,$offset+$nums);
    }
     
    
    
   
    
    
    
    //删除数据
    public function del()
    {
       // echo $this->siteKey;
        return $this->redis->del($this->siteKey);
    }
    //删除项目缓存数据
    public function del_wxappgroup()
    {
        // echo $this->groupinfokeyname;die;
       // echo $this->siteKey;
        return $this->redis->del( $this->groupinfokeyname);
    }
    
    public function testrange($negativeendnum,$negativestartnum){
        
        ///获取列表指定区间中的元素。0表示列表第一个元素,-1表示最后一个元素,-2表示倒数第二个元素。
    $ret = $this->redis->lrange($this->siteKey, -$negativeendnum, -$negativestartnum);
    var_dump($ret);
    
    }
    
    
    
    //以下为site  setting相关 我暂时没使用
    
    
      // 读取站点配置
    public function getSettings()
    {
        $arr=$this->redis->hGetAll($this->siteSettingsKey);
        return $arr;
    }


    // 缓存站点配置
    public function setSettings($settings)
    {
        $this->redis->hMset($this->siteSettingsKey,$settings);
        $this->redis->expire($this->siteSettingsKey,$this->ttl);
    }
     
     
    //删除站点配置数据
    public function delSiteSettings()
    {
        //echo $this->siteSettingsKey;
        return $this->redis->del($this->siteSettingsKey);
    }
    
    
    //
    
    
    
    
    
    // // 更新站点预览密码
    // public function setPass($pass)
    // {
    //     $this->redis->hMset($this->siteTestKey,$pass);
    // }
    
    // // 获取站点预览密码
    // public function getPass()
    // {
    //     return $this->redis->hGetAll($this->siteTestKey);
    // }
    
    //访客记录
    
     public function fangke($nonce,$key,$type){
        //  "{shop}:{appid_key}:{appid_".$appid."}";
           $key1="{".date('Y-m-d').'}:{'.$key.'}:{'.$nonce."}";
           $val=$type;
       // echo $openid;die;
        $re = $this->redis->get($key);
        // dump($re);
        if (!$re){//不存在 则写入redis
       $this->redis->set($key,$val,$timeout);
        }
     }
     
     
      // 添加访客信息
    public function addVisitor($groupPrefix, $data)
    {
        // print_r($data);die;
        //  file_put_contents("logs/".date("Y-m-d",time())."_addvisitor.txt",date("Y-m-d H:i:s",time())."\r\n\r\n".var_export($data,true)."\r\n\r\n", FILE_APPEND );
        $datePrefix="{shop}:{".date('Y-m-d')."}";
        //查询之前有没有数据,有的话走更新不添加
       $cunzai= $this->getVisitor($datePrefix,$groupPrefix, $data['user']);
       if(!$cunzai){
              $visitorKey = $datePrefix . ':{' . $groupPrefix . '}:{visitor}:' . $data['user'];
              $data['create']=date('Y-m-d H:i:s');
        $this->redis->hMSet($visitorKey, $data);
        $this->redis->expire($visitorKey,$this->ttl); // 设置缓存有效期为3天
        
        $timestamp = time();
        $this->redis->zAdd($datePrefix . ':{' . $groupPrefix . '}:{visitor_list}', $timestamp, $visitorKey);//  
        $this->redis->expire($datePrefix . ':{' . $groupPrefix . '}:{visitor_list}',$this->ttl);
        
        
                //添加统计信息  不用那个 InsertGetDataLog方法了
            //   $this->redis->incr($datePrefix . ':{' . $groupPrefix . '}:{GetNum}'); 
            //   $this->redis->expire($datePrefix . ':{' . $groupPrefix . '}:{GetNum}',$this->ttl);
            //     if($data['is_sh']==1){
            //           $this->redis->incr($datePrefix . ':{' . $groupPrefix . '}:{ShenHe}');
            //           $this->redis->expire($datePrefix . ':{' . $groupPrefix . '}:{ShenHe}',$this->ttl);
            //     }else{
            //           $this->redis->incr($datePrefix . ':{' . $groupPrefix . '}:{LuoDi}');
            //           $this->redis->expire($datePrefix . ':{' . $groupPrefix . '}:{LuoDi}',$this->ttl);
            //     } 
                
        
       }else{
          $this->updateVisitor($datePrefix, $groupPrefix, $data['user'], $data); 
       }
     
     
    }

    // 更新访客信息
    public function updateVisitor($datePrefix, $groupPrefix, $key, $data)
    {
        $visitorKey = $datePrefix .  ':{' . $groupPrefix . '}:{visitor}:' . $key;
        $data['update']=date('Y-m-d H:i:s');
        $this->redis->hMSet($visitorKey, $data);
    if(isset($data['is_sh'])){
      //先判断去重,再添加到有序数对中  

      
       $cunzai= $this->redis->get($datePrefix .  ':{' . $groupPrefix . '}:{visitors}:' . $key);

    //   file_put_contents("paylogs/".date("Y-m-d",time())."_ceshi.txt",date("Y-m-d H:i:s",time())."\r\n\r\n".$cunzai."\r\n\r\n", FILE_APPEND );  
      if($cunzai===false){
 
            //       file_put_contents("paylogs/".date("Y-m-d",time())."_ceshi.txt",date("Y-m-d H:i:s",time())."\r\n\r\n".json_encode($data)."\r\n\r\n", FILE_APPEND );
          
         if($data['is_sh']==1){
              $this->redis->incr($datePrefix . ':{' . $groupPrefix . '}:{ShenHe}');
              $this->redis->expire($datePrefix . ':{' . $groupPrefix . '}:{ShenHe}',$this->ttl);
         }else{
              $this->redis->incr($datePrefix . ':{' . $groupPrefix . '}:{LuoDi}');
               $this->redis->expire($datePrefix . ':{' . $groupPrefix . '}:{LuoDi}',$this->ttl);
         }
      
        $this->redis->set($datePrefix .  ':{' . $groupPrefix . '}:{visitors}:' . $key,$data['is_sh'], $this->ttl);
        

      }
      
    }    
        
        
    }

    // 获取访客信息
    public function getVisitor($datePrefix, $groupPrefix, $key)
    {
        $visitorKey = $datePrefix .  ':{' . $groupPrefix . '}:{visitor}:' . $key;
        return $this->redis->hGetAll($visitorKey);
    }

    // 分页查询访客信息
    public function getVisitorsByPage($datePrefix, $groupPrefix, $page, $perPage)
    {
        $start = ($page - 1) * $perPage;
        $end = $start + $perPage - 1;

        $visitorListKey = $datePrefix .  ':{' . $groupPrefix . '}:{visitor_list}';
        // $visitorKeys = $this->redis->zRange($visitorListKey, $start, $end);
        //逆序
        $visitorKeys = $this->redis->Zrevrange($visitorListKey, $start, $end);
        $visitors = [];

        foreach ($visitorKeys as $key) {
            $visitors[] = $this->redis->hGetAll($key);
        }

        return $visitors;
    }
    
    public function getShenHeCount($datePrefix, $groupPrefix){
        $key= $datePrefix .  ':{' . $groupPrefix . '}:{ShenHe}';
        $count= $this->redis->get($key);
        return $count;
    }
    
    public function getLuoDiCount($datePrefix, $groupPrefix){
        $key= $datePrefix .  ':{' . $groupPrefix . '}:{LuoDi}';
        $count= $this->redis->get($key);
        return $count;
    }
    
    
    
      public function getVisitorsCount($datePrefix, $groupPrefix)
    {
   

        $visitorListKey = $datePrefix .  ':{' . $groupPrefix . '}:{visitor_list}';
    //   $visitorKeys = $this->redis->zRange($visitorListKey, 0, 10);
       
        $visitorKeys = $this->redis->zCard($visitorListKey);
//   var_dump($visitorKeys);die;

        return $visitorKeys;
    }
    
    
    
         protected function loadDataToRedis()
    {
        set_time_limit(0);
         $pageSize = 5000;
    $page = 1;

    while (true) {
        $iplibrary = DB::name('iplibrary')
                          ->field('start_ip, end_ip, continent, country, province, city, county, isp')
                          ->page($page, $pageSize)
                          ->select();

        // if no more data, break the loop
        if (count($iplibrary) == 0) {
            break;
        }

        foreach ($iplibrary as $item) {
            $this->redis->zAdd('iplibrary', $item['start_ip'], json_encode($item));
            $this->redis->zAdd('iplibrary', $item['end_ip'], json_encode($item));
        }

        // go to next page
        $page++;
    }

          
        // $iplibrary = DB::name('iplibrary')->field('start_ip, end_ip, continent, country, province, city, county, isp')->select();
        // //  var_dump($iplibrary);die;
        // foreach ($iplibrary as $item) {
        //     $this->redis->zAdd('iplibrary', $item['start_ip'], json_encode($item));
        //     $this->redis->zAdd('iplibrary', $item['end_ip'], json_encode($item));
        // }
    }
    
    
      public function getIpInfo($ip)
    {
        // var_dump($this->redis->zCard('iplibrary'));die;
       
         if ($this->redis->zCard('iplibrary') < 300000) {
            //   var_dump(111);die;
            $this->loadDataToRedis();
        }
        //   var_dump($this->redis->zCard('iplibrary'));die;
        $ipLong = ip2long($ip);

        // First, try to find the segment that starts with an IP less than or equal to the given IP
        $ipInfo = $this->redis->zRevRangeByScore('iplibrary', $ipLong, "-inf", ['limit' => [0, 1]]);

        if (empty($ipInfo)) {
            return '未知';
        }

        $ipInfo = json_decode($ipInfo[0], true);

        // If the end IP of this segment is greater than or equal to the given IP, return this segment
        if ($ipLong <= $ipInfo['end_ip']) {
            return $this->formatIpInfo($ipInfo);
        }

        // Otherwise, return the next segment
        $ipInfo = $this->redis->zRangeByScore('iplibrary', $ipLong, "+inf", ['limit' => [0, 1]]);

        if (empty($ipInfo)) {
            return '未知';
        }

        $ipInfo = json_decode($ipInfo[0], true);

        return $this->formatIpInfo($ipInfo);
    }

    private function formatIpInfo($ipInfo)
    {
        // format the IP info as a string: "country|province|city|county|isp"
        return $ipInfo['country'] . '|' . $ipInfo['province'] . '|' . $ipInfo['city'] . '|' . $ipInfo['county'] . '|' . $ipInfo['isp'];
    }
    
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值