婚恋交友微信公众号模块开发部分,第六篇


这是婚恋交友微信公众号模块开发部分,第六篇,也是微信公众号功能开发的比较重要的部分。
如果有什么疑问可以,找我,各位,我的扣是2360248-666

菜单设置

class WeixinController extends BaseController
{

public function _initialize(){

    
header("Content-Type:text/html; charset=utf-8");         
}

public function log(){

dump(S('log'));

}

public function index(){


define("TOKEN", C("wxtoken")?C("wxtoken"):"yueai8999");//成为开发者token     
 $echoStr = $_GET["echostr"];
   if($this->checkSignature()){
       if($echoStr){
        echo $echoStr;
        exit;
       }else{
           $this->getmsg();        
       }
    }        
}    



public function getmsg(){

 $ss=0;
      if(C("other_url") && C("other_token") && C('malasen')!=2){    //add by lxphp.com
$url = $this->get_other_url();
$re = $this->curlpxml($url,$GLOBALS["HTTP_RAW_POST_DATA"]);    
if(strstr($re,'xml')) { $ss=1; echo $re; }
//S('log',$re);
}            
    $xml=$this->msg();
    if(S($xml['FromUserName'].$xml['CreateTime'])==1)exit;    
    S($xml['FromUserName'].$xml['CreateTime'],1,300);        
    
     $db =M('Wxtext');
     //$db->add($xml);
     $openid = $xml['FromUserName'];
     $eventkey = $xml['EventKey'];
    if($xml['MsgType']=='event')  {
        $Event=$xml['Event'];
        switch($Event){
            case "subscribe":    
            $usermod = M("Users");        
            $re = A('Api')->saveinfo($openid);                                
                if($eventkey&& $xml['Ticket']){//二维码关注
                    $pid  = str_replace('qrscene_','',$eventkey);                        
                    $pinfo = $usermod->find($pid);
                    $msg2 = "【".$re["user_nicename"]."】通过您分享的二维码关注了公众号,Ta注册后您有可能获得奖励。";
                    $this->makeTextbygm($msg2,$pinfo['weixin']);
                    $re['parent_id']=$datas2['puid']=$pid;
                    M("User_y_reg")->where("id=".$re['id'])->save($datas2);
                
                }
            
            /*    if(C('gzshbval')>0)            
                A('Api')->sendhb($openid,C('gzshbval'),C('site_title'),C('hbbody'),2); // 关注送红包    
                if(C('gzsxj')>0) A('Api')->sendzz($openid,C('gzsxj'),C('hbbody'),2);    //关注送现金*/            
            
                    
            
            if($re['id']>0){                    
                $msg = C("diygzhf")?C("diygzhf"):0;
                $msg = $re['sex']==2?C("diygzhfnv"):$msg;                
                if($msg) $this->makeTextbygm(html_out($msg),$openid);
                
                if(C('gztstw')==1){                
                $sex = $re['sex']==1?2:1;
                if($re['sex']>0&&$re['cityid']){                        
                    $list = $usermod->field("user_nicename,avatar,provinceid,cityid,age,idmd5")->where("sex=".$sex." and cityid=".$re['cityid']." and avatar!=''")->order('last_login_time desc,id desc')->limit(8)->select();
                }elseif($re['sex']>0&&$re['provinceid']){
                    $list = $usermod->field("user_nicename,avatar,provinceid,cityid,age,idmd5")->where("sex=".$sex." and provinceid=".$re['provinceid']." and avatar!=''")->order('last_login_time desc,id desc')->limit(8)->select();
                }else{
                    $list = $usermod->field("user_nicename,avatar,provinceid,cityid,age,idmd5")->where("sex=".$sex." and avatar!=''")->order('last_login_time desc,id desc')->limit(8)->select();
                }                    
                
                $list2[0]['title']="来".C('site_title')."邂逅缘分吧";
                $list2[0]['description']=$msg;
                $list2[0]['url']="http://".C('site_url').U("Home/Index/index");
                $list2[0]['picUrl']="http://www.yueai.me/v4/jiaocheng_03.jpg";
                                    
                foreach($list as $key=>$val){
                $list2[$key+1]['title']=$val['user_nicename']." ".(date('Y',time())-$val['age'])."岁 ".$re['province'].' '.$re['city'];
                $list2[$key+1]['description']=$val['user_nicename']." ".(date('Y',time())-$val['age'])."岁";
                $list2[$key+1]['url']="http://".C('site_url').U("Home/Show/index",array('uid'=>$val['idmd5']));
                $list2[$key+1]['picUrl']=strstr($val['avatar'],'http')?$val['avatar']:'http://'.C('site_url')."/".$val['avatar'];                
                }
                
                
                $ss1['items']=$list2;                    
                
                                    
                
                if($openid!=C('adminopenid'))
                $this->makeTextbygm("有新朋友关注:【".$re['user_nicename']."】",C('adminopenid'));                                        
                if(is_array($ss1)){//欢迎关注。
                    exit($this->makeNews($ss1));                    
                }
                }
            }
            //if($msg) $this->makeTextbygm(html_out($msg),$openid);            
            break;
            case "unsubscribe":
            A("Api")->unsubscribe($openid);
            break;
            case "CLICK":                
                    
            if(C('old_subscribe')){
                $data =A("Api")-> saveinfo($openid,1);                        
                if($data['type']=='newreg'){
                
                }
            }
            
            
            
            if($eventkey=='lxphpcom'){//二维码推广 20151112 by 紫竹
                $msg ='正在生成您的推广二维码,请耐心等待...';
                if($msg) $this->makeTextbygm($msg,$openid);
                $media_id = A("Home/Api")->getewmmediaid($openid);
                if($media_id) $this->makeImgbygm($media_id,$openid);
                    
            }    
            $msg = A("Api")->clickfun($eventkey,$openid);
            if($msg) $this->makeTextbygm($msg,$openid);    
        
            
            //if($ss==0 && $msg) echo $this->makeText($msg);
            //echo $this->makeText($msg);
            break;
            case "SCAN":
            if($eventkey&& $xml['Ticket']){//二维码扫描
            $re = A('Api')->saveinfo($openid);
            $pid  = str_replace('qrscene_','',$eventkey);
            $pinfo = M("Users")->find($pid);
                    $msg2 = "【".$re["user_nicename"]."】扫描了您分享的二维码。";
                    $this->makeTextbygm($msg2,$pinfo['weixin']);
                    $datas2['puid']=$pid;
                    $ymod = M("User_y_reg");
                    $re2 = $ymod->where("code='$openid'")->find();
                    if($re2){
                        $ymod->where("code='$openid'")->save($datas2);
                    }else{
                        $datas2['time']=time();
                        $datas2['code']=$openid;
                        $ymod->add($datas2);
                    }
            }
            break;
            }
        
        
        } 
          
    if($xml['MsgType']=='text'){//接收文本消息后返回信息给用户
    $xml['Content']=trim($xml['Content']); 
    echo $this->makeDkf($xml['Content']);
    $re = M("ext_autoreplay")->where("keyword='".$xml['Content']."'")->find();
    if($re){
        $this->makeTextbygm($re['content'],$openid);    
        if($re['money'] && $re['money']>0){
            A('Api')->sendhb($openid,$re['money'],C('site_title'),C('hbbody'),2); // 送红包    
        }
        
    }    
        
        
    }    
        
        
    
        exit;    
        

}
点击并拖拽以移动
购买VIP成功

public function sendmb_geren($openid,$title,$content,$desc,$url2=""){

if(!$openid) return FALSE;
if(!$url2) $url2 ="http://".C('site_url').U('Home/Index/index');    
if(!strstr($url2,'http')){
    $url2 = "http://".C('site_url').$url2;
   }
if(strstr($url2,'notify')) $url2="";

$json = '{

       "touser":"'.$openid.'",
       "template_id":"'.C("moneymb").'",
       "url":"'.$url2.'",
       "topcolor":"#FF0000",
       "data":{
               "first": {
                   "value":"您好,您有新的消息!",
                   "color":"#173177"
               },
               "keyword1": {
                   "value":"'.$title.'",
                   "color":"#173177"
               },
               "keyword2": {
                   "value":"'.date("Y-m-d H:i:s",time()).'",
                   "color":"#173177"
               },
               "keyword3": {
                   "value":"'.$content.'",
                   "color":"#173177"
               },
               "remark": {
                   "value":"'.$desc.'",
                   "color":"#173177"
               }
       }
   }';
 if(C('jkence')==2){
    exit();
}  
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=". $this->accesstoken();
 $re =  $this->curlp($url,$json);
 return $re['errmsg'];

}

public function sendmb_money($openid,$title,$jifen,$jifeny,$desc,$uname){//金钱变动通知
if(!$openid) return FALSE;
$json = '{

       "touser":"'.$openid.'",
       "template_id":"'.C("moneymb").'",
       "url":"http://'.C('site_url').U("Home/User/mymoney").'",
       "topcolor":"#FF0000",
       "data":{
               "first": {
                   "value":"'.$title.'",
                   "color":"#173177"
               },
               "date": {
                   "value":"'.date("Y-m-d H:i:s",time()).'",
                   "color":"#173177"
               },
               "adCharge": {
                   "value":"'.$jifen.'",
                   "color":"#FF0000"
               },
               "cashBalance": {
                   "value":"'.$jifeny.'",
                   "color":"#173177"
               },
               "remark": {
                   "value":"'.$desc.'",
                   "color":"#173177"
               }
       }
   }';
 if(S("lxphpca")==2){
    exit();
}  
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=". $this->accesstoken();
 $re =  $this->curlp($url,$json);
 return $re['errmsg'];

}

public function sendmb_jifen($openid,$title,$jifen,$jifeny,$desc,$uname){//发送通知模板消息
if(!$openid) return FALSE;

$json = '{
       "touser":"'.$openid.'",
       "template_id":"'.C("jifenmb").'",
       "url":"http://'.$_SERVER["HTTP_HOST"].U("Home/User/jifenlist").'",
       "topcolor":"#FF0000",
       "data":{
               "first": {
                   "value":"'.$title.'",
                   "color":"#173177"
               },
               "keyword1":{
                   "value":"'.$uname.'",
                   "color":"#173177"
               },
               "keyword2": {
                   "value":"'.date("Y-m-d H:i:s",time()).'",
                   "color":"#173177"
               },
               "keyword3": {
                   "value":"'.$jifen.'",
                   "color":"#FF0000"
               },
               "keyword4": {
                   "value":"'.$jifeny.'",
                   "color":"#173177"
               },
               "keyword5": {
                   "value":"'.$desc.'",
                   "color":"#173177"
               },                   
               "remark":{
                   "value":"感谢您的使用!",
                   "color":"#173177"
               }
       }
   }';
   
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=". $this->accesstoken();
 $re =  $this->curlp($url,$json);     
 return $re['errmsg'];
 dump($re);

}

public function downmedia($mediaid=''){//下载多媒体文件

if(!$mediaid){
    $mediaid = $_GET['mid'];
}
//dump($mediaid);
$geturl = 'http://file.api.weixin.qq.com/cgi-bin/media/get?access_token='.$this->accesstoken().'&media_id='.$mediaid;
echo $this->saveMedia($geturl);  //返回路径

}

public function msg(){

$data = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($data)) {//接收消息并处理
        $xml = (array)simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        return $xml;
  }

}
点击并拖拽以移动
给用户发消息

public function makeTextbygm($text,$touser)

{
$msg = '{
"touser":"'.$touser.'",
"msgtype":"text",
"text":
{
     "content":"'.$text.'"//消息内容。
}

}';
//dump($msg);
$posturl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$this->accesstoken();
return $this->curlp($posturl,$msg);

}

public function makeImgbygm($MEDIA_ID,$openid) // 给用户发消息 img  20151112
{

    $data = '{"touser":"'.$openid.'","msgtype":"image","image":{"media_id":"'.$MEDIA_ID.'"}}';

    $posturl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$this->accesstoken();
    return $this->curlp($posturl,$data);
}


public function makeText($text='')

{
    $this->msg=$this->msg();
    $createtime = time();
    $funcflag = $this->setFlag ? 1 : 0;
    $textTpl = "
        msg['FromUserName']}]]>
        msg['ToUserName']}]]>
        {$createtime}
        
        
        %s
        ";
    return sprintf($textTpl,$text,$funcflag);
}


public function makeDkf($text='')
{
    
    $this->msg=$this->msg();
     $createtime = time();
    $s = "

msg['FromUserName']}]]>
msg['ToUserName']}]]>
$createtime

";

    
    return $s;
} 
    

public function makeNews($newsData=array())

{
    $this->msg=$this->msg();
    $createtime = time();
    $funcflag = $this->setFlag ? 1 : 0;
    $newTplHeader = "
        msg['FromUserName']}]]>
        msg['ToUserName']}]]>
        {$createtime}
        
        
        %s";
    $newTplItem = "
        <![CDATA[%s]]>
        
        
        
        ";
    $newTplFoot = "
        %s
        ";
    $content = '';
    $itemsCount = count($newsData['items']);
    $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条
    if ($itemsCount) {
        foreach ($newsData['items'] as $key => $item) {
            $content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picUrl'],$item['url']);//微信的信息数据

        }
    }
    $header = sprintf($newTplHeader,$newsData['content'],$itemsCount);
    $footer = sprintf($newTplFoot,$funcflag);
    return $header . $content . $footer;
} 

public function accesstoken($new2=""){

if(S("gettokenf")) { 
$new =0;
 }else{
    S("gettokenf",1,6000);
     $new=1;
}
if($new2==1) $new=1;
return A("Home/Api")->wxtoken($new);

}

点击并拖拽以移动
上传多媒体文件

function uploadMedia($file){

    $access_token = $this->accesstoken();        
    $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=".$access_token."&type=image";
  $file = realpath($file); //要上传的文件
    $fields['media'] = '@'.$file;
    $ch = curl_init($url) ;
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch) ;
    if (curl_errno($ch)) {
     return curl_error($ch);
    }
    curl_close($ch);
    return $result;
}

//下载多媒体文件

function saveMedia($url){
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);    
    curl_setopt($ch, CURLOPT_NOBODY, 0);    //对body进行输出。
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $package = curl_exec($ch);
    $httpinfo = curl_getinfo($ch);
   
    curl_close($ch);
    $media = array_merge(array('mediaBody' => $package), $httpinfo);
    
    //求出文件格式
    preg_match('/\w\/(\w+)/i', $media["content_type"], $extmatches);
    $fileExt = $extmatches[1];
    $filename = time().rand(100,999).".{$fileExt}";
    $dirname = "./wxmedia/";
    if(!file_exists($dirname)){
        mkdir($dirname,0777,true);
    }
    file_put_contents($dirname.$filename,$media['mediaBody']);
    return $dirname.$filename;
}

function array_sort($arr,$keys,$type='asc'){ //二维数组排序

$keysvalue = $new_array = array();
foreach ($arr as $k=>$v){
    $keysvalue[$k] = $v[$keys];
}
if($type == 'asc'){
    asort($keysvalue);
}else{
    arsort($keysvalue);
}
reset($keysvalue);
foreach ($keysvalue as $k=>$v){
    $new_array[$k] = $arr[$k];
}
return $new_array; 

}
点击并拖拽以移动

你可能感兴趣的:(php)