/** * event类型消息解析, 根据不同事件类型自定义回复消息 * *@param Message $message 消息对象 * * @return Response */ private static function event($event) { $orgId = $event->ToUserName; $openId = $event->FromUserName; $app = WechatApp::getAppByOrgId($orgId); if (!isset($app)) { SysLog::error(__FILE__, __METHOD__, __LINE__, 'get wechat app failed'); return false; } $replyArr = Command::getReplyDefine($app, $event->MsgType, $event->Event); if (!$replyArr) { SysLog::error(__FILE__, __METHOD__, __LINE__, 'get event reply define array failed'); return false; } return ReplyHandler::handle($app, $openId, $replyArr); }
/** * Get reply define by command content. * * @param $cmdType * @param $cmdBody * @return array */ public static function getReplyDefine($app, $cmdType, $cmdBody) { if (isset($app) && isset($cmdType) && isset($cmdBody)) { $cmd = Command::match($cmdBody); if (!$cmd['texts']) { Log::error('invalid command content'); return null; } $reply = self::where(['wechat_id' => $app['id'], 'cmd_type' => $cmdType, 'cmd_content' => $cmd['texts'][0]])->get()->first(); if (!$reply) { Log::error('command content not found' . json_encode($cmd)); return null; } $replyArr = ['event_id' => $reply['event_id'], 'is_prefix' => $reply['is_prefix'], 'reply_type' => $reply['reply_type'], 'event_code' => Event::getEventCode($reply['event_id']), 'is_activity_event' => Event::isActivity($reply['event_id']), 'reply_json' => json_decode(base64_decode($reply['reply_json']), true), 'cmd_original' => $cmdBody]; if ($cmd['texts']) { $replyArr['cmd_text'] = $cmd['texts'][0]; } if ($cmd['numbers']) { $replyArr['cmd_number'] = $cmd['numbers']; } return $replyArr; } return null; }