serverCorpid = $component['server_corpid']; $this->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->serverCorpid); $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 ], 'SystemMsgReceiveDecrypt-Get'); } exit; } else { // 数据回调通知 $str = file_get_contents('php://input'); $encryptMsg = ""; // 解析之后的明文 try { $crypt = new \WXBizMsgCrypt($this->authToken, $this->aesKey, $this->serverCorpid); $encrypt = getInfoFromXMl($str, 'Encrypt'); $format = ""; $from_xml = sprintf($format, $encrypt); $errCode = $crypt->decryptMsg($msgSign, $timestamp, $nonce, $from_xml, $encryptMsg); Log::logInfo($encryptMsg, [], 'sysInfo'); if ($errCode == 0) { SystemCallbackService::responseDeal($encryptMsg); echo 'success'; exit; } else { Log::logError('verifyTicket获取失败', ['errCode' => $errCode], 'SystemVerifyTicketGetFail'); } } catch (\Exception $e) { Log::logError('事件信息捕获异常', [ 'msg' => $e->getMessage(), 'line' => $e->getLine(), ], 'SystemEventGet'); } echo "success"; } } }