#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
{
///
/// ResponseMessage 消息处理方法工厂类
///
public static class ResponseMessageFactory
{
//
//
//
//
// 63497820384
// text
//
// 0
//
///
/// 获取XDocument转换后的IResponseMessageBase实例(通常在反向读取日志的时候用到)。
/// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
///
///
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;
}
///
/// 获取XDocument转换后的IRequestMessageBase实例。
/// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常
///
///
public static IResponseMessageBase GetResponseEntity(string xml)
{
return GetResponseEntity(XDocument.Parse(xml));
}
///
/// 将ResponseMessage实体转为XML
///
/// ResponseMessage实体
///
public static XDocument ConvertEntityToXml(ResponseMessageBase entity)
{
return Senparc.NeuChar.Helpers.EntityHelper.ConvertEntityToXml(entity);
}
}
}