You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
4.9 KiB
115 lines
4.9 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using Model;
|
|
using Senparc.Weixin.MP;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Xml;
|
|
using Znyc.Cloudcar.Admin.Security.IServices;
|
|
using PostModel = Senparc.Weixin.MP.Entities.Request.PostModel;
|
|
|
|
namespace Znyc.Cloudcar.Admin.WebApi.Controllers
|
|
{
|
|
/// <summary>
|
|
/// 微信公众号
|
|
/// </summary>
|
|
public class WeixinController : ControllerBase
|
|
{
|
|
public static readonly string Token = "rzC0pRTuBznwd3";
|
|
//Config.SenparcWeixinSetting.MpSetting.Token;//与微信公众账号后台的Token设置保持一致,区分大小写。
|
|
public static readonly string EncodingAESKey = "VtEIJLTcbrkGT18EkevZzKIBYgqpURtxyH6w2iWR5eD";
|
|
//Config.SenparcWeixinSetting.MpSetting.EncodingAESKey;//与微信公众账号后台的EncodingAESKey设置保持一致,区分大小写。
|
|
public static readonly string AppId = "wx07c574aca93ae8d9";
|
|
//Config.SenparcWeixinSetting.MpSetting.WeixinAppId;//与微信公众账号后台的AppId设置保持一致,区分大小写。
|
|
private readonly IWxUserRelationService _wxUserRelationService;
|
|
|
|
|
|
|
|
public WeixinController(IWxUserRelationService wxUserRelationService)
|
|
{
|
|
_wxUserRelationService = wxUserRelationService;
|
|
}
|
|
/// <summary>
|
|
/// 微信后台验证地址
|
|
/// </summary>
|
|
[HttpGet]
|
|
[ActionName("Index")]
|
|
public ActionResult Get(PostModel postModel, string echostr)
|
|
{
|
|
if (CheckSignature.Check(postModel.Signature, postModel.Timestamp, postModel.Nonce, Token))
|
|
{
|
|
return Content(echostr); //返回随机字符串则表示验证通过
|
|
}
|
|
else
|
|
{
|
|
return Content("failed:" + postModel.Signature + "," + Senparc.Weixin.MP.CheckSignature.GetSignature(postModel.Timestamp, postModel.Nonce, Token) + "。" +
|
|
"如果你在浏览器中看到这句话,说明此地址可以被作为微信公众账号后台的Url,请注意保持Token一致。");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用户发送消息后,微信平台自动Post一个请求到这里,并等待响应XML。
|
|
/// PS:此方法为简化方法,效果与OldPost一致。
|
|
/// v0.8之后的版本可以结合Senparc.Weixin.MP.MvcExtension扩展包,使用WeixinResult,见MiniPost方法。
|
|
/// </summary>
|
|
[HttpPost]
|
|
[ActionName("Index")]
|
|
public async Task<ActionResult> Post(string signature, string timestamp, string nonce)
|
|
{
|
|
|
|
try
|
|
{
|
|
StreamReader sr = new StreamReader(Request.Body, Encoding.UTF8);
|
|
XmlDocument doc = new XmlDocument();
|
|
doc.Load(sr);
|
|
sr.Close();
|
|
sr.Dispose();
|
|
WxMessage wxMessage = new WxMessage();
|
|
wxMessage.ToUserName = doc.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText;
|
|
wxMessage.FromUserName = doc.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText;
|
|
wxMessage.MsgType = doc.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText;
|
|
wxMessage.CreateTime = int.Parse(doc.SelectSingleNode("xml").SelectSingleNode("CreateTime").InnerText);
|
|
if (wxMessage.MsgType == "event")
|
|
{
|
|
wxMessage.EventName = doc.SelectSingleNode("xml").SelectSingleNode("Event").InnerText;
|
|
if (!string.IsNullOrEmpty(wxMessage.EventName) && wxMessage.EventName == "subscribe")
|
|
{
|
|
string content = "您好,欢迎关注众能云车服务号";
|
|
content = SendTextMessage(wxMessage, content);
|
|
await _wxUserRelationService.UpdateAsync(wxMessage.FromUserName);
|
|
return Content(content);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//记录日志
|
|
return Content("");
|
|
}
|
|
return Content("");
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Send
|
|
/// </summary>
|
|
/// <param name="wxmessage"></param>
|
|
/// <param name="content"></param>
|
|
/// <returns></returns>
|
|
private string SendTextMessage(WxMessage wxmessage, string content)
|
|
{
|
|
string result = string.Format(@"<xml>
|
|
<ToUserName><![CDATA[{0}]]></ToUserName>
|
|
<FromUserName><![CDATA[{1}]]></FromUserName>
|
|
<CreateTime>{2}</CreateTime>
|
|
<MsgType><![CDATA[text]]></MsgType>
|
|
<Content><![CDATA[{3}]]></Content>
|
|
</xml>", wxmessage.FromUserName, wxmessage.ToUserName, DateTime.Now.Ticks, content);
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|