123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\index\controller;
- use DOMDocument;
- use wx\offiaccount\msg\ResponseMsg;
- use wx\work\callback\WXBizMsgCrypt;
- use think\helper\Str;
- class work
- {
- /**
- * 应用
- * 接收消息
- */
- public function index()
- {
- $token = '9IgAfTZY34mA';
- $encodingAesKey = '92ccGXDDZE3eE1ws1XrAvyP3VLrB9PCPHbrwB8jz5QM';
- $corpId = 'wwf5d2dea0a059ab7a';
- $wxcpt = new WXBizMsgCrypt($token, $encodingAesKey, $corpId);
- $request = request();
- if ($request->isGet()) {
- // return $this->verifyURL($wxcpt);
- echo $this->verifyURL($wxcpt);
- exit;
- }
- if ($request->isPost()) {
- // return $this->decryptMsg($wxcpt);
- echo $this->decryptMsg($wxcpt);
- exit;
- }
- }
- /**
- * 企业微信检测url
- */
- private function verifyURL(WXBizMsgCrypt $wxcpt)
- {
- $sVerifyMsgSig = $_GET['msg_signature'];
- $sVerifyTimeStamp = $_GET['timestamp'];
- $sVerifyNonce = $_GET['nonce'];
- $sVerifyEchoStr = $_GET["echostr"];
- // token验证
- $errCode = $wxcpt->VerifyURL($sVerifyMsgSig, $sVerifyTimeStamp, $sVerifyNonce, $sVerifyEchoStr, $sEchoStr);
- if ($errCode == 0) {
- return $sEchoStr;
- } else {
- trace("ERR: " . $errCode, 'error');
- }
- }
- private function decryptMsg(WXBizMsgCrypt $wxcpt)
- {
- $sMsgSignature = $_GET['msg_signature'];
- $sTimeStamp = $_GET['timestamp'];
- $sNonce = $_GET['nonce'];
- $sPostData = file_get_contents('php://input');
- trace('postxml:' . $sPostData);
- $errCode = $wxcpt->DecryptMsg($sMsgSignature, $sTimeStamp, $sNonce, $sPostData, $sMsg);
- if ($errCode == 0) {
- $xml = new DOMDocument();
- $xml->loadXML($sMsg);
- $msgType = $xml->getElementsByTagName('MsgType')->item(0)->nodeValue;
- if (method_exists($this, $msgType)) {
- return $this->$msgType($wxcpt, $xml);
- }
- } else {
- trace("ERR: " . $errCode, 'error');
- }
- }
- /**
- * 文本消息
- */
- private function text(WXBizMsgCrypt $wxcpt, $xml)
- {
- $postMsg = $xml->getElementsByTagName('Content')->item(0)->nodeValue;
-
- // 根据话术获取固定内容(分词查找内容)
-
- $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
- $toUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;
- $textData = [
- 'ToUserName' => $fromUserName,
- 'FromUserName' => $toUserName,
- 'MsgType' => 'text',
- 'Content' => '小爱还不知道你说的是什么意思,正在学习中。。。'
- ];
- $msg = new ResponseMsg();
- $sRespData = $msg->responseText($textData);
- $errCode = $wxcpt->EncryptMsg($sRespData, $_GET['timestamp'], $_GET['nonce'], $sEncryptMsg);
- if ($errCode == 0) {
- return $sEncryptMsg;
- } else {
- trace("ERR: " . $errCode, 'error');
- }
- }
- /**
- * 事件消息
- */
- private function event(WXBizMsgCrypt $wxcpt, $xml)
- {
- $event = $xml->getElementsByTagName('Event')->item(0)->nodeValue;
- $event = Str::camel($event).'Event';
- if (method_exists($this, $event)) {
- return $this->$event($wxcpt, $xml);
- }
- }
- /**
- * 用户进入应用事件(该事件在员工进入到该应用中时会一直有)
- */
- private function enterAgentEvent(WXBizMsgCrypt $wxcpt, $xml)
- {
- // $fromUserName = $xml->getElementsByTagName('FromUserName')->item(0)->nodeValue;
- // // 更新用户是否关注公众号
- // $toUserName = $xml->getElementsByTagName('ToUserName')->item(0)->nodeValue;
- // $textData = [
- // 'ToUserName' => $fromUserName,
- // 'FromUserName' => $toUserName,
- // 'MsgType' => 'text',
- // 'Content' => '欢迎进入志远装饰营销数字化系统'
- // ];
- // $msg = new ResponseMsg();
- // $sRespData = $msg->responseText($textData);
- // $errCode = $wxcpt->EncryptMsg($sRespData, $_GET['timestamp'], $_GET['nonce'], $sEncryptMsg);
- // if ($errCode == 0) {
- // return $sEncryptMsg;
- // } else {
- // trace("ERR: " . $errCode, 'error');
- // }
- }
- }
|