123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #region Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright 2019 Jeffrey Su & Suzhou Senparc Network Technology Co.,Ltd.
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software distributed under the
- License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
- either express or implied. See the License for the specific language governing permissions
- and limitations under the License.
- Detail: https://github.com/JeffreySu/WeiXinMPSDK/blob/master/license.md
- ----------------------------------------------------------------*/
- #endregion Apache License Version 2.0
- /*----------------------------------------------------------------
- Copyright (C) 2019 Senparc
-
- 文件名:ResponseMessageFactory.cs
- 文件功能描述:获取XDocument转换后的IResponseMessageBase实例
-
-
- 创建标识:Senparc - 20150211
-
- 修改标识:Senparc - 20150303
- 修改描述:整理接口
-
- 修改标识:Senparc - 20151208
- 修改描述:v13.4.6 添加ConvertEntityToXml()方法
- ----------------------------------------------------------------*/
- using System;
- using System.Xml.Linq;
- using Senparc.NeuChar;
- using Senparc.NeuChar.Entities;
- using Senparc.NeuChar.Helpers;
- using Senparc.Weixin.Exceptions;
- using Senparc.Weixin.MP.Entities;
- using Senparc.Weixin.MP.Helpers;
- namespace Senparc.Weixin.MP
- {
- /// <summary>
- /// ResponseMessage 消息处理方法工厂类
- /// </summary>
- public static class ResponseMessageFactory
- {
- //<?xml version="1.0" encoding="utf-8"?>
- //<xml>
- // <ToUserName><![CDATA[olPjZjsXuQPJoV0HlruZkNzKc91E]]></ToUserName>
- // <FromUserName><![CDATA[gh_a96a4a619366]]></FromUserName>
- // <CreateTime>63497820384</CreateTime>
- // <MsgType>text</MsgType>
- // <Content><![CDATA[您刚才发送了文字信息:中文
- //您还可以发送【位置】【图片】【语音】信息,查看不同格式的回复。
- //SDK官方地址:https://sdk.weixin.senparc.com]]></Content>
- // <FuncFlag>0</FuncFlag>
- //</xml>
- /// <summary>
- /// 获取XDocument转换后的IResponseMessageBase实例(通常在反向读取日志的时候用到)。
- /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
- /// </summary>
- /// <returns></returns>
- public static IResponseMessageBase GetResponseEntity(XDocument doc)
- {
- ResponseMessageBase responseMessage = null;
- ResponseMsgType msgType;
- try
- {
- msgType = MsgTypeHelper.GetResponseMsgType(doc);
- switch (msgType)
- {
- case ResponseMsgType.Text:
- responseMessage = new ResponseMessageText();
- break;
- case ResponseMsgType.Image:
- responseMessage = new ResponseMessageImage();
- break;
- case ResponseMsgType.Voice:
- responseMessage = new ResponseMessageVoice();
- break;
- case ResponseMsgType.Video:
- responseMessage = new ResponseMessageVideo();
- break;
- case ResponseMsgType.Music:
- responseMessage = new ResponseMessageMusic();
- break;
- case ResponseMsgType.News:
- responseMessage = new ResponseMessageNews();
- break;
- case ResponseMsgType.Transfer_Customer_Service:
- responseMessage = new ResponseMessageTransfer_Customer_Service();
- break;
- default:
- throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在ResponseMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException());
- }
- Senparc.NeuChar.Helpers.EntityHelper.FillEntityWithXml(responseMessage, doc);
- }
- catch (ArgumentException ex)
- {
- throw new WeixinException(string.Format("ResponseMessage转换出错!可能是MsgType不存在!,XML:{0}", doc.ToString()), ex);
- }
- return responseMessage;
- }
- /// <summary>
- /// 获取XDocument转换后的IRequestMessageBase实例。
- /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
- /// </summary>
- /// <returns></returns>
- public static IResponseMessageBase GetResponseEntity(string xml)
- {
- return GetResponseEntity(XDocument.Parse(xml));
- }
- /// <summary>
- /// 将ResponseMessage实体转为XML
- /// </summary>
- /// <param name="entity">ResponseMessage实体</param>
- /// <returns></returns>
- public static XDocument ConvertEntityToXml(ResponseMessageBase entity)
- {
- return Senparc.NeuChar.Helpers.EntityHelper.ConvertEntityToXml(entity);
- }
- }
- }
|