AcceptMessageAPI.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*--------------------------------------------------------------------------
  2. * AcceptMessageAPI.cs
  3. *Auth:deepleo
  4. * Date:2013.12.31
  5. * Email:2586662969@qq.com
  6. * Website:http://www.weixinsdk.net
  7. *--------------------------------------------------------------------------*/
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Dynamic;
  13. using System.Xml.Linq;
  14. using System.Xml;
  15. using Codeplex.Data;
  16. using Tencent;
  17. namespace LYFZ.Weixin.SDK
  18. {
  19. /// <summary>
  20. /// 对应微信API的 "接收消息"
  21. /// </summary>
  22. public class AcceptMessageAPI
  23. {
  24. /// <summary>
  25. /// 解析微信服务器推送的消息
  26. /// http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E6%99%AE%E9%80%9A%E6%B6%88%E6%81%AF
  27. /// http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E%A5%E6%94%B6%E4%BA%8B%E4%BB%B6%E6%8E%A8%E9%80%81
  28. /// </summary>
  29. /// <param name="message"></param>
  30. /// <returns></returns>
  31. public static WeixinMessage Parse(string message)
  32. {
  33. var msg = new WeixinMessage();
  34. msg.Body = new DynamicXml(message);
  35. string msgType = msg.Body.MsgType.Value;
  36. switch (msgType)
  37. {
  38. case "text":
  39. msg.Type = WeixinMessageType.Text;
  40. break;
  41. case "image":
  42. msg.Type = WeixinMessageType.Image;
  43. break;
  44. case "voice":
  45. msg.Type = WeixinMessageType.Voice;
  46. break;
  47. case "video":
  48. msg.Type = WeixinMessageType.Video;
  49. break;
  50. case "location":
  51. msg.Type = WeixinMessageType.Location;
  52. break;
  53. case "link":
  54. msg.Type = WeixinMessageType.Link;
  55. break;
  56. case "event":
  57. msg.Type = WeixinMessageType.Event;
  58. break;
  59. default: throw new Exception("does not support this message type:" + msgType);//不支持该消息类型
  60. }
  61. return msg;
  62. }
  63. }
  64. }