public string stoce() { WeiXinCRMComm dd = new WeiXinCRMComm(); string error = ""; WeiXCommFun wxcomm = new WeiXCommFun(); int wid = 44; string sctokn = dd.getAccessToken(wid, out error); return(sctokn); }
/// <summary> /// 获取微信accessToken; /// </summary> /// <returns></returns> public string Token() { WeiXinCRMComm dd = new WeiXinCRMComm(); string error = ""; WeiXCommFun wxcomm = new WeiXCommFun(); string accessToken = dd.getAccessToken(wid(), out error); return(accessToken); }
protected void Page_Load(object sender, EventArgs e) { string Token = "";//与微信公众账号后台的Token设置保持一致,区分大小写。 int wid = 0; wid = MyCommFun.RequestInt("apiid"); if (wid <= 0) { WriteContent("参数非法"); return; } wx_userweixin wbll = new wx_userweixin(); Token = wbll.GetWeiXinToken(wid); if (Token == null || string.IsNullOrEmpty(Token)) { WriteContent("不存在该微信号或账号已过期或已被禁用!"); return; } // Token = "uweixin"; string signature = Request["signature"]; string timestamp = Request["timestamp"]; string nonce = Request["nonce"]; string echostr = Request["echostr"]; if (Request.HttpMethod == "GET") { //get method - 仅在微信后台填写URL验证时触发 if (CheckSignature.Check(signature, timestamp, nonce, Token)) { WriteContent(echostr); //返回随机字符串则表示验证通过 } else { WriteContent("failed:" + signature + ",token:" + Token + " " + CheckSignature.GetSignature(timestamp, nonce, Token) + "。" + "如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。"); } Response.End(); } else { //本地测试的时候注释掉 ----start ----- if (!CheckSignature.Check(signature, timestamp, nonce, Token)) { WriteContent("参数错误!"); return; } //本地测试的时候注释掉 ----end ----- //post method - 当有用户想公众账号发送消息时触发 Model.wx_userweixin uweixin = wbll.GetModel(wid); var postModel = new PostModel() { Signature = Request.QueryString["signature"], Msg_Signature = Request.QueryString["msg_signature"], Timestamp = Request.QueryString["timestamp"], Nonce = Request.QueryString["nonce"], //以下保密信息不会(不应该)在网络上传播,请注意 Token = Token, EncodingAESKey = uweixin.extStr, //根据自己后台的设置保持一致 AppId = uweixin.AppId //根据自己后台的设置保持一致 }; //v4.2.2之后的版本,可以设置每个人上下文消息储存的最大数量,防止内存占用过多,如果该参数小于等于0,则不限制 var maxRecordCount = 10; //自定义MessageHandler,对微信请求的详细判断操作都在这里面。 var messageHandler = new CustomMessageHandler(Request.InputStream, postModel, maxRecordCount); try { //测试时可开启此记录,帮助跟踪数据,使用前请确保App_Data文件夹存在,且有读写权限。 messageHandler.RequestDocument.Save( Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Request_" + messageHandler.RequestMessage.FromUserName + ".txt")); //执行微信处理过程 messageHandler.Execute(); //测试时可开启,帮助跟踪数据 messageHandler.ResponseDocument.Save( Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Response_" + messageHandler.ResponseMessage.ToUserName + ".txt")); //为了解决官方微信5.0软件换行bug暂时添加的方法,平时用下面一个方法即可 begin string lastStr = ""; if (messageHandler != null && messageHandler.ResponseDocument != null && messageHandler.ResponseDocument.ToString().Trim() != "") { lastStr = messageHandler.ResponseDocument.ToString().Replace("\r\n", "\n"); } else { lastStr = messageHandler.ResponseDocument.ToString(); } // WriteContent( messageHandler.ResponseDocument.ToString()); //为了解决官方微信5.0软件换行bug暂时添加的方法,平时用下面一个方法即可 end //如果自动回复已经关闭,则不返回内容,start 1220 WeiXCommFun wxcomm = new WeiXCommFun(); int apiid = wxcomm.getApiid(); if (!wxcomm.wxCloseKW(apiid)) { lastStr = ""; } //如果自动回复已经关闭,则不返回内容,end 1220 WriteContent(lastStr); return; } catch (Exception ex) { using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt"))) { tw.WriteLine(ex.Message); tw.WriteLine(ex.InnerException.Message); if (messageHandler.ResponseDocument != null) { tw.WriteLine(messageHandler.ResponseDocument.ToString()); } tw.Flush(); tw.Close(); } WriteContent(""); } finally { Response.End(); } } }
/// <summary> /// 处理图片请求 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage) { int apiid = 0; // apiid = wxcomm.getApiid(); WeiXCommFun wxFun = new WeiXCommFun(); //查询微信上墙的活动,只取一条 BLL.wx_sq_act actBll = new BLL.wx_sq_act(); Model.wx_sq_act act = actBll.GetModel(apiid, DateTime.Now); if (act != null) { //查询是否在黑名单里 BLL.wx_sq_heimd hBll = new BLL.wx_sq_heimd(); bool isExist = hBll.Exists(requestMessage.FromUserName, act.id); if (isExist) { //存在黑名单里 return(wxFun.GetResponseMessageTxtByContent(requestMessage, "您在黑名单里,无法上传图片", apiid)); } else { //说明有微信上墙活动 //1 将图片的地址保存到数据库 BLL.wx_sq_piclist pBll = new BLL.wx_sq_piclist(); Model.wx_sq_piclist pic = new Model.wx_sq_piclist(); pic.openid = requestMessage.FromUserName; pic.aid = act.id; pic.picUrl = requestMessage.PicUrl; pic.hasShenghe = false; pic.createDate = DateTime.Now; int ret = pBll.Add(pic); //2返回提示语句 if (ret > 0) { string content = ""; if (act.shenghe) { content = "已经成功上传等待审核!<br/><a href=\"" + MyCommFun.getWebSite() + "/weixin/shangqiang/index.aspx?wid=" + apiid + "&aid=" + act.id + "\">查看相册</a>照片id为" + ret; } else { content = "已经成功上传点击查看<br/><a href=\"" + MyCommFun.getWebSite() + "/weixin/shangqiang/index.aspx?wid=" + apiid + "&aid=" + act.id + "\">查看相册</a>照片id为" + ret; } return(wxFun.GetResponseMessageTxtByContent(requestMessage, content, apiid)); } else { return(wxFun.GetResponseMessageTxtByContent(requestMessage, "图片上传失败,请重新上传", apiid)); } } } else { var responseMessage = CreateResponseMessage <ResponseMessageNews>(); responseMessage.Articles.Add(new Article() { Title = "这是你刚发送的图片", Description = "您发送的图片将会显示在边上", PicUrl = requestMessage.PicUrl, Url = requestMessage.PicUrl }); responseMessage.Articles.Add(new Article() { Title = "快速预约", Description = "第二条带连接的内容", PicUrl = "http://www.hugongll.com/hugongll/dist/img/png/service01.png", Url = "http://www.hugongll.com/hugongll/index.aspx" }); return(responseMessage); // return wxFun.GetResponseMessageTxtByContent(requestMessage, "您刚刚上传了一个图片", apiid); } //var responseMessage = CreateResponseMessage<ResponseMessageNews>(); //responseMessage.Articles.Add(new Article() //{ // Title = "您刚才发送了图片信息", // Description = "您发送的图片将会显示在边上", // PicUrl = requestMessage.PicUrl, // Url = requestMessage.PicUrl //}); //responseMessage.Articles.Add(new Article() //{ // Title = "第二条", // Description = "第二条带连接的内容", // PicUrl = requestMessage.PicUrl, // Url = "http://www.yubom.net" //}); //return responseMessage; }
/// <summary> /// 处理图片请求 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage) { int apiid = wxcomm.getApiid(); wxResponseBaseMgr.Add(apiid, requestMessage.FromUserName, requestMessage.MsgType.ToString(), requestMessage.PicUrl, "none", "", requestMessage.ToUserName); #region ==== 微拍 === var responseMessage = base.CreateResponseMessage <ResponseMessageText>(); BLL.wx_paizhao_setting setBll = new BLL.wx_paizhao_setting(); bool isOpen = setBll.isOpened(apiid); if (isOpen) { if (CurrentMessageContext.RequestMessages.Count > 1) { string keywordStr = ""; for (int i = CurrentMessageContext.RequestMessages.Count - 2; i >= 0; i--) { var historyMessage = CurrentMessageContext.RequestMessages[i]; if (historyMessage.MsgType == RequestMsgType.Text) { keywordStr += (historyMessage as RequestMessageText).Content + ","; } } threeInterface.weipaiInterface wxcf = new threeInterface.weipaiInterface(); if (wxcf.isWeipaiKeyWord(keywordStr, apiid)) { //奥尔图的照片来拉 string content = wxcf.weipaiChuanTuPian(requestMessage.FromUserName, requestMessage.PicUrl, apiid); responseMessage.Content = content; return(responseMessage); } } } #endregion #region 微信上墙 WeiXCommFun wxFun = new WeiXCommFun(); //查询微信上墙的活动,只取一条 BLL.wx_sq_act actBll = new BLL.wx_sq_act(); Model.wx_sq_act act = actBll.GetModel(apiid, DateTime.Now); if (act != null) { //查询是否在黑名单里 BLL.wx_sq_heimd hBll = new BLL.wx_sq_heimd(); bool isExist = hBll.Exists(requestMessage.FromUserName, act.id); if (isExist) { //存在黑名单里 return(wxFun.GetResponseMessageTxtByContent(requestMessage, "您在黑名单里,无法上传图片", apiid)); } else { //说明有微信上墙活动 //1 将图片的地址保存到数据库 BLL.wx_sq_piclist pBll = new BLL.wx_sq_piclist(); Model.wx_sq_piclist pic = new Model.wx_sq_piclist(); pic.openid = requestMessage.FromUserName; pic.aid = act.id; pic.picUrl = requestMessage.PicUrl; pic.hasShenghe = false; pic.createDate = DateTime.Now; int ret = pBll.Add(pic); //2返回提示语句 if (ret > 0) { string content = ""; if (act.shenghe) { if (act.shengheTip == null || act.shengheTip.Trim().Length <= 0) { content = "已经成功上传等待审核!<br/><a href=\"" + MyCommFun.getWebSite() + "/weixin/shangqiang/index.aspx?wid=" + apiid + "&aid=" + act.id + "\">查看相册</a>照片id为" + ret; } else { content = act.shengheTip; } } else { if (act.noshengheTip == null || act.noshengheTip.Trim().Length <= 0) { content = "已经成功上传点击查看<br/><a href=\"" + MyCommFun.getWebSite() + "/weixin/shangqiang/index.aspx?wid=" + apiid + "&aid=" + act.id + "\">查看相册</a>照片id为" + ret; } else { content = act.noshengheTip; } } return(wxFun.GetResponseMessageTxtByContent(requestMessage, content, apiid)); } else { return(wxFun.GetResponseMessageTxtByContent(requestMessage, "图片上传失败,请重新上传", apiid)); } } } else { return(wxFun.GetResponseMessageTxtByContent(requestMessage, "您刚刚上传了一个图片", apiid)); } #endregion //var responseMessage = CreateResponseMessage<ResponseMessageNews>(); //responseMessage.Articles.Add(new Article() //{ // Title = "您刚才发送了图片信息", // Description = "您发送的图片将会显示在边上", // PicUrl = requestMessage.PicUrl, // Url = requestMessage.PicUrl //}); //responseMessage.Articles.Add(new Article() //{ // Title = "第二条", // Description = "第二条带连接的内容", // PicUrl = requestMessage.PicUrl, // Url = "http://m.uweixin.cn" //}); //return responseMessage; }