/// <summary> /// 处理用户消息和事件 /// </summary> /// <returns></returns> public static ReplyBaseMsg ReplyMsg() { Stream inputStream = System.Web.HttpContext.Current.Request.InputStream; long pos = inputStream.Position; inputStream.Position = 0; byte[] buffer = new byte[inputStream.Length]; inputStream.Read(buffer, 0, buffer.Length); inputStream.Position = pos; string xml = System.Text.Encoding.UTF8.GetString(buffer); var dict = Util.GetDictFromXml(xml); //产生事件 if (OnReceiveMsgEvent != null) { OnReceiveMsgEvent(xml, dict); } string key = string.Empty; ReplyBaseMsg replyMsg = ReplyEmptyMsg.Instance; if (dict.ContainsKey("Event")) { #region 接收事件消息 var evt = dict["Event"].ToLower(); key = "event_"; switch (evt) { case "click": { var msg = new EventClickMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Click, EventKey = dict["EventKey"] }; replyMsg = GetReply <EventClickMsg>(key + MyEventType.Click.ToString(), msg); break; } case "view": { var msg = new EventViewMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.View, EventKey = dict["EventKey"] }; replyMsg = GetReply <EventViewMsg>(key + MyEventType.View.ToString(), msg); break; } case "location": { var msg = new EventLocationMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Location, Latitude = double.Parse(dict["Latitude"]), Longitude = double.Parse(dict["Longitude"]), Precision = double.Parse(dict["Precision"]) }; replyMsg = GetReply <EventLocationMsg>(key + MyEventType.Location.ToString(), msg); break; } case "scan": { var msg = new EventFansScanMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.FansScan, EventKey = dict["EventKey"], Ticket = dict["Ticket"] }; replyMsg = GetReply <EventFansScanMsg>(key + MyEventType.FansScan.ToString(), msg); break; } case "unsubscribe": { var msg = new EventUnattendMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Unattend }; replyMsg = GetReply <EventUnattendMsg>(key + MyEventType.Unattend.ToString(), msg); break; } case "subscribe": { if (dict.ContainsKey("Ticket")) { var msg = new EventUserScanMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.UserScan, Ticket = dict["Ticket"], EventKey = dict["EventKey"] }; replyMsg = GetReply <EventUserScanMsg>(key + MyEventType.UserScan.ToString(), msg); } else { var msg = new EventAttendMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Attend }; replyMsg = GetReply <EventAttendMsg>(key + MyEventType.Attend.ToString(), msg); } break; } case "masssendjobfinish": { var msg = new EventMassSendJobFinishMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.MASSSENDJOBFINISH, ErrorCount = int.Parse(dict["ErrorCount"]), FilterCount = int.Parse(dict["FilterCount"]), MsgID = int.Parse(dict["MsgID"]), SentCount = int.Parse(dict["SentCount"]), TotalCount = int.Parse(dict["TotalCount"]), Status = dict["Status"] }; replyMsg = GetReply <EventMassSendJobFinishMsg>(key + MyEventType.MASSSENDJOBFINISH.ToString(), msg); break; } case "merchant_order": { var msg = new EventMerchantOrderMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.MerchantOrder, OrderID = dict["OrderID"], OrderStatus = int.Parse(dict["OrderStatus"]), ProductId = dict["ProductId"], SkuInfo = dict["SkuInfo"] }; replyMsg = GetReply <EventMerchantOrderMsg>(key + MyEventType.MerchantOrder.ToString(), msg); break; } } #endregion } else if (dict.ContainsKey("MsgId")) { #region 接收普通消息 var msgType = dict["MsgType"]; key = msgType; switch (msgType) { case "text": { var msg = new RecTextMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Content = dict["Content"] }; replyMsg = GetReply <RecTextMsg>(key, msg); break; } case "image": { var msg = new RecImageMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), PicUrl = dict["PicUrl"], MediaId = dict["MediaId"] }; replyMsg = GetReply <RecImageMsg>(key, msg); break; } case "voice": { string recognition; dict.TryGetValue("Recognition", out recognition); var msg = new RecVoiceMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Format = dict["Format"], MediaId = dict["MediaId"], Recognition = recognition }; replyMsg = GetReply <RecVoiceMsg>(key, msg); break; } case "video": { var msg = new RecVideoMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), ThumbMediaId = dict["ThumbMediaId"], MediaId = dict["MediaId"] }; replyMsg = GetReply <RecVideoMsg>(key, msg); break; } case "location": { var msg = new RecLocationMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Label = dict["Label"], Location_X = double.Parse(dict["Location_X"]), Location_Y = double.Parse(dict["Location_Y"]), Scale = int.Parse(dict["Scale"]) }; replyMsg = GetReply <RecLocationMsg>(key, msg); break; } case "link": { var msg = new RecLinkMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Description = dict["Description"], Title = dict["Title"], Url = dict["Url"] }; replyMsg = GetReply <RecLinkMsg>(key, msg); break; } } #endregion } return(replyMsg); }
/// <summary> /// 处理用户消息和事件 /// </summary> /// <returns></returns> public static ReplyBaseMsg ReplyMsg() { Stream inputStream = System.Web.HttpContext.Current.Request.InputStream; long pos = inputStream.Position; inputStream.Position = 0; byte[] buffer = new byte[inputStream.Length]; inputStream.Read(buffer, 0, buffer.Length); inputStream.Position = pos; string xml = System.Text.Encoding.UTF8.GetString(buffer); //消息排重 xml = MsgQueue.GetNoRepeatMsgByXml(xml); if (string.IsNullOrEmpty(xml))//消息重复,不处理 { return(new ReplyEmptyMsg()); } WeiXinSDK.Helpers.Loger.WriteLog("xml", xml); var dict = Util.GetDictFromXml(xml); string key = string.Empty; ReplyBaseMsg replyMsg = ReplyEmptyMsg.Instance; if (dict.ContainsKey("Event")) { #region 接收事件消息 var evt = dict["Event"].ToLower(); key = "event_"; switch (evt) { case "click": //点击菜单拉取消息时的事件推送 { var msg = new EventClickMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Click, EventKey = dict["EventKey"] }; replyMsg = GetReply <EventClickMsg>(key + MyEventType.Click.ToString(), msg); break; } case "view": //点击菜单跳转链接时的事件推送 { var msg = new EventViewMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.View, EventKey = dict["EventKey"] }; replyMsg = GetReply <EventViewMsg>(key + MyEventType.View.ToString(), msg); break; } case "location": //上报地理位置事件 { var msg = new EventLocationMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Location, Latitude = double.Parse(dict["Latitude"]), Longitude = double.Parse(dict["Longitude"]), Precision = double.Parse(dict["Precision"]) }; replyMsg = GetReply <EventLocationMsg>(key + MyEventType.Location.ToString(), msg); break; } case "scan": //用户已关注时扫描带参数二维码的事件推送 { var msg = new EventFansScanMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.FansScan, EventKey = dict["EventKey"], Ticket = dict["Ticket"] }; replyMsg = GetReply <EventFansScanMsg>(key + MyEventType.FansScan.ToString(), msg); break; } case "unsubscribe": //取消关注事件 { var msg = new EventUnattendMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Unattend }; replyMsg = GetReply <EventUnattendMsg>(key + MyEventType.Unattend.ToString(), msg); break; } case "subscribe": //关注事件 { if (dict.ContainsKey("Ticket")) { //用户未关注时,扫描二维码进行关注后的事件推送 var msg = new EventUserScanMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.UserScan, Ticket = dict["Ticket"], EventKey = dict["EventKey"] }; replyMsg = GetReply <EventUserScanMsg>(key + MyEventType.UserScan.ToString(), msg); } else //普通关注事件 { var msg = new EventAttendMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.Attend }; replyMsg = GetReply <EventAttendMsg>(key + MyEventType.Attend.ToString(), msg); } break; } case "masssendjobfinish": //事件推送群发结果 { var msg = new EventMassSendJobFinishMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.MASSSENDJOBFINISH, ErrorCount = int.Parse(dict["ErrorCount"]), FilterCount = int.Parse(dict["FilterCount"]), MsgID = int.Parse(dict["MsgID"]), SentCount = int.Parse(dict["SentCount"]), TotalCount = int.Parse(dict["TotalCount"]), Status = dict["Status"] }; replyMsg = GetReply <EventMassSendJobFinishMsg>(key + MyEventType.MASSSENDJOBFINISH.ToString(), msg); break; } case "merchant_order": //订单付款通知 { var msg = new EventMerchantOrderMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.MerchantOrder, OrderId = dict["OrderId"], OrderStatus = int.Parse(dict["OrderStatus"]), ProductId = dict["ProductId"], SkuInfo = dict["SkuInfo"] }; replyMsg = GetReply <EventMerchantOrderMsg>(key + MyEventType.MerchantOrder.ToString(), msg); break; } case "card_pass_check": //卡券审核成功通知 { var msg = new EventCardPassCheckMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.CardPassCheck, CardId = dict["CardId"] }; replyMsg = GetReply <EventCardPassCheckMsg>(key + MyEventType.CardPassCheck.ToString(), msg); break; } case "card_not_pass_check": //卡券审核失败通知 { var msg = new EventCardPassCheckMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.CardNotPassCheck, CardId = dict["CardId"] }; replyMsg = GetReply <EventCardPassCheckMsg>(key + MyEventType.CardNotPassCheck.ToString(), msg); break; } case "user_get_card": //领取卡券通知 { var msg = new EventUserGetCardMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.UserGetCard, FriendUserName = dict["FriendUserName"], CardId = dict["CardId"], IsGiveByFriend = Convert.ToByte(dict["IsGiveByFriend"]), UserCardCode = dict["UserCardCode"], OuterId = dict["OuterId"] }; replyMsg = GetReply <EventUserGetCardMsg>(key + MyEventType.UserGetCard.ToString(), msg); break; } case "user_del_card": //删除卡券通知 { var msg = new EventUserDelCardMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.UserDelCard, CardId = dict["CardId"], UserCardCode = dict["UserCardCode"] }; replyMsg = GetReply <EventUserDelCardMsg>(key + MyEventType.UserDelCard.ToString(), msg); break; } case "user_consume_card": //核销卡券通知 { var msg = new EventUserConsumeCardMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MyEventType = MyEventType.UserConsumeCard, CardId = dict["CardId"], UserCardCode = dict["UserCardCode"], ConsumeSource = dict["ConsumeSource"] }; replyMsg = GetReply <EventUserConsumeCardMsg>(key + MyEventType.UserConsumeCard.ToString(), msg); break; } } #endregion } else if (dict.ContainsKey("MsgId")) { #region 接收普通消息 var msgType = dict["MsgType"]; key = msgType; switch (msgType) { case "text": { var msg = new RecTextMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Content = dict["Content"] }; replyMsg = GetReply <RecTextMsg>(key, msg); break; } case "image": { var msg = new RecImageMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), PicUrl = dict["PicUrl"], MediaId = dict["MediaId"] }; replyMsg = GetReply <RecImageMsg>(key, msg); break; } case "voice": { string recognition; dict.TryGetValue("Recognition", out recognition); var msg = new RecVoiceMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Format = dict["Format"], MediaId = dict["MediaId"], Recognition = recognition }; replyMsg = GetReply <RecVoiceMsg>(key, msg); break; } case "video": { var msg = new RecVideoMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), ThumbMediaId = dict["ThumbMediaId"], MediaId = dict["MediaId"] }; replyMsg = GetReply <RecVideoMsg>(key, msg); break; } case "location": { var msg = new RecLocationMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Label = dict["Label"], Location_X = double.Parse(dict["Location_X"]), Location_Y = double.Parse(dict["Location_Y"]), Scale = int.Parse(dict["Scale"]) }; replyMsg = GetReply <RecLocationMsg>(key, msg); break; } case "link": { var msg = new RecLinkMsg { CreateTime = Int64.Parse(dict["CreateTime"]), FromUserName = dict["FromUserName"], ToUserName = dict["ToUserName"], MsgId = long.Parse(dict["MsgId"]), Description = dict["Description"], Title = dict["Title"], Url = dict["Url"] }; replyMsg = GetReply <RecLinkMsg>(key, msg); break; } } #endregion } return(replyMsg); }