440 lines
30 KiB
C#
440 lines
30 KiB
C#
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.Web.Script.Serialization;
|
||
namespace Mtxfw.shop
|
||
{
|
||
public partial class weixintoken : System.Web.UI.Page
|
||
{
|
||
/// <summary>
|
||
/// 填写你申请的登录资料
|
||
/// </summary>
|
||
|
||
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();
|
||
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
|
||
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(" top 10 ", "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 + "\",";
|
||
strjson += "\"picurl\":\"http://shop.q-b.cc" + 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() + "\",";
|
||
strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? "http://shop.q-b.cc" + 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\":\"http://shop.q-b.cc" + strPaths2 + "\",";
|
||
strjson += "\"hqmusicurl\":\"http://shop.q-b.cc" + "" + 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 + "\",";
|
||
strjson += "\"picurl\":\"http://shop.q-b.cc" + 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() + "\",";
|
||
strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? "http://shop.q-b.cc" + 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\":\"http://shop.q-b.cc" + strPaths2 + "\",";
|
||
strjson += "\"hqmusicurl\":\"http://shop.q-b.cc" + "" + 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)
|
||
{
|
||
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;
|
||
Mtxfw.Model.Article amodel = daoArt.GetModel(int.Parse(strEventKey.Replace("menu_", "").Replace("menu2_", "")));
|
||
if (amodel != null)
|
||
{
|
||
string strjson = "{";
|
||
strjson += "\"touser\":\"" + OPENID + "\",";
|
||
strjson += "\"msgtype\":\"news\",";
|
||
strjson += "\"news\":{";
|
||
strjson += "\"articles\": [";
|
||
strjson += "{";
|
||
strjson += "\"title\":\"" + amodel.Title + "\",";
|
||
//strjson += "\"description\":\"" + amodel.Title + "\",";
|
||
strjson += "\"url\":\"" + (strEventKey == "menu2_151" ? "http://shop.q-b.cc/shop_guests.aspx" : (strEventKey.IndexOf("menu2_") != -1 ? "http://shop.q-b.cc/article-" + amodel.id + ".html" : amodel.Paths)) + "\",";
|
||
strjson += "\"picurl\":\"http://shop.q-b.cc" + Mtxfw.Utility.Common.GetCoverPic(amodel.Author, "540x270", 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 ? "http://shop.q-b.cc/article-" + drv3["Id"].ToString() : drv3["Paths"].ToString()) + "\",";
|
||
strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? "http://shop.q-b.cc" + Mtxfw.Utility.Common.GetCoverPic(drv3["Author"].ToString(), "540x270", config.webImgMode) : "") + "\"";
|
||
strjson += "}";
|
||
i3 += 1;
|
||
}
|
||
}
|
||
ds3.Clear();
|
||
strjson += "]";
|
||
strjson += "}";
|
||
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/weixin.xml", strjson + strp);
|
||
}
|
||
}
|
||
}
|
||
else if (result.InnerText == "subscribe")
|
||
{
|
||
XmlNode node = httpxml.SelectSingleNode("descendant::FromUserName");
|
||
string OPENID = node.InnerText;
|
||
|
||
string strjson = "{";
|
||
DataSet ds = daoArt.GetList(" top 1 ", "ParentID=13 And wtype=0 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 wtype2 = Dr["wtype2"].ToString();
|
||
string strId = Dr["Id"].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 + "\",";
|
||
strjson += "\"picurl\":\"http://shop.q-b.cc" + "" + 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() + "\",";
|
||
strjson += "\"picurl\":\"" + (drv3["Author"].ToString() != "" ? "http://shop.q-b.cc" + 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\":\"http://shop.q-b.cc" + strPaths2 + "\",";
|
||
strjson += "\"hqmusicurl\":\"" + "http://shop.q-b.cc" + 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 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());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 验证微信签名
|
||
/// </summary>
|
||
/// * 将token、timestamp、nonce三个参数进行字典序排序
|
||
/// * 将三个参数字符串拼接成一个字符串进行sha1加密
|
||
/// * 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信。
|
||
/// <returns></returns>
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
} |