public ActionResult Index(long id = 0) { Log.Info("进入微信API"); //string token = "weixin_test"; string token = "", signature = "", nonce = "", timestamp = "", echostr = ""; if (id == 0) { var siteSetting = _iSiteSettingService.GetSiteSettings(); token = siteSetting.WeixinToken; } else { var _tmp = _iVShopService.GetVShopSetting(id); if (_tmp == null) { throw new HimallException("错误的商铺编号"); } token = _tmp.Token; } signature = Request["signature"]; nonce = Request["nonce"]; timestamp = Request["timestamp"]; echostr = Request["echostr"]; ActionResult actionResult = Content(""); Log.Info(string.Format("传入信息:signature = {0} , nonce = {1} , timestamp = {2} , echostr = {3} , id = {4}", signature, nonce, timestamp, echostr, id)); if (Request.HttpMethod == "GET") { if (CheckSignature.Check(signature, timestamp, nonce, token)) { actionResult = Content(echostr); } } else { //TODO:[lly] 微信消息处理 if (!CheckSignature.Check(signature, timestamp, nonce, token)) { Log.Info("验证不通过"); } XDocument requestDoc = XDocument.Load(Request.InputStream); #if DEBUG //调试时记录数据日志 Core.Log.Debug(requestDoc); #endif var requestBaseMsg = RequestMessageFactory.GetRequestEntity(requestDoc); SceneHelper helper = new SceneHelper(); //卡券服务 var cardser = _iWXCardService; switch (requestBaseMsg.MsgType) { #region Event处理 case RequestMsgType.Event: var requestMsg = requestBaseMsg as Senparc.Weixin.MP.Entities.RequestMessageEventBase; Log.Info("进入RequestMsgType - Event:" + requestMsg.Event.ToString()); switch (requestMsg.Event) { //取消公众号订阅事件 case Event.unsubscribe: UnSubscribe(requestMsg.FromUserName); break; //订阅公众号事件 case Event.subscribe: //关注红包 actionResult = SendAttentionToUser(requestBaseMsg); #region 关注公众号,欢迎语! int nowtime = ConvertDateTimeInt(DateTime.Now); string msg = "";//关注时,需要发送的内容 var data = WeixinAutoReplyApplication.GetAutoReplyByKey(CommonModel.ReplyType.Follow, "", false, true, true); if (data != null) { msg = data.TextReply; } else Log.Info("请设置关注后自动回复消息内容!"); string resxml = "<xml><ToUserName><![CDATA[" + requestBaseMsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + requestBaseMsg.ToUserName + "]]></FromUserName><CreateTime>" + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>"; Response.Write(resxml); #endregion var requestSubscribe = requestMsg as Senparc.Weixin.MP.Entities.RequestMessageEvent_Subscribe; #if DEBUG Log.Debug(requestSubscribe, new Exception("进入订阅事件")); #endif bool isActivityBonus = false; SceneModel sceneModel = null; #if DEBUG Log.Info("进入订阅事件requestSubscribe.EventKey =" + requestSubscribe.EventKey); #endif #region 场景二维码 if (requestSubscribe.EventKey != string.Empty) { var scene = requestSubscribe.EventKey.Replace("qrscene_", string.Empty); sceneModel = helper.GetModel(scene); if (sceneModel != null) { #if DEBUG Log.Info("sceneModel.Type = " + sceneModel.SceneType.ToString()); #endif if (sceneModel.SceneType == Himall.Model.QR_SCENE_Type.WithDraw) {//提现场景(未关注时) echostr = ProcessWithDrawScene(requestSubscribe, scene, sceneModel); actionResult = Content(echostr); } else if (sceneModel.SceneType == Himall.Model.QR_SCENE_Type.Bonus) { //TODO:ZJT 用户通过场景二维码扫描关注后 推送消息给用户 //isActivityBonus用于判断激发关注事件的是活动红包还是其他 isActivityBonus = true; actionResult = SendActivityToUser(sceneModel.Object, requestBaseMsg); } else if (sceneModel.SceneType == Himall.Model.QR_SCENE_Type.FlashSaleRemind) { #if DEBUG Log.Info("进入限时购场景二维码事件"); #endif long flashSaleId = (long)sceneModel.Object; SendFlashSaleRemindMessage(flashSaleId, requestBaseMsg.FromUserName); } else if (sceneModel.SceneType == Himall.Model.QR_SCENE_Type.Binding)//绑定微信 { echostr = Binding(requestSubscribe, scene, sceneModel); actionResult = Content(echostr); } } } #endregion Log.Info("开始isActivityBonus=" + isActivityBonus); //通过场景二维码进来,并且二维码不是活动红包,则进行关注送红包事件 //if (!isActivityBonus && sceneModel != null && sceneModel.SceneType == QR_SCENE_Type.Bonus) //{ // actionResult = SendAttentionToUser(requestBaseMsg); //} #if DEBUG Log.Info("开始Subscribe requestBaseMsg.FromUserName="******"进入限时购场景二维码事件"); long flashSaleId = (long)sceneModel.Object; SendFlashSaleRemindMessage(flashSaleId, requestBaseMsg.FromUserName); } else if (sceneModel.SceneType == Himall.Model.QR_SCENE_Type.Binding)//绑定微信 { echostr = Binding(requestScan, requestScan.EventKey, sceneModel); actionResult = Content(echostr); } } } break; case Event.card_pass_check: //TODO:DZY[150907] 卡券审核通过 var reqpasscard = requestMsg as Senparc.Weixin.MP.Entities.RequestMessageEvent_Card_Pass_Check; if (!string.IsNullOrWhiteSpace(reqpasscard.CardId)) { cardser.Event_Audit(reqpasscard.CardId, WXCardLogInfo.AuditStatusEnum.Audited); } break; case Event.card_not_pass_check: //TODO:DZY[150907] 卡券审核失败 var reqnopasscard = requestMsg as Senparc.Weixin.MP.Entities.RequestMessageEvent_Card_Pass_Check; if (!string.IsNullOrWhiteSpace(reqnopasscard.CardId)) { cardser.Event_Audit(reqnopasscard.CardId, WXCardLogInfo.AuditStatusEnum.AuditNot); } break; case Event.user_del_card: //TODO:DZY[150907] 删除卡包内优惠券 var requdelcard = requestMsg as Senparc.Weixin.MP.Entities.RequestMessageEvent_User_Del_Card; if (!string.IsNullOrWhiteSpace(requdelcard.CardId) && !string.IsNullOrWhiteSpace(requdelcard.UserCardCode)) { cardser.Event_Unavailable(requdelcard.CardId, requdelcard.UserCardCode); } break; case Event.user_get_card: //TODO:DZY[150907] 用户获取优惠券 var requgetcard = requestMsg as Senparc.Weixin.MP.Entities.RequestMessageEvent_User_Get_Card; Log.Debug("WXCard:" + requgetcard.CardId + "_" + requgetcard.UserCardCode); if (!string.IsNullOrWhiteSpace(requgetcard.CardId) && !string.IsNullOrWhiteSpace(requgetcard.UserCardCode)) { cardser.Event_Send(requgetcard.CardId, requgetcard.UserCardCode, requgetcard.FromUserName, requgetcard.OuterId); } break; } break; #endregion case RequestMsgType.Text: textCase(requestBaseMsg); break; } Response.End(); } return actionResult; }
public ActionResult Index() { Log.Info("进入微信API"); SiteSettingsInfo siteSettings = ServiceHelper.Create <ISiteSettingService>().GetSiteSettings(); string weixinToken = ""; string item = ""; string str = ""; string item1 = ""; string str1 = ""; weixinToken = siteSettings.WeixinToken; item = base.Request["signature"]; str = base.Request["nonce"]; item1 = base.Request["timestamp"]; str1 = base.Request["echostr"]; ActionResult user = base.Content(""); if (base.Request.HttpMethod != "GET") { if (!Senparc.Weixin.MP.CheckSignature.Check(item, item1, str, weixinToken)) { Log.Info("验证不通过"); } XDocument xDocument = XDocument.Load(base.Request.InputStream); Senparc.Weixin.MP.Entities.IRequestMessageBase requestEntity = Senparc.Weixin.MP.RequestMessageFactory.GetRequestEntity(xDocument, null); SceneHelper sceneHelper = new SceneHelper(); IWXCardService wXCardService = ServiceHelper.Create <IWXCardService>(); if (requestEntity.MsgType == RequestMsgType.Event) { RequestMessageEventBase requestMessageEventBase = requestEntity as RequestMessageEventBase; Event @event = requestMessageEventBase.Event; switch (@event) { case Event.subscribe: { RequestMessageEvent_Subscribe requestMessageEventSubscribe = requestMessageEventBase as RequestMessageEvent_Subscribe; bool flag = false; if (requestMessageEventSubscribe.EventKey != string.Empty) { string str2 = requestMessageEventSubscribe.EventKey.Replace("qrscene_", string.Empty); SceneModel model = sceneHelper.GetModel(str2); if (model != null) { if (model.SceneType == QR_SCENE_Type.WithDraw) { flag = true; str1 = ProcessWithDrawScene(requestMessageEventSubscribe, str2, model); user = base.Content(str1); } else if (model.SceneType == QR_SCENE_Type.Bonus) { flag = true; user = SendActivityToUser(sceneHelper.GetModel(str2).Object, requestEntity); } } } if (!flag) { user = SendAttentionToUser(requestEntity); } Subscribe(requestEntity.FromUserName); break; } case Event.unsubscribe: { UnSubscribe(requestMessageEventBase.FromUserName); break; } case Event.CLICK: { break; } case Event.scan: { RequestMessageEvent_Scan requestMessageEventScan = requestMessageEventBase as RequestMessageEvent_Scan; if (string.IsNullOrWhiteSpace(requestMessageEventScan.EventKey)) { break; } SceneModel sceneModel = sceneHelper.GetModel(requestMessageEventScan.EventKey); if (sceneModel == null || sceneModel.SceneType != QR_SCENE_Type.WithDraw) { break; } str1 = ProcessWithDrawScene(requestMessageEventScan, requestMessageEventScan.EventKey, sceneModel); user = base.Content(str1); break; } default: { switch (@event) { case Event.card_pass_check: { RequestMessageEvent_Card_Pass_Check requestMessageEventCardPassCheck = requestMessageEventBase as RequestMessageEvent_Card_Pass_Check; if (string.IsNullOrWhiteSpace(requestMessageEventCardPassCheck.CardId)) { return(user); } wXCardService.Event_Audit(requestMessageEventCardPassCheck.CardId, WXCardLogInfo.AuditStatusEnum.Audited); break; } case Event.card_not_pass_check: { RequestMessageEvent_Card_Pass_Check requestMessageEventCardPassCheck1 = requestMessageEventBase as RequestMessageEvent_Card_Pass_Check; if (string.IsNullOrWhiteSpace(requestMessageEventCardPassCheck1.CardId)) { return(user); } wXCardService.Event_Audit(requestMessageEventCardPassCheck1.CardId, WXCardLogInfo.AuditStatusEnum.AuditNot); break; } case Event.user_get_card: { RequestMessageEvent_User_Get_Card requestMessageEventUserGetCard = requestMessageEventBase as RequestMessageEvent_User_Get_Card; if (string.IsNullOrWhiteSpace(requestMessageEventUserGetCard.CardId) || string.IsNullOrWhiteSpace(requestMessageEventUserGetCard.UserCardCode)) { return(user); } wXCardService.Event_Send(requestMessageEventUserGetCard.CardId, requestMessageEventUserGetCard.UserCardCode, requestMessageEventUserGetCard.FromUserName, requestMessageEventUserGetCard.OuterId); break; } case Event.user_del_card: { RequestMessageEvent_User_Del_Card requestMessageEventUserDelCard = requestMessageEventBase as RequestMessageEvent_User_Del_Card; if (string.IsNullOrWhiteSpace(requestMessageEventUserDelCard.CardId) || string.IsNullOrWhiteSpace(requestMessageEventUserDelCard.UserCardCode)) { return(user); } wXCardService.Event_Unavailable(requestMessageEventUserDelCard.CardId, requestMessageEventUserDelCard.UserCardCode); break; } } break; } } } } else if (Senparc.Weixin.MP.CheckSignature.Check(item, item1, str, weixinToken)) { user = base.Content(str1); } return(user); }