共计 11285 个字符,预计需要花费 29 分钟才能阅读完成。
这是婚恋交友微信公众号模块开发局部,第六篇,也是微信公众号性能开发的比拟重要的局部。
如果有什么疑难能够,找我,各位,我的扣是 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 = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$createtime}</CreateTime>
<MsgType><![CDATA]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%s</FuncFlag>
</xml>";
return sprintf($textTpl,$text,$funcflag);
}
public function makeDkf($text='')
{$this->msg=$this->msg();
$createtime = time();
$s = "<xml>
<ToUserName><![CDATA[{$this->msg[‘FromUserName’]}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg[‘ToUserName’]}]]></FromUserName>
<CreateTime>$createtime</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>”;
return $s;
}
public function makeNews($newsData=array())
{$this->msg=$this->msg();
$createtime = time();
$funcflag = $this->setFlag ? 1 : 0;
$newTplHeader = "<xml>
<ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
<FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
<CreateTime>{$createtime}</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[%s]]></Content>
<ArticleCount>%s</ArticleCount><Articles>";
$newTplItem = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>";
$newTplFoot = "</Articles>
<FuncFlag>%s</FuncFlag>
</xml>";
$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;
}
点击并拖拽以挪动