<?php
namespace app\common\library\wechat;
use think\Exception;
use think\Db;
class Wechat
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
$postStr = file_get_contents("php://input");
if (!empty($postStr)){
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$msgType = $postObj->MsgType $event = $postObj->Event $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
switch($msgType){
case "event":
if($event=="subscribe"){
$contentStr = "Hi,欢迎关注";
}
break;
case "text":
if(!empty($keyword)){
if(preg_match("/^1[34578]\d{9}$/", $keyword)){
$member = Db("u_tanfang_member")->where(array('mobile'=>$keyword))->find();
if(!empty($member)){
Db("u_tanfang_member")->where(array('mobile'=>$keyword))->update(array('wechat_openid'=>$fromUsername));
}
$member = Db("u_tanfang_techer")->where(array('mobile'=>$keyword))->find();
if(!empty($member)){
Db("u_tanfang_techer")->where(array('mobile'=>$keyword))->update(array('wechat_openid'=>$fromUsername));
}
$contentStr = "Hi,亲已绑定好了,坐等消息回复即可!";
}else{
echo "";
exit;
}
}
break;
}
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else {
echo "";
exit;
}
}
private function checkSignature()
{
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>