/// <summary> /// 处理取消关注事件 /// </summary> /// <param name="xmlModel"></param> /// <returns></returns> public string UnSubscribeHandle(ref Dictionary <string, string> xmlModel) { string result = ""; string wxappusername = xmlModel["ToUserName"]; //事件发给哪个公众号ID,这里用于验证是否本公众号的消息,防止虚构数据 string wxopenid = xmlModel["FromUserName"]; //事件是哪个粉丝发起的(粉丝的openid) string strCreateTime = xmlModel["CreateTime"]; if (string.IsNullOrEmpty(strCreateTime)) { //无消息创建时间,无法排重,放弃处理 return(""); } double wxcreatetime; if (!double.TryParse(strCreateTime, out wxcreatetime)) { //无法将createtime转换成功,无法排重,放弃处理 return(""); } DALWXMessageHandleBasic mydal = new DALWXMessageHandleBasic(); #region 事件消息排重 //事件消息排重 bool success = true; success = mydal.EventMsgPreventDuplicates(wxopenid, wxcreatetime); if (!success) { //事件消息排重失败,放弃处理 return(""); } #endregion #region 换取并判断是否存在wxappid string wxappid = WXApiInfo.wxappid; #endregion //WXUser bllWXUser = new WXUser(); //bllWXUser.UnSubscribe(wxopenid, wxappid); //处理取消关注的方法 string errmsg = ""; BLLWXFansManage mybll = new BLLWXFansManage(); mybll.SubscribeLog(wxopenid, 0, ref errmsg);//关注日志 success = mybll.UnSubscribe(wxopenid, ref errmsg); return(result); }
/// <summary> /// 处理关注事件 /// </summary> /// <param name="xmlModel"></param> /// <returns></returns> public string SubscribeHandle(ref Dictionary <string, string> xmlModel) { string result = ""; //返回欢迎语内容 string wxappusername = xmlModel["ToUserName"]; //事件发给哪个开发者微信号(注意这里不是公众号的AppID,而是公众号的微信号) string wxopenid = xmlModel["FromUserName"]; //事件是哪个粉丝发起的(粉丝的openid) string strCreateTime = xmlModel["CreateTime"]; if (string.IsNullOrEmpty(strCreateTime)) { //无消息创建时间,无法排重,放弃处理 return(""); } double wxcreatetime; if (!double.TryParse(strCreateTime, out wxcreatetime)) { //无法将createtime转换成功,无法排重,放弃处理 return(""); } string wxappid = ""; DALWXMessageHandleBasic mydal = new DALWXMessageHandleBasic(); //事件消息排重 bool success = true; success = mydal.EventMsgPreventDuplicates(wxopenid, wxcreatetime); if (!success) { //事件消息排重失败,放弃处理 return(""); } wxappid = WXApiInfo.wxappid; CallWXInterface wxinterface = new CallWXInterface(); //获取粉丝信息,这一步是必须的,因为在明文模式下,要防止虚构粉丝数据 WXFansInfo wxuser = wxinterface.GetUserInfo(wxappid, wxopenid); if (wxuser == null || wxuser.errcode != 0) { return(""); } //根据微信POST过来的事件数据,判断是普通关注还是带参数的二维码关注,如果是带参数二维码关注,需要读取相应的二维码应用信息 string EventKey = ""; if (xmlModel.ContainsKey("EventKey")) { EventKey = xmlModel["EventKey"]; } //注意微信的坑:普通关注的事件是有EventKey的(而文档上面没有)。带参数二维码扫描关注的EventKey是qrscene_开头的。 #region 根据二维码应用信息来处理 //WXUser bllWXUser = new WXUser(); if (string.IsNullOrEmpty(EventKey) || !TextValidation.CheckStringByRegexp(EventKey, "qrscene_.+")) { //普通关注 string errmsg = ""; BLLWXFansManage mybll = new BLLWXFansManage(); mybll.SubscribeLog(wxopenid, 1, ref errmsg);//关注日志 success = mybll.Subscribe(wxopenid, wxuser, ref errmsg); if (success) { result = "欢迎关注每通系统!"; } else { result = errmsg; } } else { //二维码扫描 //result = QRCodeApplication(wxopenid, EventKey.Replace("qrscene_", ""), wxuser); string errmsg = ""; BLLWXFansManage mybll = new BLLWXFansManage(); mybll.SubscribeLog(wxopenid, 1, ref errmsg);//关注日志 success = mybll.QRCodeApplication(wxopenid, EventKey.Replace("qrscene_", ""), wxuser, ref errmsg); if (success) { result = errmsg; } else { result = errmsg; } } #endregion //因为对于关注粉丝,可以自动发送欢迎语或其他内容,所以这里可返回内容 string rspxml = ""; if (!string.IsNullOrEmpty(result)) { rspxml = "<xml><ToUserName><![CDATA[" + xmlModel["FromUserName"] + "]]></ToUserName><FromUserName><![CDATA[" + xmlModel["ToUserName"] + "]]></FromUserName><CreateTime>" + xmlModel["CreateTime"] + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + result + "]]></Content><FuncFlag>0</FuncFlag></xml>"; } return(rspxml); }