void Listen(SubscribeEventHandler func) { do { if (SubscriptionReceived != null) { func(this, ReadData()); } else { ReadData(); } } while (Status == 1); }
/// <summary> /// 解析事件并执行相应逻辑 /// </summary> /// <param name="msgStr">微信post过来的事件数据,xml格式</param> /// <returns>回复给用户的消息,将使用客服接口进行回复</returns> /// <exception cref="NotSupportedException"></exception> public static string ParseEvent(string msgStr) { var eventBase = Xml.Net.XmlConvert.DeserializeObject <EventMessageBase>(msgStr); IWxMsgHandler handler; switch (eventBase.Event.ToLower()) { case "subscribe": // 关注事件 // 分扫码关注和普通关注 handler = new SubscribeEventHandler( ); break; case "unsubscribe": // 取关事件 handler = new UnsubscribeEventHandler( ); break; case "scan": // 已关注用户扫描带参数二维码事件 handler = new ScanWithSubscribeEventHandler( ); break; case "location": // 上报地理位置事件 handler = new LocationEventHandler( ); break; case "click": // 自定义菜单click事件 handler = new ClickEventHandler( ); break; case "view": // 自定义菜单view事件 handler = new ViewEventHandler( ); break; default: // todo: 实现更多事件处理,比如自定义菜单的事件 throw new System.NotSupportedException("不支持的事件类型: " + eventBase.Event); } return(handler.Handle(msgStr)); }