PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/Controllers/WeixinController.cs

https://github.com/mjhuangzk/WeiXinMPSDK
C# | 126 lines | 82 code | 16 blank | 28 comment | 5 complexity | c6f3ff63701e62a41920a2cb1a185c1a MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.Mvc;
  8. using System.Xml.Linq;
  9. using Senparc.Weixin.MP.MessageHandlers;
  10. namespace Senparc.Weixin.MP.Sample.Controllers
  11. {
  12. using Senparc.Weixin.MP.Entities;
  13. using Senparc.Weixin.MP.Helpers;
  14. using Senparc.Weixin.MP.MvcExtension;
  15. //using Senparc.Weixin.MP.Sample.Service;
  16. //using Senparc.Weixin.MP.Sample.CustomerMessageHandler;
  17. using Senparc.Weixin.MP.Sample.CommonService;
  18. using Senparc.Weixin.MP.Sample.CommonService.CustomMessageHandler;
  19. public partial class WeixinController : Controller
  20. {
  21. public readonly string Token = "weixin";//与微信公众账号后台的Token设置保持一致,区分大小写。
  22. public WeixinController()
  23. {
  24. }
  25. /// <summary>
  26. /// 微信后台验证地址(使用Get),微信后台的“接口配置信息”的Url填写如:http://weixin.senparc.com/weixin
  27. /// </summary>
  28. [HttpGet]
  29. [ActionName("Index")]
  30. public ActionResult Get(string signature, string timestamp, string nonce, string echostr)
  31. {
  32. if (CheckSignature.Check(signature, timestamp, nonce, Token))
  33. {
  34. return Content(echostr); //返回随机字符串则表示验证通过
  35. }
  36. else
  37. {
  38. return Content("failed:" + signature + "," + MP.CheckSignature.GetSignature(timestamp, nonce, Token)+"。如果您在浏览器中看到这条信息,表明此Url可以填入微信后台。");
  39. }
  40. }
  41. /// <summary>
  42. /// 用户发送消息后,微信平台自动Post一个请求到这里,并等待响应XML。
  43. /// PS:此方法为简化方法,效果与OldPost一致。
  44. /// v0.8之后的版本可以结合Senparc.Weixin.MP.MvcExtension扩展包,使用WeixinResult,见MiniPost方法。
  45. /// </summary>
  46. [HttpPost]
  47. [ActionName("Index")]
  48. public ActionResult Post(string signature, string timestamp, string nonce, string echostr)
  49. {
  50. if (!CheckSignature.Check(signature, timestamp, nonce, Token))
  51. {
  52. return Content("参数错误!");
  53. }
  54. //自定义MessageHandler,对微信请求的详细判断操作都在这里面。
  55. var messageHandler = new CustomMessageHandler(Request.InputStream);
  56. try
  57. {
  58. //测试时可开启此记录,帮助跟踪数据
  59. messageHandler.RequestDocument.Save(Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Request_" + messageHandler.RequestMessage.FromUserName + ".txt"));
  60. //执行微信处理过程
  61. messageHandler.Execute();
  62. //测试时可开启,帮助跟踪数据
  63. messageHandler.ResponseDocument.Save(Server.MapPath("~/App_Data/" + DateTime.Now.Ticks + "_Response_" + messageHandler.ResponseMessage.ToUserName + ".txt"));
  64. //return Content(messageHandler.ResponseDocument.ToString());//v0.7-
  65. return new WeixinResult(messageHandler);//v0.8+
  66. }
  67. catch (Exception ex)
  68. {
  69. using (TextWriter tw = new StreamWriter(Server.MapPath("~/App_Data/Error_" + DateTime.Now.Ticks + ".txt")))
  70. {
  71. tw.WriteLine("ExecptionMessage:" + ex.Message);
  72. tw.WriteLine(ex.Source);
  73. tw.WriteLine(ex.StackTrace);
  74. //tw.WriteLine("InnerExecptionMessage:" + ex.InnerException.Message);
  75. if (messageHandler.ResponseDocument != null)
  76. {
  77. tw.WriteLine(messageHandler.ResponseDocument.ToString());
  78. }
  79. tw.Flush();
  80. tw.Close();
  81. }
  82. return Content("");
  83. }
  84. }
  85. /// <summary>
  86. /// 最简化的处理流程
  87. /// </summary>
  88. [HttpPost]
  89. [ActionName("MiniPost")]
  90. public ActionResult MiniPost(string signature, string timestamp, string nonce, string echostr)
  91. {
  92. if (!CheckSignature.Check(signature, timestamp, nonce, Token))
  93. {
  94. //return Content("参数错误!");//v0.7-
  95. return new WeixinResult("参数错误!");//v0.8+
  96. }
  97. var messageHandler = new CustomMessageHandler(Request.InputStream);
  98. messageHandler.Execute();//执行微信处理过程
  99. //return Content(messageHandler.ResponseDocument.ToString());//v0.7-
  100. return new WeixinResult(messageHandler);//v0.8+
  101. }
  102. /*
  103. * v0.3.0之前的原始Post方法见:WeixinController_OldPost.cs
  104. *
  105. * 注意:虽然这里提倡使用CustomerMessageHandler的方法,但是MessageHandler基类最终还是基于OldPost的判断逻辑,
  106. * 因此如果需要深入了解Senparc.Weixin.MP内部处理消息的机制,可以查看WeixinController_OldPost.cs中的OldPost方法。
  107. * 目前为止OldPost依然有效,依然可用于生产。
  108. */
  109. }
  110. }