« 上一篇 下一篇 »

一个微信链接及接收消息的类​

<?php
namespace app\member\controller;
use app\member\Lib\qyWeixin\WXBizMsgCrypt;
use think\Log;
class Weixin 
{
    public function index(){
        $sVerifyEchoStr = urldecode(input("echostr"));
        $token = config("token");
        $encodingAesKey = config("encodingAesKey");
        $corpId = config("corpId");
        $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
        //url验证 建立连接
        if (!empty($sVerifyEchoStr)){
            $sVerifyMsgSig = urldecode(input("msg_signature"));
            $sVerifyTimeStamp = urldecode(input("timestamp"));
            $sVerifyNonce = urldecode(input("nonce"));
            
            $sEchoStr = "";
            $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
            if ($errCode == 0){
                echo $sEchoStr;
                exit();
            }else{
                Log::write("ERR: " . $errCode . "\n\n");
                print("ERR: " . $errCode . "\n\n");
                exit();
            }
        }
        else{
           $sReqMsgSig = input("msg_signature");
           $sReqTimeStamp = input("timestamp");
           $sReqNonce = input("nonce");
           $sReqData =file_get_contents("php://input");
           $sMsg = "";  // 解析之后的明文
           $errCode = $wxcpt->DecryptMsg($sReqMsgSig, $sReqTimeStamp, $sReqNonce, $sReqData, $sMsg);
           if ($errCode == 0) {
               Log::write($sMsg);
               $xml = new \DOMDocument();
               $xml->loadXML($sMsg);
               //FromUserName
               $FromUserName = $xml->getElementsByTagName("FromUserName")->item(0)->nodeValue;
               //AgentID
               $AgentID = $xml->getElementsByTagName("AgentID")->item(0)->nodeValue;
               //与配置的agentid相同
               if ($AgentID == config("agentid")){
                   //接收到的消息类型
                   $MsgType = $xml->getElementsByTagName("MsgType")->item(0)->nodeValue;
                   //事件
                   if ($MsgType=="event"){
                       //事件类型
                       $Event = $xml->getElementsByTagName("Event")->item(0)->nodeValue;
                       if ($Event=="scancode_push"){//扫码推送事件
                           $EventKey = $xml->getElementsByTagName("EventKey")->item(0)->nodeValue;
                           if ($EventKey==config("eventKey")){//事件key值与配置相同
                               $ScanType = $xml->getElementsByTagName("ScanType")->item(0)->nodeValue;
                               if ($ScanType == "qrcode"){//二维码扫描
                                  //扫描结果
                                  $ScanResult= $xml->getElementsByTagName("ScanResult")->item(0)->nodeValue;
                                  //打开消费页面
                                  Log::write($ScanResult);
                               }
                               
                           }
                       }
                   }
               }
           }else{
               Log::write("ERR: " . $errCode . "\n\n");
           }
        }
    }
}