.NET Core3.1 响应微信公众号发来的请求并返回数据
当有人在微信公众号发送消息或点击菜单时,微信公众号会向你的服务器发起一次请求,你可以做出回应
[HttpPost]
public IActionResult Check()
{
var buffer = new byte[Convert.ToInt32(Request.ContentLength)];
Request.Body.ReadAsync(buffer, 0, buffer.Length);
string body = Encoding.UTF8.GetString(buffer);
XmlDocument doc = new XmlDocument();
doc.LoadXml(body);//读取xml字符串
XmlElement root = doc.DocumentElement;
Message m = new Message()
{
FromUserName = root.SelectSingleNode("FromUserName").InnerText,
ToUserName = root.SelectSingleNode("ToUserName").InnerText,
CreateTime = root.SelectSingleNode("CreateTime").InnerText,
MsgType = root.SelectSingleNode("MsgType").InnerText,
};
DateTime a = new DateTime(1970, 1, 1, 8, 0, 0);
//有人向公众号内发消息
if (m.MsgType.Trim().ToLower() == "text")
{
m.Content = root.SelectSingleNode("Content").InnerText;
List<string> resContent = new List<string>();
foreach (var item in WeChatMessageService.GetAll())
{
if (m.Content.Contains(item.Key))
{
resContent.Add(item.Value);
}
}
if (resContent.Count == 0)
{
resContent.Add(conf["DefaultMessage"]);
}
string response = $"<xml><ToUserName><![CDATA[{m.FromUserName}]]></ToUserName><FromUserName><![CDATA[{m.ToUserName}]]></FromUserName><CreateTime>{(DateTime.Now - a).TotalSeconds}</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{string.Join("\n", resContent)}]]></Content></xml>";
return Content(response);
}
//有人点击了菜单,如果该菜单类型为click,向他回复一条图文消息
else if (m.MsgType.Trim().ToLower() == "event")
{
m.Event = root.SelectSingleNode("Event").InnerText;
if(m.Event.Trim().ToLower()=="click")
{
m.EventKey = Convert.ToInt32(root.SelectSingleNode("EventKey").InnerText);
WeChatMenu wcm = WeChatMenuService.FindFirst(a=>a.id==m.EventKey).Result;
//获取这个人在系统中的注册数据
Member me = MemberService.FindFirst(a => a.OpenId == m.FromUserName).Result;
//using (StreamWriter sw = new StreamWriter("C:/cs.txt", true))
//{
// sw.WriteLine("有一个事件请求:" + JsonConvert.SerializeObject(me));
//}
//判断是否注册
if (me==null)
return Content($"<xml><ToUserName><![CDATA[{m.FromUserName}]]></ToUserName><FromUserName><![CDATA[{m.ToUserName}]]></FromUserName><CreateTime>{(DateTime.Now - a).TotalSeconds}</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[错误提示]]></Title><Description><![CDATA[您还没有注册]]></Description><PicUrl><![CDATA[{conf["DefaultErrorImage"]}]]></PicUrl><Url><![CDATA[{conf["DefaultUrl"]+ "/WechatMessage/ShowMessage" + "?Id=-1"}]]></Url></item></Articles></xml>");
//判断是否审核通过
if(me.State!=1)
return Content($"<xml><ToUserName><![CDATA[{m.FromUserName}]]></ToUserName><FromUserName><![CDATA[{m.ToUserName}]]></FromUserName><CreateTime>{(DateTime.Now - a).TotalSeconds}</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[错误提示]]></Title><Description><![CDATA[抱歉!您的账号还未通过审核]]></Description><PicUrl><![CDATA[{conf["DefaultErrorImage"]}]]></PicUrl><Url><![CDATA[{conf["DefaultUrl"]+ "/WechatMessage/ShowMessage" + "?Id=-2"}]]></Url></item></Articles></xml>");
//判断url是否为默认消息页
if (wcm.url.ToLower().Contains("/wechatmessage/showmessage"))
return Content($"<xml><ToUserName><![CDATA[{m.FromUserName}]]></ToUserName><FromUserName><![CDATA[{m.ToUserName}]]></FromUserName><CreateTime>{(DateTime.Now - a).TotalSeconds}</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[{wcm.title}]]></Title><Description><![CDATA[{wcm.content}]]></Description><PicUrl><![CDATA[{conf["DefaultImage"]}]]></PicUrl><Url><![CDATA[{wcm.url+"?Id="+wcm.id}]]></Url></item></Articles></xml>");
//发送消息
else
return Content($"<xml><ToUserName><![CDATA[{m.FromUserName}]]></ToUserName><FromUserName><![CDATA[{m.ToUserName}]]></FromUserName><CreateTime>{(DateTime.Now - a).TotalSeconds}</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[{wcm.title}]]></Title><Description><![CDATA[{wcm.content}]]></Description><PicUrl><![CDATA[{conf["DefaultImage"]}]]></PicUrl><Url><![CDATA[{wcm.url + "?OpenId=" + m.FromUserName}]]></Url></item></Articles></xml>");
}
}
return Content("success");
}
//微信菜单
public class WeChatMenu
{
[Key]
public int id { get; set; }
public string type { get; set; }
public string name { get; set; }
[NotMapped]
public int key { get; set; }
public string title { get; set; }
public string content { get; set; }
public string url { get; set; }
public int pid { get; set; }
public int isOutUrl { get; set; }
public int order { get; set; }
[NotMapped]
public string changeOrder { get; set; }
[NotMapped]
public List<WeChatMenu> sub_button { get; set; }
}
//响应消息
public class Message
{
/// <summary>
/// 本公众账号
/// </summary>
public string ToUserName { get; set; }
/// <summary>
/// 用户openid
/// </summary>
public string FromUserName { get; set; }
/// <summary>
/// 发送时间戳
/// </summary>
public string CreateTime { get; set; }
/// <summary>
/// 发送的文本内容
/// </summary>
public string Content { get; set; }
/// <summary>
/// 消息的类型
/// </summary>
public string MsgType { get; set; }
/// <summary>
/// 事件名称
/// </summary>
public string Event { get; set; }
/// <summary>
/// 事件KEY值
/// </summary>
public int EventKey { get; set; }
}