1.首先配置URL(即域名下得某一个页面地址,用来获取验证Token和功能操作),验证Token秘钥等!见下图:
在指定页面后台做操作:
1.需要验证的Token
2.获取客户操作事件(扫码关注,订阅关注等)
3.实现菜单栏,回复消息等功能
protected void Page_Load(object sender, EventArgs e)
{
//成为开发者url测试时需注释
CustomMenu();//生成菜单栏
string postStr = "";
if (Request.HttpMethod.ToLower() == "post")
{
postStr = this.PostStr();//把post数据流转换成字符串。
if (!string.IsNullOrEmpty(postStr))
{
DealPost(postStr);//调用需要操作的事件
}
}
else if (this.CheckSignature())
{
string echostr = Request["echostr"];
Response.Write(echostr);
Response.End();
}
}
//--------------------------------------------验证token----------------------------------------------//
private bool CheckSignature()
{
string token = "zhiboshi";
string signature = Request["signature"];
string timestamp = Request["timestamp"];
string nonce = Request["nonce"];
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(signature) || string.IsNullOrEmpty(timestamp) || string.IsNullOrEmpty(nonce))
{
return false;
}
else
{
ArrayList arr = new ArrayList() { token, timestamp, nonce };
arr.Sort();
string signature1 = GetSHA1(arr[0].ToString() + arr[1].ToString() + arr[2].ToString()).ToLower();
if (signature == signature1)
{
return true;
}
else
{
return false;
}
}
}
//--------------------------------------------加密解密字符串----------------------------------------------//
//把post数据解码成字符串
private string PostStr()
{
Stream s = Request.InputStream;//读取post来的数据流
byte[] b = new byte[s.Length];//定义一个和post来的数据流长度一致的空数组
s.Read(b, 0, (int)s.Length);//把这个数据流存入数组
return System.Text.Encoding.UTF8.GetString(b);//把数组转换成字符串
}
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
private string GetSHA1(string password)
{
string shh1string = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
return shh1string;
}
//---------------------------------------------------------消息处理----------------------------------------------------//
/// <summary>
/// 处理消息
/// </summary>
/// <param name="postStr"></param>
private void DealPost(string postStr)
{
CommXml.DealXml dx = new CommXml.DealXml(postStr);
if (dx.MsgType.Equals("text"))
{
//处理文本消息
CommXml.TextType tt = dx.NewTextType(dx.Xn);
string msg = tt.Content.ToUpper();
string contentMsg = "";
//判断微信中输入字符是否存在
if (msg == "喊单")
{
contentMsg = "立即<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5798931f96&redirect_uri=http://weixin.666100.cc/dy_index.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">【订阅讲师】</a>";
}
else if (msg == "交易")
{
contentMsg = "立即<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5798931f96&redirect_uri=http://weixin.666100.cc/dy_index.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">【订阅讲师】</a>";
}
else if (msg == "下单")
{
contentMsg = "立即<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5798951196&redirect_uri=http://weixin.666100.cc/dy_index.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">【订阅讲师】</a>";
}
else if (msg == "成交")
{
contentMsg = "立即<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5791932f96&redirect_uri=http://weixin.666100.cc/dy_index.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">【订阅讲师】</a>";
}
else if (msg == "交易提醒")
{
contentMsg = "立即<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5799931f16&redirect_uri=http://weixin.666100.cc/dy_index.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">【订阅讲师】</a>";
}
else if (msg == "直播")
{
contentMsg = "请选择您要进入的直播室:"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c1199931f16&redirect_uri=http://weixin.666100.cc/zb1_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">1.时代资本</a>"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7215798931f96&redirect_uri=http://weixin.666100.cc/zb2_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">2.点石成金</a>";
}
else if (msg == "视频")
{
contentMsg = "请选择您要进入的直播室:"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5798931f96&redirect_uri=http://weixin.666100.cc/zb1_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">1.时代资本</a>"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5799931f16&redirect_uri=http://weixin.666100.cc/zb2_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">2.点石成金</a>";
}
else if (msg == "视频直播")
{
contentMsg = "请选择您要进入的直播室:"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c57923931f96&redirect_uri=http://weixin.666100.cc/zb1_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">1.时代资本</a>"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c57538931f96&redirect_uri=http://weixin.666100.cc/zb2_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">2.点石成金</a>";
}
else if (msg == "时代资本")
{
contentMsg = "请选择您要进入的直播室:"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5799933316&redirect_uri=http://weixin.666100.cc/zb1_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">1.时代资本</a>"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5799931f16&redirect_uri=http://weixin.666100.cc/zb2_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">2.点石成金</a>";
}
else if (msg == "点石成金")
{
contentMsg = "请选择您要进入的直播室:"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5798331f96&redirect_uri=http://weixin.666100.cc/zb1_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">1.时代资本</a>"
+ "<a href=\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5722931f96&redirect_uri=http://weixin.666100.cc/zb2_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\">2.点石成金</a>";
}
else
{
contentMsg = "非常抱歉,没能找到您想要的东西,搜搜别的试试呗!";
}
Response.Write(ResponseWeixin.ResponseText(contentMsg, tt.FromUserName, tt.ToUserName));
}
else if (dx.MsgType.Equals("event") && dx.Xn != null) //以下部分是客户通过哪种方式促发的事件,不同方式促发可以进行不同的操作
{
CommXml.EventType et = dx.NewEnentType(dx.Xn);
Kz_LiveRoom.Service1Client c = new Service1Client();
if (et.Event.Equals("subscribe"))
{
if (et.EventKey == null || et.EventKey == "")
{
//订阅事件
string contentMsg = "您好,热烈欢迎您成为直播室的一员。";
Response.Write(ResponseWeixin.ResponseText(contentMsg, et.FromUserName, et.ToUserName));
}
else
{
//未关注 扫描二维码 时促发事件
string contentMsg = "您已经成功绑定了直播室微信!\n" +
"绑定状态:电脑扫码绑定成功 \n" +
"绑定时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm") + "";
Response.Write(ResponseWeixin.ResponseText(contentMsg, et.FromUserName, et.ToUserName));
}
}
else if (et.Event.Equals("SCAN"))
{
//已关注 扫描二维码 时促发事件
string contentMsg = "您已经成功绑定了金融家微信!\n" +
"绑定状态:电脑扫码绑定成功 \n" +
"绑定时间:" + DateTime.Now.ToString("yyyy年MM月dd日 HH:mm") + "";
Response.Write(ResponseWeixin.ResponseText(contentMsg, et.FromUserName, et.ToUserName));
}
else if (et.Event.Equals("unsubscribe"))
{
//取消订阅
}
}
}
/// <summary>
/// 获取xml配置内容.
/// </summary>
/// <param name="type"></param>
/// <param name="content"></param>
/// <returns></returns>
private string GetXmlContent(string type, string content)
{
//初始化一个xml实例
XmlDocument xml = new XmlDocument();
//加载文件
xml.Load(AppDomain.CurrentDomain.BaseDirectory + "weixin\\LotteryConfig.xml");
//指定根节点
// XmlNode root = xml.SelectSingleNode("/data");
//获取同名同级节点集合
XmlNodeList nodelist = xml.SelectNodes("/data/item");
foreach (XmlNode node in nodelist)
{
if (node.Attributes["id"].Value == type)
{
return node.SelectSingleNode(content).InnerText;
}
}
return "";
}
//------------------------------------------------------------生成菜单栏方法--------------------------------------------------//
/// <summary>
/// 微信企业号开发菜单栏,并实现在访问权限设置
/// </summary>
/// <param name="Token"></param>
/// <returns></returns>
public static string CustomMenu()
{
string AppUrl = "https://api.weixin.qq.com/cgi-bin/token?";
string grant_type = "client_credential";
string appid = "wx993f335798931f96";
string secret = "261beef332baae21c23ad9274356b9f04";
WebClient webClient = new WebClient();
Byte[] bytes = webClient.DownloadData(string.Format("{0}grant_type={1}&appid={2}&secret={3}", AppUrl, grant_type, appid, secret));
string result = Encoding.GetEncoding("utf-8").GetString(bytes);
JObject jObj = JObject.Parse(result);
string token = jObj["access_token"].ToString();
string expires_in = jObj["expires_in"].ToString();
string CustomMenuEdit = "";
CustomMenuEdit += "{\n";
CustomMenuEdit += "\"button\":[\n";
CustomMenuEdit += "{\n";
CustomMenuEdit += "\"type\":\"click\",\n";
CustomMenuEdit += "\"name\":\"视频直播\",\n";
CustomMenuEdit += "\"sub_button\":[\n";
CustomMenuEdit += "{\n";
CustomMenuEdit += "\"type\":\"view\",\n";
CustomMenuEdit += "\"name\":\"时代资本\",\n";
CustomMenuEdit += "\"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c3398931f96&redirect_uri=http://weixin.666100.cc/zb1_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\"\n";
CustomMenuEdit += "},\n";
CustomMenuEdit += "{\n";
CustomMenuEdit += "\"type\":\"view\",\n";
CustomMenuEdit += "\"name\":\"点石成金\",\n";
CustomMenuEdit += "\"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5793331f96&redirect_uri=http://weixin.666100.cc/zb2_links.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\"\n";
CustomMenuEdit += "}]\n";
CustomMenuEdit += "},\n";
CustomMenuEdit += "{\n";
CustomMenuEdit += "\"type\":\"view\",\n";
CustomMenuEdit += "\"name\":\"订阅讲师\",\n";
CustomMenuEdit += "\"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f7c5793331f96&redirect_uri=http://weixin.666100.cc/dy_index.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\"\n";
CustomMenuEdit += "},\n";
CustomMenuEdit += "{\n";
CustomMenuEdit += "\"type\":\"view\",\n";
CustomMenuEdit += "\"name\":\"个人中心\",\n";
CustomMenuEdit += "\"url\":\"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx993f335798931f96&redirect_uri=http://weixin.666100.cc/Gr_center.aspx/oauth2/&response_type=code&scope=snsapi_base&state=1#wechat_redirect\"\n";
CustomMenuEdit += "},\n";
CustomMenuEdit += "]\n";
CustomMenuEdit += "}\n";
string url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + token;
WebRequest req = WebRequest.Create(url);
JavaScriptSerializer aa = new JavaScriptSerializer();
byte[] requestBytes = Encoding.UTF8.GetBytes(CustomMenuEdit);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = requestBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(requestBytes, 0, requestBytes.Length);
requestStream.Close();
HttpWebResponse res = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default);
string backstr = sr.ReadToEnd();
sr.Close();
res.Close();
return null;
}
//-----------------------------------------------------------------以上实现所需类---------------------------------------------------------//
public class CommXml
{
/// <summary>
///微信xml消息的类封装
/// </summary>
public class TextType
{
private string ToUserName1;
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName
{
get { return ToUserName1; }
set { ToUserName1 = value; }
}
private string FromUserName1;
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName
{
get { return FromUserName1; }
set { FromUserName1 = value; }
}
private string CreateTime1;
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public string CreateTime
{
get { return CreateTime1; }
set { CreateTime1 = value; }
}
private string MsgType1 = "text";
/// <summary>
///消息类型:text
/// </summary>
public string MsgType
{
get { return MsgType1; }
set { MsgType1 = value; }
}
private string Content1;
/// <summary>
/// 文本消息内容
/// </summary>
public string Content
{
get { return Content1; }
set { Content1 = value; }
}
private string MsgId1;
/// <summary>
/// 消息id,64位整型
/// </summary>
public string MsgId
{
get { return MsgId1; }
set { MsgId1 = value; }
}
}
public class PicType
{
private string ToUserName1;
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName
{
get { return ToUserName1; }
set { ToUserName1 = value; }
}
private string FromUserName1;
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName
{
get { return FromUserName1; }
set { FromUserName1 = value; }
}
private string CreateTime1;
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public string CreateTime
{
get { return CreateTime1; }
set { CreateTime1 = value; }
}
private string MsgType1 = "image";
/// <summary>
/// 消息类型:image
/// </summary>
public string MsgType
{
get { return MsgType1; }
set { MsgType1 = value; }
}
private string PicUrl1;
/// <summary>
/// 图片链接
/// </summary>
public string PicUrl
{
get { return PicUrl1; }
set { PicUrl1 = value; }
}
private string MsgId1;
/// <summary>
/// 消息id,64位整型
/// </summary>
public string MsgId
{
get { return MsgId1; }
set { MsgId1 = value; }
}
}
public class LocationType
{
private string ToUserName1;
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName
{
get { return ToUserName1; }
set { ToUserName1 = value; }
}
private string FromUserName1;
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName
{
get { return FromUserName1; }
set { FromUserName1 = value; }
}
private string CreateTime1;
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public string CreateTime
{
get { return CreateTime1; }
set { CreateTime1 = value; }
}
private string MsgType1;
/// <summary>
/// location
/// </summary>
public string MsgType
{
get { return MsgType1; }
set { MsgType1 = value; }
}
private string Location_X1;
/// <summary>
/// 地理位置纬度
/// </summary>
public string Location_X
{
get { return Location_X1; }
set { Location_X1 = value; }
}
private string Location_Y1;
/// <summary>
/// 地理位置经度
/// </summary>
public string Location_Y
{
get { return Location_Y1; }
set { Location_Y1 = value; }
}
private string Scale1;
/// <summary>
/// 地图缩放大小
/// </summary>
public string Scale
{
get { return Scale1; }
set { Scale1 = value; }
}
private string Label1;
/// <summary>
/// 地理位置信息
/// </summary>
public string Label
{
get { return Label1; }
set { Label1 = value; }
}
private string MsgId1;
/// <summary>
/// 消息id,64位整型
/// </summary>
public string MsgId
{
get { return MsgId1; }
set { MsgId1 = value; }
}
}
public class LinkType
{
private string ToUserName1;
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName
{
get { return ToUserName1; }
set { ToUserName1 = value; }
}
private string FromUserName1;
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName
{
get { return FromUserName1; }
set { FromUserName1 = value; }
}
private string CreateTime1;
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public string CreateTime
{
get { return CreateTime1; }
set { CreateTime1 = value; }
}
private string MsgType1 = "text";
/// <summary>
/// link
/// </summary>
public string MsgType
{
get { return MsgType1; }
set { MsgType = value; }
}
private string Title1;
/// <summary>
/// 消息标题
/// </summary>
public string Title
{
get { return Title1; }
set { Title1 = value; }
}
private string Description1;
/// <summary>
/// 消息描述
/// </summary>
public string Description
{
get { return Description1; }
set { Description1 = value; }
}
private string Url1;
/// <summary>
/// 消息链接
/// </summary>
public string Url
{
get { return Url1; }
set { Url1 = value; }
}
private string MsgId1;
/// <summary>
/// 消息id,64位整型
/// </summary>
public string MsgId
{
get { return MsgId1; }
set { MsgId1 = value; }
}
}
public class EventType
{
private string Event1;
/// <summary>
/// 事件类型,subscribe(订阅)、unsubscribe(取消订阅)、CLICK(自定义菜单点击事件)
/// </summary>
public string Event
{
get { return Event1; }
set { Event1 = value; }
}
private string ToUserName1;
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName
{
get { return ToUserName1; }
set { ToUserName1 = value; }
}
private string FromUserName1;
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName
{
get { return FromUserName1; }
set { FromUserName1 = value; }
}
private string EventKey1;
public string EventKey
{
get { return EventKey1; }
set { EventKey1 = value; }
}
}
public class DealXml
{
private string MsgType1;
/// <summary>
/// 消息类型
/// </summary>
public string MsgType
{
get { return MsgType1; }
set { MsgType1 = value; }
}
private XmlNode xn;
public XmlNode Xn
{
get { return xn; }
set { xn = value; }
}
public DealXml(string weixinXML)
{
System.Xml.XmlDocument doc = new XmlDocument();
doc.LoadXml(weixinXML);
XmlNodeList list = doc.GetElementsByTagName("xml");
xn = list[0];
MsgType = xn.SelectSingleNode("//MsgType").InnerText;
}
public TextType NewTextType(XmlNode xn)
{
TextType tt = new TextType();
tt.ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
tt.FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
tt.CreateTime = xn.SelectSingleNode("//CreateTime").InnerText;
tt.MsgType = xn.SelectSingleNode("//MsgType").InnerText;
tt.Content = xn.SelectSingleNode("//Content").InnerText;
tt.MsgId = xn.SelectSingleNode("//MsgId").InnerText;
return tt;
}
public PicType NewPicType(XmlNode xn)
{
PicType tt = new PicType();
tt.ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
tt.FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
tt.CreateTime = xn.SelectSingleNode("//CreateTime").InnerText;
tt.MsgType = xn.SelectSingleNode("//MsgType").InnerText;
tt.PicUrl = xn.SelectSingleNode("//PicUrl").InnerText;
tt.MsgId = xn.SelectSingleNode("//MsgId").InnerText;
return tt;
}
public LocationType NewLocationType(XmlNode xn)
{
LocationType tt = new LocationType();
tt.ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
tt.FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
tt.CreateTime = xn.SelectSingleNode("//CreateTime").InnerText;
tt.MsgType = xn.SelectSingleNode("//MsgType").InnerText;
tt.Location_X = xn.SelectSingleNode("//Location_X").InnerText;
tt.Location_Y = xn.SelectSingleNode("//Location_Y").InnerText;
tt.Scale = xn.SelectSingleNode("//Scale").InnerText;
tt.Label = xn.SelectSingleNode("//Label").InnerText;
tt.MsgId = xn.SelectSingleNode("//MsgId").InnerText;
return tt;
}
public LinkType NewLinkType(XmlNode xn)
{
LinkType tt = new LinkType();
tt.ToUserName = xn.SelectSingleNode("//ToUserName").InnerText;
tt.FromUserName = xn.SelectSingleNode("//FromUserName").InnerText;
tt.CreateTime = xn.SelectSingleNode("//CreateTime").InnerText;
tt.MsgType = xn.SelectSingleNode("//MsgType").InnerText;
tt.Title = xn.SelectSingleNode("//Title").InnerText;
tt.Description = xn.SelectSingleNode("//Description").InnerText;
tt.Url = xn.SelectSingleNode("//Url").InnerText;
tt.MsgId = xn.SelectSingleNode("//MsgId").InnerText;
return tt;
}
public EventType NewEnentType(XmlNode xn)
{
EventType tt = new EventType();
tt.ToUserName = xn.SelectSingleNode("//ToUserName") != null ? xn.SelectSingleNode("//ToUserName").InnerText : "";
tt.FromUserName = xn.SelectSingleNode("//FromUserName") != null ? xn.SelectSingleNode("//FromUserName").InnerText : "";
tt.Event = xn.SelectSingleNode("//Event") != null ? xn.SelectSingleNode("//Event").InnerText : "";
tt.EventKey = xn.SelectSingleNode("//EventKey") != null ? xn.SelectSingleNode("//EventKey").InnerText : "";
return tt;
}
}
public class news
{
private string Title1;
/// <summary>
/// 图文消息标题
/// </summary>
public string Title
{
get { return Title1; }
set { Title1 = value; }
}
private string Description1;
/// <summary>
/// 图文消息描述
/// </summary>
public string Description
{
get { return Description1; }
set { Description1 = value; }
}
private string PicUrl1;
/// <summary>
/// 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80,限制图片链接的域名需要与开发者填写的基本资料中的Url一致
/// </summary>
public string PicUrl
{
get { return PicUrl1; }
set { PicUrl1 = value; }
}
private string Url1;
/// <summary>
/// 点击图文消息跳转链接
/// </summary>
public string Url
{
get { return Url1; }
set { Url1 = value; }
}
}
}
public static class ResponseWeixin
{
/// <summary>
/// 回复文本消息
/// </summary>
/// <param name="content">回复内容</param>
/// <param name="UserOpenId">用户的openid</param>
/// <param name="DevOpenId">开发者的微信号</param>
/// <returns></returns>
public static string ResponseText(string content, string UserOpenId, string DevOpenId)
{
string Txt = "<xml>";
Txt = Txt + "<ToUserName><![CDATA[" + UserOpenId + "]]></ToUserName>";
Txt = Txt + "<FromUserName><![CDATA[" + DevOpenId + "]]></FromUserName>";
Txt = Txt + "<CreateTime>1348831860</CreateTime>";
Txt = Txt + "<MsgType><![CDATA[text]]></MsgType>";
Txt = Txt + "<Content><![CDATA[" + content + "]]></Content>";
Txt = Txt + "<MsgId>1234567890123456</MsgId>";
Txt = Txt + "</xml>";
return Txt;
}
/// <summary>
/// 回复图文消息
/// </summary>
/// <param name="_List">List类型的news消息,具体参照news类</param>
/// <param name="UserOpenId">要发送用户的openid</param>
/// <param name="DevOpenId">开发者的微信号</param>
/// <returns></returns>
public static string ResponsePic(List<JRJWeiXin.Common.CommXml.news> _List, string UserOpenId, string DevOpenId)
{
string pic = "<xml>";
pic = pic + "<ToUserName><![CDATA[" + UserOpenId + "]]></ToUserName>";
pic = pic + "<FromUserName><![CDATA[" + DevOpenId + "]]></FromUserName>";
pic = pic + "<CreateTime>1348831860</CreateTime>";
pic = pic + "<MsgType><![CDATA[news]]></MsgType>";
pic = pic + "<ArticleCount>" + _List.Count + "</ArticleCount>";
pic = pic + "<Articles>";
for (int i = 0; i < _List.Count; i++)
{
pic = pic + "<item>";
pic = pic + "<Title><![CDATA[" + _List[i].Title + "]]></Title>";
pic = pic + "<Description><![CDATA[" + _List[i].Description + "]]></Description>";
pic = pic + "<PicUrl><![CDATA[" + _List[i].PicUrl + "]]></PicUrl>";
pic = pic + "<Url><![CDATA[" + _List[i].Url + "]]></Url>";
pic = pic + "</item>";
}
pic = pic + "</Articles>";
pic = pic + "<FuncFlag>1</FuncFlag>";
pic = pic + "</xml>";
return pic;
}
/// <summary>
/// 菜单事件
/// </summary>
/// <param name="content">事件key</param>
/// <param name="UserOpenId">用户的openid</param>
/// <param name="DevOpenId">开发者的微信号</param>
/// <returns></returns>
public static string ResponseMenu(string EventKey, string UserOpenId, string DevOpenId)
{
string Menu = "<xml>";
Menu = Menu + "<ToUserName><![CDATA[" + UserOpenId + "]]></ToUserName>";
Menu = Menu + "<FromUserName><![CDATA[" + DevOpenId + "]]></FromUserName>";
Menu = Menu + "<CreateTime>1348831860</CreateTime>";
Menu = Menu + "<MsgType><![CDATA[event]]></MsgType>";
Menu = Menu + "<Event><![CDATA[CLICK]]></Event>";
Menu = Menu + "<EventKey><![CDATA[" + EventKey + "]]></EventKey>";
Menu = Menu + "</xml>";
return Menu;
}
}