public void ProcessRequest(HttpContext context) { siteCode = context.Request.QueryString["id"]; if (!string.IsNullOrEmpty(siteCode)) { WXConfigDAL dal = new WXConfigDAL(); wxConfig = dal.GetWXConfigBySiteCode(siteCode); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; weixin = new WeiXinCore.WeiXin(weixinConfig); weixin.OnMsgReceiving = MsgReceiving; weixin.OnTextMsgReceived = TextMsgReceive; weixin.OnImageMsgReceived = ImageMsgReceive; weixin.OnSubscribeEvent = SubscribeProcess; weixin.OnMenuClick = MenuClickProcess; //MenuDAL.CreateWeiXinMenu(wxConfig.ID); //string json=WeiXinHelper.GetCustomMenu(weixin.GetAccessToken().Access_Token); //string fileDir = HttpContext.Current.Server.MapPath("/HP_PHOTO/"); //if (!Directory.Exists(fileDir)) //{ // Directory.CreateDirectory(fileDir); //} //string fileName = weixin.SaveAsMedia("WVNXJOLqpySbvX1T7zGCKYjboBbcv2x4YOgz9e0Avbq_xks5mGujx10avfPQ3ZlW", fileDir); //string file = HttpContext.Current.Server.MapPath("/HP_PHOTO/"); //if (!Directory.Exists(file)) //{ // Directory.CreateDirectory(file); //} //string tt=weixin.SaveAsMedia("d3VvIIQU7kaxd-NPw8a5qmDPkxLLY1NlBPLmCGNA_40K9DmDN4bbBEBJywfx2JFx", // file); //List<UserInfo> list= weixin.GetUserInfos(); context.Response.ContentType = "text/plain"; context.Response.Write(weixin.AcceptMsg()); return; } } context.Response.ContentType = "text/plain"; context.Response.Write("ERROR"); }
/// <summary> /// 获取用户OpenID /// </summary> void GetUserOpenID() { if (null == Request.QueryString["state"]) { //return; } else { strSiteCode = Common.Common.NoHtml(Request.QueryString["state"].ToString()); Session["strSiteCode"] = strSiteCode; } string code = Request.QueryString["code"] as string; if (!string.IsNullOrEmpty(code)) { WXConfigDAL dal = new WXConfigDAL(); Model.WeiXin.WXConfig wxConfig = dal.GetWXConfigBySiteCode(strSiteCode); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig); Oauth2AccessToken oauth2AccessToken = weixin.GetOauth2AccessToken(code); if (oauth2AccessToken != null) { strOpenID = oauth2AccessToken.OpenID; } UserInfo userinfo = weixin.GetUserInfo(strOpenID); if (userinfo != null) { regNickName = userinfo.NickName; regSex = userinfo.Sex; regHeadImg = userinfo.Headimgurl; } } else { strOpenID = code; } } if (strOpenID == null || strOpenID == "") { if (Session["customerID"] != null && Session["customerID"].ToString() != "") { MSCustomersDAL CustomerDal = new MSCustomersDAL(); try { strOpenID = CustomerDal.GetCustomerValueByID("OpenID", Session["customerID"].ToString()).ToString(); } catch (Exception) { } } } if (strOpenID != null && strOpenID != "") { Session["OpenID"] = strOpenID; } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (null == Request["state"] || Request["state"] == "") { return; } else { strSiteCode = Common.Common.NoHtml(Request.QueryString["state"].ToString()); Session["strSiteCode"] = strSiteCode; } string code = Request.QueryString["code"] as string; if (!string.IsNullOrEmpty(code)) { WXConfigDAL dal = new WXConfigDAL(); Model.WeiXin.WXConfig wxConfig = dal.GetWXConfigBySiteCode(strSiteCode); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig); Oauth2AccessToken oauth2AccessToken = weixin.GetOauth2AccessToken(code); if (oauth2AccessToken != null) { strOpenID = oauth2AccessToken.OpenID; } } } //if (string.IsNullOrEmpty(strOpenID)) //{ // return; //} if (Request["action"] != null && Request["action"] != "") { if (Request["openid"] != null && Request["openid"] != "") { strOpenID = Request.QueryString["openid"].ToString(); Session["openid"] = strOpenID; } } else { if (null != Session["openid"]) { strOpenID = Session["openid"].ToString(); } } if (Request["openid"] != null && Request["openid"] != "") { strOpenID = Request.QueryString["openid"].ToString(); Session["openid"] = strOpenID; } //取有效竞猜ID DataSet dsQuiz = dalQuiz.GetJCQuizDataList(" DATEDIFF(MI,GETDATE(),StartTime) > 15 "); if (Request["id"] != null && Request["id"] != "") { strQuizID = Common.Common.NoHtml(Request["id"].ToString()); } else if (null != dsQuiz && dsQuiz.Tables.Count > 0 && dsQuiz.Tables[0].Rows.Count > 0) { strQuizID = dalQuiz.GetJCQuizDataList(" DATEDIFF(MI,GETDATE(),StartTime) > 15 ").Tables[0].Rows[0]["ID"].ToString(); } else { #region 消费完成发生消息 string strAppID = string.Empty; string strSecret = string.Empty; Model.WeiXin.WXConfig wc = new Model.WeiXin.WXConfig(); WXConfigDAL wcdal = new WXConfigDAL(); wc = wcdal.GetWXConfigBySiteCode(strSiteCode); if (null != wc) { strAppID = wc.WXAppID; strSecret = wc.WXAppSecret; } string strToken = WeiXinHelper.GetAccessToken(strAppID, strSecret); var KeyToken = new { access_token = "" }; var b = JsonConvert.DeserializeAnonymousType(strToken, KeyToken); string strRToken = b.access_token; WeiXinHelper.SendCustomTextMessage(strRToken, strOpenID, "当前没有竞猜场次!<a href='http://114.215.108.27/MicroSite/MyQuiz.aspx?sitecode=" + strSiteCode + "&openid=" + strOpenID + "'>查看所有参加的竞猜</a>"); #endregion Response.Write("<script>document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { WeixinJSBridge.call('closeWindow');});</script>"); return; } if (Request["action"] != null && Request["action"] != "") { strAction = Common.Common.NoHtml(Request.QueryString["action"].ToString()); } if (strAction.Trim() != null && strAction.Trim() != "" && strAction.Trim().ToLower() == "save") { SaveJCGScore(); } else { GetDetailList(); } } }
/// <summary> /// 创建微信菜单 /// </summary> /// <param name="siteCode"></param> public static void CreateWeiXinMenu(string wxConfigID) { if (!string.IsNullOrEmpty(wxConfigID)) { WXConfigDAL dal = new WXConfigDAL(); WXConfig wxConfig = dal.GetWXConfigByID(wxConfigID); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig); MenuDAL dal2 = CreateInstance(); List <Menu> menus = dal2.GetWeiXinMenu(wxConfig.ID); MenuInfo menuInfo = new MenuInfo(); var buttons = from x in menus where x.MenuType.ToLower() == "button" orderby x.OrderNum ascending select x; foreach (Menu button in buttons) { if (!string.IsNullOrEmpty(button.ButtonType)) { switch (button.ButtonType.ToLower()) { case "view": string url = button.AccessLink; if (!string.IsNullOrEmpty(button.RedirectScope)) { url = WeiXinHelper.GenerateAuthorizeUrl(weixinConfig.AppId, HttpUtility.UrlEncode(url), button.RedirectScope, button.RedirectState); } menuInfo.Buttons.Add(new MenuViewBtton() { Name = button.ButtonName, Type = button.ButtonType, Url = url }); break; case "click": menuInfo.Buttons.Add(new MenuClickButton() { Name = button.ButtonName, Type = button.ButtonType, Key = button.ButtonKey }); break; default: menuInfo.Buttons.Add(new MenuEventButton() { Name = button.ButtonName, Type = button.ButtonType, Key = button.ButtonKey }); break; } } else { MenuSubButton subButton = new MenuSubButton() { Name = button.ButtonName }; var subButtons = from x in menus where x.MenuType.ToLower() == "sub_button" && x.ParentMenuID == button.ID orderby x.OrderNum ascending select x; foreach (Menu subButton1 in subButtons) { switch (subButton1.ButtonType.ToLower()) { case "view": string url = subButton1.AccessLink; if (!string.IsNullOrEmpty(subButton1.RedirectScope)) { url = WeiXinHelper.GenerateAuthorizeUrl(weixinConfig.AppId, HttpUtility.UrlEncode(url), subButton1.RedirectScope, subButton1.RedirectState); } subButton.SubButtons.Add(new MenuViewBtton() { Name = subButton1.ButtonName, Type = subButton1.ButtonType, Url = url }); break; case "click": subButton.SubButtons.Add(new MenuClickButton() { Name = subButton1.ButtonName, Type = subButton1.ButtonType, Key = subButton1.ButtonKey }); break; default: subButton.SubButtons.Add(new MenuEventButton() { Name = subButton1.ButtonName, Type = subButton1.ButtonType, Key = subButton1.ButtonKey }); break; } } menuInfo.Buttons.Add(subButton); } } string strJosn = menuInfo.ToJson(); weixin.CreateCustomMenu(menuInfo); } } }
protected void btnSave_Click(object sender, EventArgs e) { if (null == Session["strSiteName"] || null == Session["strSiteCode"] || null == Session["strLoginName"]) { Response.Write("<script language=JavaScript>;parent.location.href='../Index.aspx';</script>"); Response.End(); } if (!string.IsNullOrEmpty(keyword.Text.Trim()) && fudVoice.HasFile && fudVoice.PostedFile.ContentLength <= 1024 * 1024 * 2 && (fudVoice.PostedFile.FileName.ToLower().Contains(".mp3") || fudVoice.PostedFile.FileName.ToLower().Contains(".amr"))) {//语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式 try { //先上传并插入微信媒体表 string fileDir = string.Format("/Uploads/{0}/", DateTime.Now.ToString("yyyyMM")); if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(fileDir))) { System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(fileDir)); } string ext = System.IO.Path.GetExtension(fudVoice.FileName); string fileName = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), ext); string filePath = fileDir + fileName; fudVoice.SaveAs(HttpContext.Current.Server.MapPath(filePath)); Media media = new Media() { MediaName = fudVoice.FileName, MediaFile = filePath, MediaType = MediaUploadType.Voice.ToString().ToLower() }; //选中自动同步时,将语音文件自动同步至微信服务器 if (cboIsSyn.Checked) { WXConfigDAL cdal = new WXConfigDAL(); Model.WeiXin.WXConfig wxConfig = cdal.GetWXConfigBySiteCode(Session["strSiteCode"].ToString()); if (wxConfig != null) { WeiXinCore.Models.WeiXinConfig weixinConfig = new WeiXinCore.Models.WeiXinConfig() { ID = wxConfig.WXID, Name = wxConfig.WXName, Token = wxConfig.WXToken, AppId = wxConfig.WXAppID, AppSecret = wxConfig.WXAppSecret }; WeiXinCore.WeiXin weixin = new WeiXinCore.WeiXin(weixinConfig); MediaUploadResult obj = weixin.PostMedia(HttpContext.Current.Server.MapPath(filePath), MediaUploadType.Voice); media.MediaID = obj.MediaID; media.LastSynTime = DateTime.Now; } } MediaDAL.CreateInstance().InsertInfo(media); //插入自动回复匹配表 WXConfigDAL configdal = new WXConfigDAL(); DataSet wxconfigds = configdal.GetWXConfigDataList(Session["strSiteCode"].ToString()); MsgAutoRule model = new MsgAutoRule(); MsgAutoRuleDAL dal = new MsgAutoRuleDAL(); model.MatchPattern = keyword.Text; model.Order = Convert.ToInt32(sort.Text); model.MsgValue = media.ID;//对应微信媒体文件ID,因微信上传文件有效期为两天,所以需要调度程序定期自动上传 model.ID = Guid.NewGuid().ToString("N").ToUpper(); model.Enabled = 1; model.LastModTime = DateTime.Now; model.MatchType = "keywords"; model.MsgType = "voice"; if (wxconfigds != null && wxconfigds.Tables.Count > 0 && wxconfigds.Tables[0].Rows.Count > 0) { model.WXConfigID = wxconfigds.Tables[0].Rows[0]["ID"].ToString(); } if (dal.AddMsgAutoRule(model)) { MessageBox.Show(this, "操作成功!"); } else { MessageBox.Show(this, "操作失败!"); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); } } else { MessageBox.Show(this, "请核对信息后再操作!"); } }