using System; using System.Data; using System.Configuration; using System.Web; using System.Net; using System.IO; using System.Security.Cryptography; using System.Xml; using System.Collections.Specialized; using System.Text.RegularExpressions; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data; using System.Data.SqlClient; using System.Web.Script.Serialization; namespace Mtxfw.shop { public partial class weixintoken : System.Web.UI.Page { /// /// 填写你申请的登录资料 /// public Mtxfw.Utility.Config config = new Mtxfw.Utility.Config(""); public Mtxfw.DAL.Article daoArt = new Mtxfw.DAL.Article(); public Mtxfw.DAL.user_info daoUser = new Mtxfw.DAL.user_info(); public int gtype = 0; protected void Page_Load(object sender, EventArgs e) { string hostname = Request.Url.Host; gtype = new DAL.host().GetId(hostname); if (!IsPostBack) { if (Request.QueryString["echoStr"] != null) { string echoStr = Request.QueryString["echoStr"]; if (CheckSignature()) { if (!string.IsNullOrEmpty(echoStr)) { Response.Write(echoStr); Response.End(); } } } if (Request.InputStream != null) { if (Request.InputStream.ToString() != "") { //接收并读取POST过来的XML文件流 try { XmlDocument httpxml = new XmlDocument(); //string MyString = "/App_Data/weixin.xml"; httpxml.Load(Request.InputStream); XmlNode result = httpxml.SelectSingleNode(@"//MsgId"); if (result != null) { XmlNode node = httpxml.SelectSingleNode("descendant::FromUserName"); string OPENID = node.InnerText; node = httpxml.SelectSingleNode("descendant::Content"); string Content = node.InnerText; Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", "OPENID=" + OPENID + "&Content" + Content); string strjson = "{"; if (Content.Trim() != "") { bool bb = false; DataSet ds = daoArt.GetList("", "ParentID=13 And wtype=2 Order by Seef desc,ID desc"); if (ds.Tables[0].Rows.Count > 0) { foreach (System.Data.DataRow Dr in ds.Tables[0].Rows) { if (bb == false) { strjson = "{"; string strId = Dr["Id"].ToString(); string wtype2 = Dr["wtype2"].ToString(); string strTitle = Dr["Title"].ToString(); string Content1 = Dr["Content"].ToString(); string Content2 = Dr["Content2"].ToString(); string strAuthor = Dr["Author"].ToString(); string strPaths = Dr["Paths"].ToString(); string strPaths2 = Dr["Paths2"].ToString(); string thumb_media_id = Dr["thumb_media_id"].ToString(); string[] KeyWords = Dr["KeyWords"].ToString().Split('|'); foreach (string strKey in KeyWords) { if (strKey.Trim() != "") { if (Content.Trim().IndexOf(strKey) != -1) { bb = true; } } } if (bb) { if (wtype2 == "0") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"text\","; strjson += "\"text\":{"; strjson += "\"content\":\"" + Content2 + "\""; strjson += "}"; strjson += "}"; } else if (wtype2 == "1") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"news\","; strjson += "\"news\":{"; strjson += "\"articles\":[{"; strjson += "\"title\":\"" + strTitle + "\","; strjson += "\"description\":\"" + Content2 + "\","; strjson += "\"url\":\"" + (strPaths.IndexOf("http") == -1 ? config.webUrl + strPaths : strPaths) + "\","; strjson += "\"picurl\":\"" + config.webUrl + Mtxfw.Utility.Common.GetCoverPic(strAuthor, "640x320", config.webImgMode) + "\""; strjson += "}"; DataSet ds3 = daoArt.GetList("", "CategoryId='" + strId + "' Order By addtime Desc"); if (ds3.Tables[0].Rows.Count > 0) { int i3 = 0; foreach (DataRow drv3 in ds3.Tables[0].Rows) { strjson += ",{"; strjson += "\"title\":\"" + drv3["Title"].ToString() + "\","; //strjson += "\"description\":\"" + drv3["Title"].ToString() + "\","; strjson += "\"url\":\"" + (drv3["Paths"].ToString().IndexOf("http") == -1 ? config.webUrl + drv3["Paths"].ToString() : drv3["Paths"].ToString()) + "\","; strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? config.webUrl + Mtxfw.Utility.Common.GetCoverPic(drv3["Author"].ToString(), "540x270", config.webImgMode) : "") + "\""; strjson += "}"; i3 += 1; } } ds3.Clear(); strjson += "]"; strjson += "}"; strjson += "}"; } else if (wtype2 == "2") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"music\","; strjson += "\"music\":{"; strjson += "\"title\":\"" + strTitle + "\","; strjson += "\"description\":\"" + Content2 + "\","; strjson += "\"musicurl\":\"" + config.webUrl + strPaths2 + "\","; strjson += "\"hqmusicurl\":\"" + config.webUrl + "" + strPaths2 + "\","; strjson += "\"thumb_media_id\":\"" + "" + thumb_media_id + "\""; strjson += "}"; strjson += "}"; } else if (wtype2 == "3") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"image\","; strjson += "\"image\":{"; strjson += "\"media_id\":\"" + thumb_media_id + "\""; strjson += "}"; strjson += "}"; } } } } } ds.Clear(); if (!bb) { ds = daoArt.GetList(" top 1 ", "ParentID=13 And wtype=1 Order by Seef desc,ID desc"); if (ds.Tables[0].Rows.Count > 0) { foreach (System.Data.DataRow Dr in ds.Tables[0].Rows) { strjson = "{"; string strId = Dr["Id"].ToString(); string wtype2 = Dr["wtype2"].ToString(); string strTitle = Dr["Title"].ToString(); string Content1 = Dr["Content"].ToString(); string Content2 = Dr["Content2"].ToString(); string strAuthor = Dr["Author"].ToString(); string strPaths = Dr["Paths"].ToString(); string strPaths2 = Dr["Paths2"].ToString(); string thumb_media_id = Dr["thumb_media_id"].ToString(); if (wtype2 == "0") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"text\","; strjson += "\"text\":{"; strjson += "\"content\":\"" + Content2 + "\""; strjson += "}"; strjson += "}"; } else if (wtype2 == "1") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"news\","; strjson += "\"news\":{"; strjson += "\"articles\":[{"; strjson += "\"title\":\"" + strTitle + "\","; strjson += "\"description\":\"" + Content2 + "\","; strjson += "\"url\":\"" + (strPaths.IndexOf("http") == -1 ? config.webUrl + strPaths : strPaths) + "\","; strjson += "\"picurl\":\"" + config.webUrl + Mtxfw.Utility.Common.GetCoverPic(strAuthor, "640x320", config.webImgMode) + "\""; strjson += "}"; DataSet ds3 = daoArt.GetList("", "CategoryId='" + strId + "' Order By addtime Desc"); if (ds3.Tables[0].Rows.Count > 0) { int i3 = 0; foreach (DataRow drv3 in ds3.Tables[0].Rows) { strjson += ",{"; strjson += "\"title\":\"" + drv3["Title"].ToString() + "\","; //strjson += "\"description\":\"" + drv3["Title"].ToString() + "\","; strjson += "\"url\":\"" + (drv3["Paths"].ToString().IndexOf("http") == -1 ? config.webUrl + drv3["Paths"].ToString() : drv3["Paths"].ToString()) + "\","; strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? config.webUrl + Mtxfw.Utility.Common.GetCoverPic(drv3["Author"].ToString(), "540x270", config.webImgMode) : "") + "\""; strjson += "}"; i3 += 1; } } ds3.Clear(); strjson += "]"; strjson += "}"; strjson += "}"; } else if (wtype2 == "2") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"music\","; strjson += "\"music\":{"; strjson += "\"title\":\"" + strTitle + "\","; strjson += "\"description\":\"" + Content2 + "\","; strjson += "\"musicurl\":\"" + config.webUrl + strPaths2 + "\","; strjson += "\"hqmusicurl\":\"" + config.webUrl + "" + strPaths2 + "\","; strjson += "\"thumb_media_id\":\"" + "" + thumb_media_id + "\""; strjson += "}"; strjson += "}"; } else if (wtype2 == "3") { strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"image\","; strjson += "\"image\":{"; strjson += "\"media_id\":\"" + thumb_media_id + "\""; strjson += "}"; strjson += "}"; } } } ds.Clear(); } } string str = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config),""); Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", str + "/n|" + strjson); } result = httpxml.SelectSingleNode(@"//Event"); if (result != null) { string strjson = ""; string strp = ""; if (result.InnerText == "CLICK") { XmlNode node = httpxml.SelectSingleNode("descendant::FromUserName"); XmlNode EventKey = httpxml.SelectSingleNode("descendant::EventKey"); if (EventKey != null && node != null) { string OPENID = node.InnerText; string strEventKey = EventKey.InnerText; if (strEventKey == "kong") { strjson = "{"; strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"text\","; strjson += "\"text\":{"; strjson += "\"content\":\"暂无内容,敬请期待!\""; strjson += "}"; strjson += "}"; } else { Mtxfw.Model.Article amodel = daoArt.GetModel(int.Parse(strEventKey.Replace("menu_", "").Replace("menu2_", ""))); if (amodel != null) { strjson = "{"; strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"news\","; strjson += "\"news\":{"; strjson += "\"articles\": ["; strjson += "{"; strjson += "\"title\":\"" + amodel.Title + "\","; //strjson += "\"description\":\"" + amodel.Title + "\","; strjson += "\"url\":\"" + ((strEventKey.IndexOf("menu_") != -1 || strEventKey.IndexOf("menu2_") != -1) ? config.webUrl + "/mobile/about2.aspx?id=" + amodel.id + "" : (amodel.Paths.IndexOf("http") == -1 ? config.webUrl + amodel.Paths : amodel.Paths)) + "\","; strjson += "\"picurl\":\"" + config.webUrl + Mtxfw.Utility.Common.GetCoverPic(amodel.Author, "720x360", config.webImgMode) + "\""; strjson += "}"; DataSet ds3 = daoArt.GetList("", "CategoryId='" + amodel.id + "' Order By addtime Desc"); if (ds3.Tables[0].Rows.Count > 0) { int i3 = 0; foreach (DataRow drv3 in ds3.Tables[0].Rows) { strjson += ",{"; strjson += "\"title\":\"" + drv3["Title"].ToString() + "\","; //strjson += "\"description\":\"" + drv3["Title"].ToString() + "\","; strjson += "\"url\":\"" + (strEventKey.IndexOf("menu2_") != -1 ? config.webUrl + "/article-" + drv3["Id"].ToString() : (drv3["Paths"].ToString().IndexOf("http") == -1 ? config.webUrl + drv3["Paths"].ToString() : drv3["Paths"].ToString())) + "\","; strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? config.webUrl + Mtxfw.Utility.Common.GetCoverPic(drv3["Author"].ToString(), "720x360", config.webImgMode) : "") + "\""; strjson += "}"; i3 += 1; } } ds3.Clear(); strjson += "]"; strjson += "}"; strjson += "}"; } } strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config),""); Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", strjson + strp); } } else if (result.InnerText == "SCAN") { XmlNode node = httpxml.SelectSingleNode("descendant::FromUserName"); string OPENID = node.InnerText; XmlNode node2 = httpxml.SelectSingleNode("descendant::EventKey"); if (node2 != null) { string strVContactID = node2.InnerText; if (strVContactID != "") { if (strVContactID.IndexOf("last_trade_no_") == -1) { int VContactID = 0; Mtxfw.Utility.Common.WriteHtml("/weixin/SCAN0.txt", OPENID + "|" + strVContactID); strVContactID = strVContactID.Replace("qrscene_", ""); if (strVContactID != "") { VContactID = Convert.ToInt32(strVContactID); login(OPENID, VContactID); } } } else { Mtxfw.Utility.Common.WriteHtml("/weixin/SCAN1.txt", "1"); } } else { Mtxfw.Utility.Common.WriteHtml("/weixin/SCAN2.txt", "2"); } } else if (result.InnerText == "subscribe") { XmlNode node = httpxml.SelectSingleNode("descendant::FromUserName"); string OPENID = node.InnerText; int VContactID = 0; XmlNode node2 = httpxml.SelectSingleNode("descendant::EventKey"); if (node2 != null) { string strVContactID = node2.InnerText; if (strVContactID != "") { if (strVContactID.IndexOf("last_trade_no_") == -1) { Mtxfw.Utility.Common.WriteHtml("/weixin/subscribe0.txt", OPENID + "|" + strVContactID); strVContactID = strVContactID.Replace("qrscene_", ""); if (strVContactID != "") { VContactID = Convert.ToInt32(strVContactID); login(OPENID, VContactID); } } } else { Mtxfw.Utility.Common.WriteHtml("/weixin/subscribe1.txt", "1"); } } else { Mtxfw.Utility.Common.WriteHtml("/weixin/subscribe2.txt", "2"); } /*DataSet ds = new Mtxfw.DAL.user_Goods().GetList("top 1", "utype='5' Order By AddTime asc"); if (ds.Tables[0].Rows.Count > 0) { foreach (System.Data.DataRow Dr in ds.Tables[0].Rows) { strjson = "{"; strjson += "\"touser\":\"" + OPENID + "\","; strjson += "\"msgtype\":\"news\","; strjson += "\"news\":{"; strjson += "\"articles\":[{"; strjson += "\"title\":\"" + Dr["GoodsName"].ToString() + "\","; strjson += "\"description\":\"\","; strjson += "\"url\":\"" + config.webUrl + "/mobile/about2.aspx?ID=152\","; strjson += "\"picurl\":\"" + config.webUrl + Mtxfw.Utility.Common.GetCoverPic(Dr["GoodsPic"].ToString(), "720x360", config.webImgMode) + "\""; strjson += "}"; strjson += "]"; strjson += "}"; strjson += "}"; } } ds.Clear(); strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config)); Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.xml", strp);*/ } } } catch (Exception err) { Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", err.ToString()); } } } } } /// /// 验证微信签名 /// /// * 将token、timestamp、nonce三个参数进行字典序排序 /// * 将三个参数字符串拼接成一个字符串进行sha1加密 /// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。 /// private bool CheckSignature() { string signature = Request.QueryString["signature"]; string timestamp = Request.QueryString["timestamp"]; string nonce = Request.QueryString["nonce"]; Mtxfw.Utility.Common.WriteHtml("/weixin/weixin.txt", timestamp + "|" + nonce); string[] ArrTmp = { config.webToken, timestamp, nonce }; Array.Sort(ArrTmp); //字典排序 string tmpStr = string.Join("", ArrTmp); tmpStr = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(tmpStr, "SHA1"); tmpStr = tmpStr.ToLower(); if (tmpStr == signature) { return true; } else { return false; } } public void login(string openid, int ContactID) { try { if (!String.IsNullOrEmpty(openid)) { Mtxfw.Model.user_info uModel = daoUser.GetModelbyopenid(openid, gtype); if (uModel == null) { int SuperiorsID = 0, uLevel0 = 0, uLevel1 = 0; int gtype = 0; string ContactIDS = "", SuperiorsIDS = ""; SqlDataReader dr = daoUser.GetUserName(gtype,ContactID); if (dr.HasRows) { if (dr.Read()) { ContactIDS = dr["ContactIDS"].ToString(); uLevel1 = Convert.ToInt32(dr["uLevel1"]); gtype = Convert.ToInt32(dr["gtype"]); } } dr.Close(); if (ContactID > 0) { /*int tjcount = new Mtxfw.DAL.user_info().GetCount("SuperiorsID=" + ContactID); if (tjcount >= 3) { string strzd = new Mtxfw.DAL.user_info().Getzd("id", "SuperiorsIDS Like '%," + ContactID + ",%' Order By RegTime Asc").ToString(); if (strzd != "") { SuperiorsID = Convert.ToInt32(strzd); Mtxfw.Model.user_info cumodel = new Mtxfw.DAL.user_info().GetModel(SuperiorsID); if (cumodel != null) { SuperiorsID = cumodel.Id; uLevel0 = cumodel.uLevel0; SuperiorsIDS = cumodel.SuperiorsIDS; } } } else { Mtxfw.Model.user_info cumodel = new Mtxfw.DAL.user_info().GetModel(ContactID); if (cumodel != null) { SuperiorsID = cumodel.Id; uLevel0 = cumodel.uLevel0; SuperiorsIDS = cumodel.SuperiorsIDS; } }*/ string strMemberCard = (daoUser.GetMaxMemberCard(gtype) + 1).ToString(); Mtxfw.Model.user_info model = new Mtxfw.Model.user_info(); model.MemberCard = "zh" + strMemberCard; string strUserName = Mtxfw.Utility.Common.RandNum(6).ToString().ToLower(); if (daoUser.IsExists(strUserName, gtype)) { strUserName = "zh" + strMemberCard; } model.UserName = strUserName; model.Password = Mtxfw.Utility.Security.EncryptString("111111"); model.Password2 = Mtxfw.Utility.Security.EncryptString("111111"); model.RealName = ""; model.NCName = ""; model.UserPic = ""; model.Sex = ""; model.SFZ = ""; model.Mobile = ""; model.Phone = ""; model.Email = ""; model.Province = ""; model.City = ""; model.County = ""; model.MailingAddress = ""; model.PostalCode = ""; model.QQ = ""; model.CompanyName = ""; model.CompanyNumber = ""; model.ProductName = ""; model.UserState = "正常"; model.RegTime = DateTime.Now; model.RegTime2 = DateTime.Now; model.RememberID = 0; model.RememberIDS = ""; model.SuperiorsID = SuperiorsID; if (SuperiorsIDS == "") { model.SuperiorsIDS = "," + SuperiorsID.ToString() + ","; } else { model.SuperiorsIDS = SuperiorsIDS + SuperiorsID.ToString() + ","; } model.ContactID = ContactID; if (ContactIDS == "") { model.ContactIDS = "," + ContactID.ToString() + ","; } else { model.ContactIDS = ContactIDS + ContactID.ToString() + ","; } model.DLRememberID = 0; model.DLRememberIDS = ""; model.SJRememberID = 0; model.SJRememberIDS = ""; model.uLevel0 = uLevel0 + 1; model.uLevel1 = uLevel1 + 1; model.uLevel3 = 0; int uLevel5 = 0; model.uLevel5 = uLevel5; model.utype = "0"; model.uutype = "0"; model.openid = openid; model.opentype = 4; model.opentype2 = 4; model.refresh_token = ""; model.refresh_token_time = DateTime.Now.ToString(); model.IFUpUserName = 1; model.gtype = gtype; int id = daoUser.Add(model); string strjson = "{"; strjson += "\"touser\":\"" + openid + "\","; strjson += "\"msgtype\":\"text\","; strjson += "\"text\":{\"content\":\"您尚未注册,请点击此处注册!\"}"; strjson += "}"; string strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config),""); Mtxfw.Utility.Common.WriteHtml("/weixin/touser.txt", strp); } else { if (ContactID > 68) { string strjson = "{"; strjson += "\"touser\":\"" + openid + "\","; strjson += "\"msgtype\":\"text\","; strjson += "\"text\":{\"content\":\"您没有推荐人,请联系管理员再重新关注!\"}"; strjson += "}"; string strp = Mtxfw.Utility.Common.GetResponseData(strjson, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + Mtxfw.Utility.Common.getaccess_token(HttpContext.Current, config.webappKey, config.webappSecret, config),""); Mtxfw.Utility.Common.WriteHtml("/weixin/nouser.txt", strp); } } } else { if (uModel.ContactID == 0 && uModel.Id > 68) { int SuperiorsID = 0, uLevel0 = 0, uLevel1 = 0; int gtype = 0; string ContactIDS = "", SuperiorsIDS = ""; SqlDataReader dr = daoUser.GetUserName(gtype,ContactID); if (dr.HasRows) { if (dr.Read()) { ContactIDS = dr["ContactIDS"].ToString(); uLevel1 = Convert.ToInt32(dr["uLevel1"]); gtype = Convert.ToInt32(dr["gtype"]); } } dr.Close(); if (ContactID > 0) { /*int tjcount = new Mtxfw.DAL.user_info().GetCount("SuperiorsID=" + ContactID); if (tjcount >= 3) { string strzd = new Mtxfw.DAL.user_info().Getzd("id", "SuperiorsIDS Like '%," + ContactID + ",%' Order By RegTime Asc").ToString(); if (strzd != "") { SuperiorsID = Convert.ToInt32(strzd); Mtxfw.Model.user_info cumodel = new Mtxfw.DAL.user_info().GetModel(SuperiorsID); if (cumodel != null) { SuperiorsID = cumodel.Id; uLevel0 = cumodel.uLevel0; SuperiorsIDS = cumodel.SuperiorsIDS; } } } else { Mtxfw.Model.user_info cumodel = new Mtxfw.DAL.user_info().GetModel(ContactID); if (cumodel != null) { SuperiorsID = cumodel.Id; uLevel0 = cumodel.uLevel0; SuperiorsIDS = cumodel.SuperiorsIDS; } } if (SuperiorsIDS == "") { SuperiorsIDS = "," + SuperiorsID.ToString() + ","; } else { SuperiorsIDS = SuperiorsIDS + SuperiorsID.ToString() + ","; }*/ if (ContactIDS == "") { ContactIDS = "," + ContactID.ToString() + ","; } else { ContactIDS = ContactIDS + ContactID.ToString() + ","; } new Mtxfw.DAL.user_info().UpdateJDRAndTJR(uLevel0 + 1, SuperiorsID, SuperiorsIDS, uLevel1 + 1, ContactID, ContactIDS, uModel.Id); } } } } } catch (Exception err) { Mtxfw.Utility.Common.WriteHtml("/weixin/adderr.txt", err.ToString()); } } } }