ResponseMessageFactory.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #region Apache License Version 2.0
  2. /*----------------------------------------------------------------
  3. Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
  5. except in compliance with the License. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software distributed under the
  8. License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  9. either express or implied. See the License for the specific language governing permissions
  10. and limitations under the License.
  11. Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
  12. ----------------------------------------------------------------*/
  13. #endregion Apache License Version 2.0
  14. /*----------------------------------------------------------------
  15. Copyright (C) 2019 Senparc
  16. 文件名:ResponseMessageFactory.cs
  17. 文件功能描述:获取XDocument转换后的IResponseMessageBase实例
  18. 创建标识:Senparc - 20150211
  19. 修改标识:Senparc - 20150303
  20. 修改描述:整理接口
  21. 修改标识:Senparc - 20151208
  22. 修改描述:v13.4.6 添加ConvertEntityToXml()方法
  23. ----------------------------------------------------------------*/
  24. using System;
  25. using System.Xml.Linq;
  26. using Senparc.NeuChar;
  27. using Senparc.NeuChar.Entities;
  28. using Senparc.NeuChar.Helpers;
  29. using Senparc.Weixin.Exceptions;
  30. using Senparc.Weixin.MP.Entities;
  31. using Senparc.Weixin.MP.Helpers;
  32. namespace Senparc.Weixin.MP
  33. {
  34. /// <summary>
  35. /// ResponseMessage 消息处理方法工厂类
  36. /// </summary>
  37. public static class ResponseMessageFactory
  38. {
  39. //<?xml version="1.0" encoding="utf-8"?>
  40. //<xml>
  41. // <ToUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></ToUserName>
  42. // <FromUserName><![CDATA[gh_a96a4a619366]]></FromUserName>
  43. // <CreateTime>63497820384</CreateTime>
  44. // <MsgType>text</MsgType>
  45. // <Content><![CDATA[您刚才发送了文字信息:中文
  46. //您还可以发送【位置】【图片】【语音】信息,查看不同格式的回复。
  47. //SDK官方地址:https://sdk.weixin.senparc.com]]></Content>
  48. // <FuncFlag>0</FuncFlag>
  49. //</xml>
  50. /// <summary>
  51. /// 获取XDocument转换后的IResponseMessageBase实例(通常在反向读取日志的时候用到)。
  52. /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
  53. /// </summary>
  54. /// <returns></returns>
  55. public static IResponseMessageBase GetResponseEntity(XDocument doc)
  56. {
  57. ResponseMessageBase responseMessage = null;
  58. ResponseMsgType msgType;
  59. try
  60. {
  61. msgType = MsgTypeHelper.GetResponseMsgType(doc);
  62. switch (msgType)
  63. {
  64. case ResponseMsgType.Text:
  65. responseMessage = new ResponseMessageText();
  66. break;
  67. case ResponseMsgType.Image:
  68. responseMessage = new ResponseMessageImage();
  69. break;
  70. case ResponseMsgType.Voice:
  71. responseMessage = new ResponseMessageVoice();
  72. break;
  73. case ResponseMsgType.Video:
  74. responseMessage = new ResponseMessageVideo();
  75. break;
  76. case ResponseMsgType.Music:
  77. responseMessage = new ResponseMessageMusic();
  78. break;
  79. case ResponseMsgType.News:
  80. responseMessage = new ResponseMessageNews();
  81. break;
  82. case ResponseMsgType.Transfer_Customer_Service:
  83. responseMessage = new ResponseMessageTransfer_Customer_Service();
  84. break;
  85. default:
  86. throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在ResponseMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException());
  87. }
  88. Senparc.NeuChar.Helpers.EntityHelper.FillEntityWithXml(responseMessage, doc);
  89. }
  90. catch (ArgumentException ex)
  91. {
  92. throw new WeixinException(string.Format("ResponseMessage转换出错!可能是MsgType不存在!,XML:{0}", doc.ToString()), ex);
  93. }
  94. return responseMessage;
  95. }
  96. /// <summary>
  97. /// 获取XDocument转换后的IRequestMessageBase实例。
  98. /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
  99. /// </summary>
  100. /// <returns></returns>
  101. public static IResponseMessageBase GetResponseEntity(string xml)
  102. {
  103. return GetResponseEntity(XDocument.Parse(xml));
  104. }
  105. /// <summary>
  106. /// 将ResponseMessage实体转为XML
  107. /// </summary>
  108. /// <param name="entity">ResponseMessage实体</param>
  109. /// <returns></returns>
  110. public static XDocument ConvertEntityToXml(ResponseMessageBase entity)
  111. {
  112. return Senparc.NeuChar.Helpers.EntityHelper.ConvertEntityToXml(entity);
  113. }
  114. }
  115. }