/// <summary> /// 主动给用户推送订单消息 /// </summary> /// <returns></returns> public string SetOrderPush() { var respData = new RespData(); if (string.IsNullOrEmpty(Request["WeiXinId"]) || string.IsNullOrEmpty(Request["OpenId"]) || string.IsNullOrEmpty(Request["OrdeNo"])) { respData.Code = "103"; respData.Description = "数据库操作错误"; respData.Exception = "请求的数据不能为空"; return(respData.ToJSON()); } string content = string.Empty; try { string vipID = string.Empty; string vipName = string.Empty; var loggingSessionInfo = Default.GetLjLoggingSession(); //根据客户标识获取连接字符串 qianzhi 2013-07-30 if (!string.IsNullOrEmpty(Request["customerId"])) { loggingSessionInfo = Default.GetBSLoggingSession(Request["customerId"].Trim(), ""); } #region 获取VIP信息 VipBLL vipService = new VipBLL(loggingSessionInfo); var vipList = vipService.QueryByEntity(new VipEntity() { WeiXinUserId = Request["OpenId"], WeiXin = Request["WeiXinId"] }, null); if (vipList == null || vipList.Length == 0) { respData.Code = "103"; respData.Description = "未查找到匹配的VIP信息"; return(respData.ToJSON()); } else { vipID = vipList.FirstOrDefault().VIPID; vipName = vipList.FirstOrDefault().VipName; } #endregion // 推送消息 string msgUrl = ConfigurationManager.AppSettings["push_weixin_msg_url"].Trim(); string msgText = string.Format("亲爱的会员{1},您单号为{0}的购买请求我们已经收到,请您到指定渠道下交纳钱款。谢谢再次惠顾!", Request["OrdeNo"], vipName); string msgData = "<xml><OpenID><![CDATA[" + Request["OpenId"] + "]]></OpenID><Content><![CDATA[" + msgText + "]]></Content></xml>"; var msgResult = Common.Utils.GetRemoteData(msgUrl, "POST", msgData); #region 发送日志 MarketSendLogBLL logServer = new MarketSendLogBLL(loggingSessionInfo); MarketSendLogEntity logInfo = new MarketSendLogEntity(); logInfo.LogId = BaseService.NewGuidPub(); logInfo.IsSuccess = 1; logInfo.MarketEventId = Request["OrdeNo"]; logInfo.SendTypeId = "2"; logInfo.TemplateContent = msgData; logInfo.VipId = vipID; logInfo.WeiXinUserId = Request["OpenId"]; logInfo.CreateTime = System.DateTime.Now; logServer.Create(logInfo); #endregion Loggers.Debug(new DebugLogInfo() { Message = string.Format("PushMsgResult:{0}", msgResult) }); } catch (Exception ex) { respData.Code = "201"; respData.Description = "操作失败"; respData.Exception = ex.ToString(); } content = respData.ToJSON(); return(content); }
public void PushWeiXin(string OpenId, LoggingSessionInfo loggingSessionInfo, string OrderId, string VipId) { string webUrl = ConfigurationManager.AppSettings["website_url"]; string msgUrl = ConfigurationManager.AppSettings["push_weixin_msg_url"];; Random rad = new Random(); string msgText = "<a href='" + webUrl + "wap/Event/20131109/aboutEvent.htm'>点击查看本次活动议程</a>"; string msgData = "<xml><OpenID><![CDATA[" + OpenId + "]]></OpenID>" + "<Content><![CDATA[" + msgText + "]]></Content></xml>"; //var msgResult = Common.Utils.GetRemoteData(msgUrl, "POST", msgData); #region 发送日志 MarketSendLogBLL logServer = new MarketSendLogBLL(loggingSessionInfo); MarketSendLogEntity logInfo = new MarketSendLogEntity(); logInfo.LogId = BaseService.NewGuidPub(); logInfo.IsSuccess = 1; logInfo.MarketEventId = OrderId; logInfo.SendTypeId = "2"; logInfo.TemplateContent = msgData; logInfo.VipId = VipId; logInfo.WeiXinUserId = OpenId; logInfo.CreateTime = System.DateTime.Now; logServer.Create(logInfo); #endregion #region msgText = "亲爱的,为答谢您参加本次活动,主办方推出多个奖品的刮刮卡等你来刮,快来试试运气,<a href='" + webUrl + "OnlineClothing/1109guagua.html?customerId=" + loggingSessionInfo.CurrentUser.customer_id + "&userId=" + VipId + "&openId=" + OpenId + "&eventId=" + OrderId + "'>点击试试手气</a>"; //msgData = "<xml><OpenID><![CDATA[" + OpenId + "]]></OpenID>" // + "<Content><![CDATA[" + msgText + "]]></Content></xml>"; LEventsBLL eventServer = new LEventsBLL(loggingSessionInfo); LEventsEntity eventInfo = new LEventsEntity(); eventInfo = eventServer.GetByID(OrderId); if (eventInfo != null && eventInfo.EventID != null) { string picUrl1 = eventInfo.Content; string Url1 = "" + webUrl + "OnlineClothing/1109guagua.html?customerId=" + loggingSessionInfo.CurrentUser.customer_id + "&userId=" + VipId + "&openId=" + OpenId + "&eventId=" + OrderId + ""; msgData = "<xml>" + "<OpenID><![CDATA[" + OpenId + "]]></OpenID>" + "<MsgType><![CDATA[news]]></MsgType>" + "<Articles>" + "<item>" + "<Title><![CDATA[活动刮刮卡,惊喜享不停!]]></Title> " + "<Description><![CDATA[亲!为答谢您来参加本次活动,我们特别推出刮刮卡,大量精美奖品等你来取,快来试试运气吧。]]></Description> " + "<Url><![CDATA[" + Url1 + "]]></Url> " + "<PicUrl><![CDATA[" + picUrl1 + "]]></PicUrl> " + "</item>" + "</Articles>" + "</xml>"; var msgResult1 = Common.Utils.GetRemoteData(msgUrl, "POST", msgData); } #endregion #region 发送日志 MarketSendLogEntity logInfo1 = new MarketSendLogEntity(); logInfo1.LogId = BaseService.NewGuidPub(); logInfo1.IsSuccess = 1; logInfo1.MarketEventId = OrderId; logInfo1.SendTypeId = "2"; logInfo1.TemplateContent = msgData; logInfo1.VipId = VipId; logInfo1.WeiXinUserId = OpenId; logInfo1.CreateTime = System.DateTime.Now; logServer.Create(logInfo1); #endregion }
/// <summary> /// 获取Access Token /// </summary> /// <param name="code"></param> private void GetAccessToken(string code) { MarketSendLogBLL sendServer = new MarketSendLogBLL(loggingSessionInfo); MarketSendLogEntity sendInfo = new MarketSendLogEntity(); Random rad = new Random(); int iRad = rad.Next(1000, 100000); try { sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 1; sendInfo.MarketEventId = "GetAccessToken"; sendInfo.SendTypeId = "220"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = "GetAccessToken:code:" + code + ",strAppId:" + strAppId + ",strAppSecret:" + strAppSecret; sendInfo.VipId = code; sendInfo.WeiXinUserId = "1111"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); var sendObjList = sendServer.QueryByEntity(new MarketSendLogEntity { VipId = code , WeiXinUserId = "AccessToken" , IsDelete = 0 }, null); string data = string.Empty; if (sendObjList == null || sendObjList.Length == 0 || sendObjList[0] == null) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; string url = "https://api.weixin.qq.com/sns/oauth2/access_token"; WebClient myWebClient = new WebClient(); // 注意这种拼字符串的ContentType myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // 转化成二进制数组 var postData = "appid=" + strAppId + "&secret=" + strAppSecret + "&code=" + code + "&grant_type=authorization_code"; sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 1; sendInfo.MarketEventId = "GetAccessToken转化成二进制数组"; sendInfo.SendTypeId = "220"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("{0}", postData); sendInfo.VipId = code; sendInfo.WeiXinUserId = "AccessToken-Para"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 上传数据,并获取返回的二进制数据. byte[] responseArray = myWebClient.UploadData(url, "POST", byteArray); data = System.Text.Encoding.UTF8.GetString(responseArray); if (data.IndexOf("errcode") > 1) { var sendObjList1 = sendServer.QueryByEntity(new MarketSendLogEntity { VipId = code , WeiXinUserId = "AccessToken" , IsDelete = 0 , IsSuccess = 1 }, null); if (sendObjList1 == null || sendObjList1.Length == 0 || sendObjList1[0] == null) { } else { data = sendObjList1[0].TemplateContent.ToString().Trim(); } } sendInfo.LogId = BaseService.NewGuidPub(); if (data.IndexOf("errcode") > 1) { sendInfo.IsSuccess = 0; } else { sendInfo.IsSuccess = 1; } sendInfo.MarketEventId = "GetAccessToken-3"; sendInfo.SendTypeId = "220"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("{0}", data); sendInfo.VipId = code; sendInfo.WeiXinUserId = "AccessToken"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); } else { data = sendObjList[0].TemplateContent.ToString().Trim(); sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 1; sendInfo.MarketEventId = "GetAccessToken-3-2"; sendInfo.SendTypeId = "220"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("{0}", data); sendInfo.VipId = code; sendInfo.WeiXinUserId = "AccessToken--重复2"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); } var tokenInfo = data.DeserializeJSONTo <cAccessTokenReturn>(); //Response.Write("<br/>"); //Response.Write("获取Access Token"); if (tokenInfo != null) { //Response.Write("<br/>"); //Response.Write("获取Access Token不为空"); GetUserIdByOpenId(tokenInfo.openid); } Loggers.Debug(new DebugLogInfo() { Message = string.Format("GetAccessToken: {0}", data) }); } catch (Exception ex) { Loggers.Exception(new ExceptionLogInfo() { ErrorMessage = string.Format("GetAccessToken错误: {0}", ex.ToString()) }); Response.Write("向微信请求认证Access Token出错,请联系管理员尽快处理."); sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 0; sendInfo.MarketEventId = "向微信请求认证Access Token出错"; sendInfo.SendTypeId = "200"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("GetAccessToken错误: {0}", ex.ToString()); sendInfo.VipId = code; sendInfo.WeiXinUserId = "1111"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); Response.End(); } }
/// <summary> /// 第二步:通过code换取网页授权access_token /// </summary> /// <param name="code">填写第一步获取的code参数</param> /// <param name="strAppId">公众号的唯一标识</param> /// <param name="strAppSecret">公众号的appsecret</param> /// <param name="loggingSessionInfo"></param> /// <param name="iRad"></param> /// <returns></returns> public string GetAccessToken(string code, string strAppId, string strAppSecret, LoggingSessionInfo loggingSessionInfo, int iRad, out string token, string openOAuthAppid = null) { MarketSendLogBLL sendServer = new MarketSendLogBLL(loggingSessionInfo); MarketSendLogEntity sendInfo = new MarketSendLogEntity(); //Random rad = new Random(); //int iRad = rad.Next(1000, 100000); try { sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 1; sendInfo.MarketEventId = "GetAccessToken-1"; sendInfo.SendTypeId = "200"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = "GetAccessToken:code:" + code + ",strAppId:" + strAppId + ",strAppSecret:" + strAppSecret; sendInfo.VipId = code; sendInfo.WeiXinUserId = "1111"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); var sendObjList = sendServer.QueryByEntity(new MarketSendLogEntity { VipId = code , WeiXinUserId = "AccessToken" , IsDelete = 0 }, null); string data = string.Empty; if (sendObjList == null || sendObjList.Length == 0 || sendObjList[0] == null) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3; string url = "https://api.weixin.qq.com/sns/oauth2/access_token"; WebClient myWebClient = new WebClient(); // 注意这种拼字符串的ContentType myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded"); // 转化成二进制数组 var postData = string.Empty; if (string.IsNullOrEmpty(strAppSecret))//已登录授权了 { if (string.IsNullOrEmpty(openOAuthAppid)) { var wailist = new WApplicationInterfaceBLL(loggingSessionInfo).QueryByEntity(new WApplicationInterfaceEntity { AppID = strAppId, CustomerId = loggingSessionInfo.ClientID }, null).FirstOrDefault(); openOAuthAppid = wailist != null ? wailist.OpenOAuthAppid : string.Empty; } //openOAuthAppid = !string.IsNullOrEmpty(openOAuthAppid) ? openOAuthAppid : "wx691c2f2bbac04b4b"; var openOAuthUrl = ConfigurationManager.AppSettings["openOAuthUrl"]; var openuri = openOAuthUrl + "/OpenOAuth/GetComponentAccessToken"; var opendata = CommonBLL.GetRemoteData(openuri, "GET", string.Empty).Replace("\"", "");//先获取第三方平台的component_access_token var url2 = "https://api.weixin.qq.com/sns/oauth2/component/access_token?appid=" + strAppId + "&code=" + code + "&grant_type=authorization_code&component_appid=" + openOAuthAppid + "&component_access_token=" + opendata + ""; opendata = CommonBLL.GetRemoteData(url2, "GET", string.Empty); data = opendata; } else//未授权的情况下 { postData = "appid=" + strAppId + "&secret=" + strAppSecret + "&code=" + code + "&grant_type=authorization_code"; sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 1; sendInfo.MarketEventId = "GetAccessToken-2"; sendInfo.SendTypeId = "200"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("{0}", postData); sendInfo.VipId = code; sendInfo.WeiXinUserId = "GetAccessToken-Para"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); byte[] byteArray = Encoding.UTF8.GetBytes(postData); // 上传数据,并获取返回的二进制数据. byte[] responseArray = myWebClient.UploadData(url, "POST", byteArray); data = System.Text.Encoding.UTF8.GetString(responseArray); } if (data.IndexOf("errcode") > 1) { var sendObjList1 = sendServer.QueryByEntity(new MarketSendLogEntity { VipId = code , WeiXinUserId = "AccessToken" , IsDelete = 0 , IsSuccess = 1 }, null); if (sendObjList1 == null || sendObjList1.Length == 0 || sendObjList1[0] == null) { } else { data = sendObjList1[0].TemplateContent.ToString().Trim(); } } sendInfo.LogId = BaseService.NewGuidPub(); if (data.IndexOf("errcode") > 1) { sendInfo.IsSuccess = 0; } else { sendInfo.IsSuccess = 1; } sendInfo.MarketEventId = "GetAccessToken-3"; sendInfo.SendTypeId = "200"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("{0}", data); sendInfo.VipId = code; sendInfo.WeiXinUserId = "AccessToken"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); } else { data = sendObjList[0].TemplateContent.ToString().Trim(); sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 1; sendInfo.MarketEventId = "GetAccessToken-3-2"; sendInfo.SendTypeId = "200"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("{0}", data); sendInfo.VipId = code; sendInfo.WeiXinUserId = "AccessToken--重复2"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); } var tokenInfo = data.DeserializeJSONTo <cAccessTokenReturn>(); //Response.Write("<br/>"); //Response.Write("获取Access Token"); token = ""; if (tokenInfo != null) { //Response.Write("<br/>"); //Response.Write("获取Access Token不为空"); token = tokenInfo.access_token; return(tokenInfo.openid); } else { return(""); } Loggers.Debug(new DebugLogInfo() { Message = string.Format("GetAccessToken: {0}", data) }); //Response.Write(data); } catch (Exception ex) { Loggers.Debug(new DebugLogInfo() { Message = string.Format("GetAccessToken错误: {0}", ex.ToString()) }); sendInfo.LogId = BaseService.NewGuidPub(); sendInfo.IsSuccess = 0; sendInfo.MarketEventId = "GetAccessToken-4"; sendInfo.SendTypeId = "200"; sendInfo.Phone = iRad.ToString(); sendInfo.TemplateContent = string.Format("GetAccessToken错误: {0}", ex.ToString()); sendInfo.VipId = code; sendInfo.WeiXinUserId = "1111"; sendInfo.CreateTime = System.DateTime.Now; sendServer.Create(sendInfo); token = ""; return(""); } }