aesKey = $component['aes_key']; $this->authToken = $component['auth_token']; } /** * 消息与事件接收 * */ public function receive(Request $request) { $msgSign = trim($request->input('msg_signature')); $timestamp = trim($request->input('timestamp')); $nonce = trim($request->input('nonce')); if($request->isMethod('get')) { // 初步校验回调链接合法性 $echoStr = trim($request->input('echostr')); $encryptMsg = ""; // 解析之后的明文 $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->corpid); $errCode = $crypt->VerifyURL($msgSign, $timestamp, $nonce, $echoStr, $encryptMsg); if($errCode == 0) { echo $encryptMsg; } else { # 解密失败,写入日志 Log::logError($echoStr, [ 'msgSign' => $msgSign, 'timestamp' => $timestamp, 'nonce' => $nonce, 'err_code' => $errCode ], 'MsgReceiveDecrypt-Get'); } exit; } else { // 数据回调通知 $str = file_get_contents('php://input'); $encryptMsg = ""; // 解析之后的明文 try { $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->appTemplateId); $encrypt = getInfoFromXMl($str, 'Encrypt'); $format = ""; $from_xml = sprintf($format, $encrypt); $errCode = $crypt->decryptMsg($msgSign, $timestamp, $nonce, $from_xml, $encryptMsg); Log::logInfo($encryptMsg, []); if($errCode == 0) { TemplateCallbackService::responseCommand($encryptMsg); echo 'success'; exit; } else { Log::logError('verifyTicket获取失败', ['errCode' => $errCode], 'TemplateVerifyTicketGetFail'); } } catch (\Exception $e) { Log::logError('事件信息捕获异常', [ 'msg' => $e->getMessage(), 'line' => $e->getLine(), ],'TemplateEventGet'); } echo "success"; } } }