public OutputData Insert(IInputData input, object instance) { MpNewsArticle article = instance as MpNewsArticle; WeNewsMaterial news = new WeNewsMaterial(article); news.Add(); return(OutputData.CreateToolkitObject(new KeyData(article.Title, article.Content))); }
private static MpNewsArticle CreateArticle(DataRow docRow, string mediaId) { MpNewsArticle article = new MpNewsArticle(docRow["Title"].ToString(), mediaId, docRow["Content"].ToString()) { //Author = docRow["OrginOrg"].ToString(), Digest = docRow["PrevContent"].ToString(), ContentSourceUrl = "http://cxcs.mituyun.com/doc.vp?" + docRow["DocId"], ShowCoverPic = false }; return(article); }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": prop.SetValue(entity, DateTimeHelper.GetDateTimeFromXml(root.Element(propName).Value), null); break; case "Boolean": if (propName == "FuncFlag") { prop.SetValue(entity, root.Element(propName).Value == "1", null); } else { goto default; } break; case "Int32": prop.SetValue(entity, int.Parse(root.Element(propName).Value), null); break; case "Int64": prop.SetValue(entity, long.Parse(root.Element(propName).Value), null); break; case "Double": prop.SetValue(entity, double.Parse(root.Element(propName).Value), null); break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "ThirdPartyInfo": //ThirdPartyInfo适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } } }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": case "Int32": case "Int64": case "Double": case "Nullable`1": //可为空对象 EntityUtility.EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value); break; case "Boolean": if (propName == "FuncFlag") { EntityUtility.EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1"); } else { goto default; } break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "ThirdPartyInfo": //ThirdPartyInfo适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; case "BatchJobInfo": //异步任务完成事件推送BatchJob BatchJobInfo batchJobInfo = new BatchJobInfo(); FillEntityWithXml(batchJobInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, batchJobInfo, null); break; case "AgentType": { AgentType tp; #if NET35 try { tp = (AgentType)Enum.Parse(typeof(AgentType), root.Element(propName).Value, true); prop.SetValue(entity, tp, null); } catch { } #else if (Enum.TryParse(root.Element(propName).Value, out tp)) { prop.SetValue(entity, tp, null); } #endif break; } case "Receiver": { Receiver receiver = new Receiver(); FillEntityWithXml(receiver, new XDocument(root.Element(propName))); prop.SetValue(entity, receiver, null); break; } default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } else if (prop.PropertyType.Name == "List`1")//客服回调特殊处理 { var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "RequestBase") { List <RequestBase> items = new List <RequestBase>(); foreach (var item in root.Elements("Item")) { RequestBase reqItem = null; var msgTypeEle = item.Element("MsgType"); if (msgTypeEle != null) { RequestMsgType type = RequestMsgType.DEFAULT; var parseSuccess = false; #if NET35 try { type = (RequestMsgType)Enum.Parse(typeof(RequestMsgType), msgTypeEle.Value, true); parseSuccess = true; } catch { } #else parseSuccess = Enum.TryParse(msgTypeEle.Value, true, out type); #endif if (parseSuccess) { switch (type) { case RequestMsgType.Event: { reqItem = new RequestEvent(); break; } case RequestMsgType.File: { reqItem = new RequestMessageFile(); break; } case RequestMsgType.Image: { reqItem = new Entities.Request.KF.RequestMessageImage(); break; } case RequestMsgType.Link: { reqItem = new Entities.Request.KF.RequestMessageLink(); break; } case RequestMsgType.Location: { reqItem = new Entities.Request.KF.RequestMessageLocation(); break; } case RequestMsgType.Text: { reqItem = new Entities.Request.KF.RequestMessageText(); break; } case RequestMsgType.Voice: { reqItem = new Entities.Request.KF.RequestMessageVoice(); break; } } } } if (reqItem != null) { FillEntityWithXml(reqItem, new XDocument(item)); items.Add(reqItem); } } prop.SetValue(entity, items, null); } } } }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; if (root == null) { return;//无法处理 } var props = entity.GetType().GetProperties(); foreach (var prop in props) { if (!prop.CanWrite) { continue;//如果不可读则跳过 } var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": case "Int32": case "Int64": case "Double": case "Nullable`1": //可为空对象 EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value); break; case "Boolean": if (propName == "FuncFlag") { EntityUtility.FillSystemType(entity, prop, root.Element(propName).Value == "1"); } else { goto default; } break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 { var genericArguments = prop.PropertyType.GetGenericArguments(); var genericArgumentTypeName = genericArguments[0].Name; if (genericArgumentTypeName == "Article") { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArgumentTypeName == "Account") { List <CustomerServiceAccount> accounts = new List <CustomerServiceAccount>(); foreach (var item in root.Elements(propName)) { var account = new CustomerServiceAccount(); FillEntityWithXml(account, new XDocument(item)); accounts.Add(account); } prop.SetValue(entity, accounts, null); } else if (genericArgumentTypeName == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } else if (genericArgumentTypeName == "AroundBeacon") { List <AroundBeacon> aroundBeacons = new List <AroundBeacon>(); foreach (var item in root.Elements(propName).Elements("AroundBeacon")) { var aroundBeaconItem = new AroundBeacon(); FillEntityWithXml(aroundBeaconItem, new XDocument(item)); aroundBeacons.Add(aroundBeaconItem); } prop.SetValue(entity, aroundBeacons, null); } else if (genericArgumentTypeName == "CopyrightCheckResult_ResultList") //RequestMessageEvent_MassSendJobFinish { List <CopyrightCheckResult_ResultList> resultList = new List <CopyrightCheckResult_ResultList>(); foreach (var item in root.Elements("ResultList").Elements("item")) { CopyrightCheckResult_ResultList resultItem = new CopyrightCheckResult_ResultList(); FillEntityWithXml(resultItem.item, new XDocument(item)); resultList.Add(resultItem); } prop.SetValue(entity, resultList, null); } //企业微信 else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } break; } case "Music": //ResponseMessageMusic适用 FillClassValue <Music>(entity, root, propName, prop); break; case "Image": //ResponseMessageImage适用 FillClassValue <Image>(entity, root, propName, prop); break; case "Voice": //ResponseMessageVoice适用 FillClassValue <Voice>(entity, root, propName, prop); break; case "Video": //ResponseMessageVideo适用 FillClassValue <Video>(entity, root, propName, prop); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 FillClassValue <ScanCodeInfo>(entity, root, propName, prop); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 FillClassValue <SendLocationInfo>(entity, root, propName, prop); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 FillClassValue <SendPicsInfo>(entity, root, propName, prop); break; case "ChosenBeacon": //摇一摇事件通知 FillClassValue <ChosenBeacon>(entity, root, propName, prop); break; case "AroundBeacon": //摇一摇事件通知 FillClassValue <AroundBeacon>(entity, root, propName, prop); break; #region RequestMessageEvent_MassSendJobFinish case "CopyrightCheckResult": FillClassValue <CopyrightCheckResult>(entity, root, propName, prop); break; case "CopyrightCheckResult_ResultList_Item": FillClassValue <CopyrightCheckResult_ResultList_Item>(entity, root, "item", prop); break; #region 企业号 /* 暂时放在Work.dll中 * case "AgentType": * { * AgentType tp; #if NET35 * try * { * tp = (AgentType)Enum.Parse(typeof(AgentType), root.Element(propName).Value, true); * prop.SetValue(entity, tp, null); * } * catch * { * * } #else * if (Enum.TryParse(root.Element(propName).Value, out tp)) * { * prop.SetValue(entity, tp, null); * } #endif * break; * } * case "Receiver": * { * Receiver receiver = new Receiver(); * FillEntityWithXml(receiver, new XDocument(root.Element(propName))); * prop.SetValue(entity, receiver, null); * break; * } */ #endregion #endregion default: var enumSuccess = false; if (prop.PropertyType.IsEnum) { //未知的枚举类型 try { prop.SetValue(entity, Enum.Parse(prop.PropertyType, root.Element(propName).Value, true), null); enumSuccess = true; } catch { } } if (!enumSuccess) { prop.SetValue(entity, root.Element(propName).Value, null); } break; } } } }
public static List <object> CreateNewsResponseMessage(AutoReplyContentView content, int appId, bool isCorp, bool isSafe, bool isAutoReply = false) { // var photoTextMsg = this.CreateResponseMessage<ResponseMessageNews>(); var Articles = new List <object>(); if (content.IsNewContent.Value) { var info = JsonConvert.DeserializeObject <List <NewsInfoView> >(content.Content); var configs = Infrastructure.Web.Domain.Service.CommonService.lstSysConfig; // var config = configs.Where(a => a.ConfigName.Equals("Content Server", StringComparison.CurrentCultureIgnoreCase)).First(); //var contentConfig = configs.Where(a => a.ConfigName.Equals("Content Server", StringComparison.CurrentCultureIgnoreCase)).First(); string host = Infrastructure.Web.Domain.Service.CommonService.GetSysConfig("Content Server", "").TrimEnd('/');// config.ConfigValue; //if (host.EndsWith("/")) //{ // host = host.Substring(0, host.Length - 1); //} int ii = 0; foreach (var entity in info) { if (ii == 0) //位置不同,缩略图的比例不一样 { entity.ImageSrc = WechatCommon.doGetFileCover(entity.ImageSrc, "_B"); } else { entity.ImageSrc = WechatCommon.doGetFileCover(entity.ImageSrc, "_T"); } ii++; var picUrl = host + entity.ImageSrc; //var url = host + "/News/ArticleInfo/wxdetail/" + content.AutoReplyId + "?wechatid=" + appId;// "&subId=" + item.Id; var url = string.Format("{0}/{1}/Message/GetNews?id={2}&wechatid={2}&type={3}&subId={4}", host, isCorp ? "news" : "mpnews", content.AutoReplyId, appId, (int)NewsTypeEnum.AutoReply, entity.Id); //host + "/News/Message/GetNews?id=" + content.AutoReplyId + "&wechatid=" + appId + "&type=" + (int)NewsTypeEnum.AutoReply; var newArticle = new Article() { Title = entity.NewsTitle, Url = url, PicUrl = picUrl, Description = entity.NewsComment }; Articles.Add(newArticle); } } else { List <ArticleInfoView> articleList = new List <ArticleInfoView>(); if (content.SecondaryType == (int)AutoReplyNewsEnum.MANUAL) { List <int> articleIds = content.NewsID.Trim(',').Split(',').ToList().Select(n => int.Parse(n)).ToList(); if (articleIds.Count > 0) { log.Debug("article count :{0}.", articleIds.Count); var lst = ((DbSet <ArticleInfo>)_articleInfoService.Repository.Entities).AsNoTracking() .Where(t => t.AppId == appId && articleIds.Contains(t.Id) && t.IsDeleted == false).ToList() .Select(a => (ArticleInfoView) new ArticleInfoView().ConvertAPIModelListWithContent(a)).ToList(); //解决顺序问题 foreach (var aID in articleIds) { var al = lst.Find(a => a.Id == aID); if (al != null) { articleList.Add(al); } } } } else if (content.SecondaryType == (int)AutoReplyNewsEnum.LATEST) { articleList.AddRange(((DbSet <ArticleInfo>)_articleInfoService.Repository.Entities).AsNoTracking() .Where(t => t.AppId == appId && t.IsDeleted == false) .OrderBy("Id", System.ComponentModel.ListSortDirection.Descending) .Take(int.Parse(content.Content)).ToList() .Select(a => (ArticleInfoView) new ArticleInfoView().ConvertAPIModelListWithContent(a)).ToList()); } ; // articleList = articleList.Distinct().OrderByDescending(t => t.PublishDate).ToList(); var token = WeChatCommonService.GetWeiXinToken(appId); int ii = 0; foreach (var a in articleList) { log.Debug("Start ID:{0} ImageCoverUrl:{1} ", a.Id, a.ImageCoverUrl); if (ii == 0) //位置不同,缩略图的比例不一样 { a.ImageCoverUrl = WechatCommon.doGetFileCover(a.ImageCoverUrl, "_B"); } else { a.ImageCoverUrl = WechatCommon.doGetFileCover(a.ImageCoverUrl, "_T"); } ii++; if (isSafe) { var newArticle = new MpNewsArticle() { title = a.ArticleTitle, content_source_url = string.Format("{0}/{3}/ArticleInfo/wxdetail/{1}?wechatid={2}&isAutoReply={4}", _newsHost, a.Id, appId, isCorp ? "news" : "mpnews", isAutoReply ? 1 : 0),// _newsHost + "/News/ArticleInfo/wxdetail/" + a.Id + "?wechatid=" + appId, //content = _newsHost + a.ImageCoverUrl, digest = a.ArticleComment, content = a.ArticleContent,// WechatCommonMP.ImageConvert(a.ArticleContent, appId), author = a.CreatedUserID, show_cover_pic = "0", thumb_media_id = WechatCommon.GetMediaId(a.ImageCoverUrl, token) }; log.Debug("Creating MPNews - \r\n\tTitle: " + newArticle.title + "\r\n\tUrl: " + newArticle.content_source_url + "\r\n\tPush Image: " + newArticle.thumb_media_id); Articles.Add(newArticle); } else { var newArticle = new Article() { Title = a.ArticleTitle, Url = string.Format("{0}/{3}/ArticleInfo/wxdetail/{1}?wechatid={2}&isAutoReply={4}", _newsHost, a.Id, appId, isCorp ? "news" : "mpnews", isAutoReply ? 1 : 0),// _newsHost + "/News/ArticleInfo/wxdetail/" + a.Id + "?wechatid=" + appId, PicUrl = _newsHost + a.ImageCoverUrl.Replace("\\", "/").TrimStart('/'), Description = a.ArticleComment }; log.Debug("Creating News - \r\n\tTitle: " + newArticle.Title + "\r\n\tUrl: " + newArticle.Url + "\r\n\tPush Image: " + newArticle.PicUrl); Articles.Add(newArticle); } } } return(Articles); }
public void AddArticle(MpNewsArticle article) { TkDebug.AssertArgumentNull(article, "article", null); Articles.Add(article); }
/// <summary> /// 根据XML信息填充实实体 /// </summary> /// <typeparam name="T">MessageBase为基类的类型,Response和Request都可以</typeparam> /// <param name="entity">实体</param> /// <param name="doc">XML</param> public static void FillEntityWithXml <T>(this T entity, XDocument doc) where T : /*MessageBase*/ class, new() { entity = entity ?? new T(); var root = doc.Root; var props = entity.GetType().GetProperties(); foreach (var prop in props) { var propName = prop.Name; if (root.Element(propName) != null) { switch (prop.PropertyType.Name) { //case "String": // goto default; case "DateTime": prop.SetValue(entity, DateTimeHelper.GetDateTimeFromXml(root.Element(propName).Value), null); break; case "Boolean": if (propName == "FuncFlag") { prop.SetValue(entity, root.Element(propName).Value == "1", null); } else { goto default; } break; case "Int32": if (!string.IsNullOrEmpty(root.Element(propName).Value)) { prop.SetValue(entity, int.Parse(root.Element(propName).Value), null); } break; case "Int64": if (!string.IsNullOrEmpty(root.Element(propName).Value)) { prop.SetValue(entity, long.Parse(root.Element(propName).Value), null); } break; case "Double": if (!string.IsNullOrEmpty(root.Element(propName).Value)) { prop.SetValue(entity, double.Parse(root.Element(propName).Value), null); } break; //以下为枚举类型 case "RequestMsgType": //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetRequestMsgType(root.Element(propName).Value), null); break; case "ResponseMsgType": //Response适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "ThirdPartyInfo": //ThirdPartyInfo适用 //已设为只读 //prop.SetValue(entity, MsgTypeHelper.GetResponseMsgType(root.Element(propName).Value), null); break; case "Event": //已设为只读 //prop.SetValue(entity, EventHelper.GetEventType(root.Element(propName).Value), null); break; //以下为实体类型 case "List`1": //List<T>类型,ResponseMessageNews适用 var genericArguments = prop.PropertyType.GetGenericArguments(); if (genericArguments[0].Name == "Article") //ResponseMessageNews适用 { //文章下属节点item List <Article> articles = new List <Article>(); foreach (var item in root.Element(propName).Elements("item")) { var article = new Article(); FillEntityWithXml(article, new XDocument(item)); articles.Add(article); } prop.SetValue(entity, articles, null); } else if (genericArguments[0].Name == "MpNewsArticle") { List <MpNewsArticle> mpNewsArticles = new List <MpNewsArticle>(); foreach (var item in root.Elements(propName)) { var mpNewsArticle = new MpNewsArticle(); FillEntityWithXml(mpNewsArticle, new XDocument(item)); mpNewsArticles.Add(mpNewsArticle); } prop.SetValue(entity, mpNewsArticles, null); } else if (genericArguments[0].Name == "PicItem") { List <PicItem> picItems = new List <PicItem>(); foreach (var item in root.Elements(propName).Elements("item")) { var picItem = new PicItem(); var picMd5Sum = item.Element("PicMd5Sum").Value; Md5Sum md5Sum = new Md5Sum() { PicMd5Sum = picMd5Sum }; picItem.item = md5Sum; picItems.Add(picItem); } prop.SetValue(entity, picItems, null); } else if (genericArguments[0].Name.ToLower() == "irequestmessagebase") //群聊或者客服接口适用 { //文章下属节点item List <IRequestMessageBase> articles = new List <IRequestMessageBase>(); foreach (var item in root.Elements(propName)) { RequestMessageBase requestMessage; var msgType = MsgTypeHelper.GetRequestMsgType(item); switch (msgType) { case RequestMsgType.Text: requestMessage = new RequestMessageChatText(); break; case RequestMsgType.ShortVideo: requestMessage = new RequestMessageChatShortVideo(); break; case RequestMsgType.Image: requestMessage = new RequestMessageChatImage(); break; case RequestMsgType.Voice: requestMessage = new RequestMessageChatVoice(); break; case RequestMsgType.Event: var eventType = item.Element("Event").Value.ToLower(); switch (eventType) { case "create_chat": requestMessage = new RequestMessageChatEvent_Create(); break; case "update_chat": requestMessage = new RequestMessageChatEvent_Update(); break; case "quit_chat": requestMessage = new RequestMessageChatEvent_Quit(); break; default: throw new WeixinException(string.Format("EventType:{0} 在FillEntityWithXml中没有对应的处理程序!", eventType), new ArgumentOutOfRangeException()); //为了能够对类型变动最大程度容错(如微信目前还可以对公众账号suscribe等未知类型,但API没有开放),建议在使用的时候catch这个异常 } //requestMessage = new RequestMessageChatVoice(); break; default: requestMessage = new RequestMessageChatText(); break; } //var article = new Article(); FillEntityWithXml(requestMessage, new XDocument(item)); articles.Add(requestMessage); } prop.SetValue(entity, articles, null); } break; case "Image": //ResponseMessageImage适用 Image image = new Image(); FillEntityWithXml(image, new XDocument(root.Element(propName))); prop.SetValue(entity, image, null); break; case "Voice": //ResponseMessageVoice适用 Voice voice = new Voice(); FillEntityWithXml(voice, new XDocument(root.Element(propName))); prop.SetValue(entity, voice, null); break; case "Video": //ResponseMessageVideo适用 Video video = new Video(); FillEntityWithXml(video, new XDocument(root.Element(propName))); prop.SetValue(entity, video, null); break; case "ChatInfo": //ResponseMessageVideo适用 ChatInfo chat = new ChatInfo(); FillEntityWithXml(chat, new XDocument(root.Element(propName))); prop.SetValue(entity, chat, null); break; case "Receiver": //ResponseMessageVideo适用 Receiver Receiver = new Receiver(); FillEntityWithXml(Receiver, new XDocument(root.Element(propName))); prop.SetValue(entity, Receiver, null); break; case "ScanCodeInfo": //扫码事件中的ScanCodeInfo适用 ScanCodeInfo scanCodeInfo = new ScanCodeInfo(); FillEntityWithXml(scanCodeInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, scanCodeInfo, null); break; case "SendLocationInfo": //弹出地理位置选择器的事件推送中的SendLocationInfo适用 SendLocationInfo sendLocationInfo = new SendLocationInfo(); FillEntityWithXml(sendLocationInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendLocationInfo, null); break; case "SendPicsInfo": //系统拍照发图中的SendPicsInfo适用 SendPicsInfo sendPicsInfo = new SendPicsInfo(); FillEntityWithXml(sendPicsInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, sendPicsInfo, null); break; case "BatchJobInfo": //异步任务完成事件推送BatchJob BatchJobInfo batchJobInfo = new BatchJobInfo(); FillEntityWithXml(batchJobInfo, new XDocument(root.Element(propName))); prop.SetValue(entity, batchJobInfo, null); break; default: prop.SetValue(entity, root.Element(propName).Value, null); break; } } } }
internal WeNewsUpdate(WeMediaId mediaId, int index, MpNewsArticle articles) { MediaId = mediaId.MediaId; Index = index; Articles = articles; }