public ActionResult Get(PostModel postModel, string echostr, string appId) { int wxId = 0; if (string.IsNullOrWhiteSpace(appId) && !int.TryParse(appId, out wxId)) { return(Content("未知平台接入!")); } wxId = Convert.ToInt32(appId); Weixin_Sys_Info wxInfo = null; using (SeekAskContext context = new SeekAskContext()) { wxInfo = context.Weixin_Sys_Info.FirstOrDefault(p => p.WxId == wxId); if (wxInfo == null) { return(Content("平台未新增管理代号(" + appId + ")!")); } bool isCheckSignature = CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, wxInfo.Wx_Token); #region 记录接入日志 try { Szx_Sys_Log log = new Szx_Sys_Log() { LogID = Guid.NewGuid().ToString("N"), LogName = "微信接入验证", Source = "01", LevelCode = 0, RequestUrl = HttpContext.Request.Url.ToString(), LogDate = DateTime.Now, Message = "验证结果:" + isCheckSignature, Create_Id = "wxjk", Create_Name = "微信接口", Create_Time = DateTime.Now, Create_IP = WebSiteTools.GetRequestIP() }; context.Szx_Sys_Log.Add(log); context.SaveChanges(); } catch (Exception) { } #endregion if (isCheckSignature) { try { wxInfo.Wx_Status = 1; //状态改为已对接 context.SaveChanges(); } catch (Exception) { } return(Content(echostr)); //返回随机字符串则表示验证通过 } else { return(Content("failed:" + postModel.Signature + "," + CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, wxInfo.Wx_Token) + "。" + "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。")); } } }
public ActionResult Post(PostModel postModel, string appId) { int wxId = 0; if (string.IsNullOrWhiteSpace(appId) && !int.TryParse(appId, out wxId)) { return(Content("未知平台接入!")); } wxId = Convert.ToInt32(appId); Weixin_Sys_Info wxInfo = null; using (SeekAskContext context = new SeekAskContext()) { wxInfo = context.Weixin_Sys_Info.FirstOrDefault(p => p.WxId == wxId); if (wxInfo == null) { return(Content("平台未新增管理代号(" + appId + ")!")); } if (!CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, wxInfo.Wx_Token)) { return(Content("参数错误!")); } postModel.Token = wxInfo.Wx_Token; //根据自己后台的设置保持一致 postModel.EncodingAESKey = wxInfo.Wx_EncodingAESKey; //根据自己后台的设置保持一致 postModel.AppId = wxInfo.Wx_AppId; //根据自己后台的设置保持一致 var maxRecordCount = 10; //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, maxRecordCount); try { /* 如果需要添加消息去重功能,只需打开OmitRepeatedMessage功能,SDK会自动处理。 * 收到重复消息通常是因为微信服务器没有及时收到响应,会持续发送2-5条不等的相同内容的RequestMessage*/ messageHandler.OmitRepeatedMessage = true; //执行微信处理过程 messageHandler.Execute(); #region 记录微信操作日志 try { if (!messageHandler.CancelExcute) { Weixin_Msg_Request msgRequest = new Weixin_Msg_Request(); msgRequest.WxId = wxInfo.WxId; msgRequest.MsgType = messageHandler.RequestMessage.MsgType.ToString(); if (messageHandler.RequestMessage.MsgType != RequestMsgType.Event) { msgRequest.MsgId = messageHandler.RequestMessage.MsgId.ToString(); } else { msgRequest.MsgId = Guid.NewGuid().ToString("N"); } msgRequest.FromUserName = messageHandler.RequestMessage.FromUserName; msgRequest.ToUserName = messageHandler.RequestMessage.ToUserName; msgRequest.Encrypt = null; msgRequest.CreateTime = messageHandler.RequestMessage.CreateTime; msgRequest.XmlDocument = messageHandler.RequestDocument.ToString(); context.Weixin_Msg_Request.Add(msgRequest); context.SaveChanges(); } } catch (Exception) { } #endregion //return Content(messageHandler.ResponseDocument.ToString());//v0.7- //return new FixWeixinBugWeixinResult(messageHandler);//为了解决官方微信5.0软件换行bug暂时添加的方法,平时用下面一个方法即可 return(new WeixinResult(messageHandler));//v0.8+ } catch (Exception ex) { #region 记录系统日志 try { Szx_Sys_Log log = new Szx_Sys_Log() { LogID = Guid.NewGuid().ToString("N"), LogName = "微信消息处理", Source = "01", LevelCode = 2, RequestUrl = HttpContext.Request.Url.ToString(), LogDate = DateTime.Now, Message = "处理异常:" + ex.Message, Create_Id = "wxjk", Create_Name = "微信接口", Create_Time = DateTime.Now, Create_IP = WebSiteTools.GetRequestIP() }; context.Szx_Sys_Log.Add(log); context.SaveChanges(); } catch (Exception) { } #endregion } } return(Content("参数错误!")); }
public ActionResult BindWeiXinByAccount(string wxUserID, string wxUserPwd) { StatusBackModel errBack = new StatusBackModel(); try { using (SmartBindUtil smartBind = new SmartBindUtil(wxUserID, wxUserPwd)) { //登录成功后,在开发者中心获取开发者信息 WechatDevInfo devInfo = smartBind.GetWechatDev(); WechatAccountInfo accountInfo = smartBind.GetAccount(); //无法获取用户信息,结束操作并跳出 if (accountInfo == null) { errBack.ErrMsg = "无法获取用户信息,结束操作并跳出"; return(Json(errBack)); } if (devInfo != null && !string.IsNullOrWhiteSpace(devInfo.AppSecret)) { devInfo.AppSecret = devInfo.AppSecret.Replace("重置", "").Trim(); if (devInfo.AppSecret.Contains("显示密钥")) { devInfo.AppSecret = null; } } Weixin_Sys_Info info = null; using (SeekAskContext context = new SeekAskContext()) { #region 更新微信号管理 info = context.Weixin_Sys_Info.FirstOrDefault(p => p.Wx_AccountId == accountInfo.AccountId && p.Wx_WechatNumber == accountInfo.WechatNumber && p.Wx_AppId == devInfo.AppId); if (info == null) { info = new Weixin_Sys_Info(); info.Create_Id = "Sys"; info.Create_Name = "系统管理员"; info.Create_IP = WebSiteTools.GetRequestIP(); info.Create_Time = DateTime.Now; info = context.Weixin_Sys_Info.Add(info); } info.Wx_Status = 0; info.Wx_AccountId = accountInfo.AccountId; info.Wx_AccountName = accountInfo.AccountName; info.Wx_WechatNumber = accountInfo.WechatNumber; info.Wx_WechatType = (int)accountInfo.WechatType; info.Wx_Introduces = accountInfo.Introduces; info.Wx_Authenticate = (int)accountInfo.Authenticate; info.Wx_PlaceAddress = accountInfo.PlaceAddress; info.Wx_SubjectInfo = accountInfo.SubjectInfo; info.Wx_LoginEmail = accountInfo.LoginEmail; info.Wx_AppId = devInfo.AppId; if (!string.IsNullOrWhiteSpace(devInfo.AppSecret)) { info.Wx_AppSecret = devInfo.AppSecret; } info.Wx_URL = devInfo.URL; info.Wx_EncodingAESKey = devInfo.EncodingAESKey; info.Wx_EncodingAESType = (int)devInfo.EncodingAESType; info.Modify_Id = "Sys"; info.Modify_Name = "系统管理员"; info.Modify_IP = WebSiteTools.GetRequestIP(); info.Modify_Time = DateTime.Now; info.Is_Deleted = false; context.SaveChanges(); #endregion #region 载头像及设置开发模式 //下载头像 if (!string.IsNullOrWhiteSpace(accountInfo.HeadImage)) { string fileName = "weixinImg/headImg_" + info.WxId + ".jpg"; int downStatus = smartBind.DownloadImg(accountInfo.HeadImage, Server.MapPath("~/") + fileName, true); if (downStatus == (int)HttpStatusCode.OK) { info.Wx_HeadImage = "/" + fileName; } } //下载二维码 if (!string.IsNullOrWhiteSpace(accountInfo.HeadImage)) { string fileName = "weixinImg/qrCode_" + info.WxId + ".jpg"; int downStatus = smartBind.DownloadImg(accountInfo.QRCode, Server.MapPath("~/") + fileName, true); if (downStatus == (int)HttpStatusCode.OK) { info.Wx_QRCode = "/" + fileName; } } //info.Wx_URL = "http://sjianshang.xicp.net/seekask.ui/weixin?appid=" + info.WxId; info.Wx_URL = "http://wx.seekask.cn/weixin?appid=" + info.WxId; info.Wx_Token = WeChatTools.GetRandomStr(20); info.Wx_EncodingAESKey = WeChatTools.GetRandomStr(43); info.Wx_EncodingAESType = 2; context.SaveChanges(); //设置启用开发模式 int status = smartBind.EnabledDev(1, 2); //启用开发模式失败,结束操作并跳出 if (status != 0) { return(Content("启用开发模式失败,结束操作并跳出")); } // 验证服务器接口回调,此处修改服务器配置中的URL和Token status = smartBind.SetDevServiceUrl( info.Wx_URL, info.Wx_Token, info.Wx_EncodingAESKey, info.Wx_EncodingAESType.ToString()); #endregion errBack.BackData = info.WxId; if (status == 0) { errBack.ErrCode = 1; errBack.ErrMsg = "接入成功!"; } else { errBack.ErrMsg = "用户信息获取成功,接入失败!"; } } }; } catch (Exception) { errBack.ErrMsg = "执行异常"; } return(Json(errBack)); }