コード例 #1
0
        public static string DoNewsInfo(NewsInfoView news, SysWechatConfig config, AutoReplyContentEnum type, string strFileName)
        {
            string mediaId = null;

            if (news.materialId.HasValue && news.materialId.Value > 0) //有素材
            {
                IAttachmentsItemService _attachmentsItemService = EngineContext.Current.Resolve <IAttachmentsItemService>();

                NewsInfoView news1;
                mediaId = WechatCommon.GetMediaIDByFileID(news.materialId, _attachmentsItemService, config.WeixinCorpId, out news1);
            }
            else
            {
                mediaId = WechatCommon.GetMediaInfo(type, news, news.AppId);

                NewsToAttachments(news, type.ToString().ToLower(), strFileName);
            }
            return(mediaId);
        }
コード例 #2
0
        /// <summary>
        /// 根据资源ID获取回复信息,如果过期重新上传
        /// </summary>
        /// <param name="fileID"></param>
        /// <param name="news"></param>
        /// <returns></returns>
        public static string GetMediaIDByFileID(int?fileID, IAttachmentsItemService _attachmentsItemService, string CorpId, out NewsInfoView news)
        {
            if (fileID == null)
            {
                throw new Exception("do not found file id.");
            }
            else
            {
                log.Debug("GetMediaIDByFileID start:   fileID:{0}", fileID);

                //
                var fileInfo = _attachmentsItemService.GetById <AttachmentsItemView>((int)fileID);

                if (fileInfo != null)
                {
                    news = new NewsInfoView()
                    {
                        NewsContent  = "",
                        NewsTitle    = fileInfo.AttachmentTitle,
                        ImageContent = fileInfo.AttachmentUrl,
                        VideoContent = fileInfo.AttachmentUrl,
                        FileSrc      = fileInfo.AttachmentUrl,
                        SoundSrc     = fileInfo.AttachmentUrl,
                        MediaId      = fileInfo.MediaId,
                        NewsComment  = fileInfo.Description
                    };
                }
                else
                {
                    news = new NewsInfoView();
                }

                if (fileInfo != null && (string.IsNullOrEmpty(fileInfo.MediaId) || !fileInfo.MediaExpireTime.HasValue || fileInfo.MediaExpireTime.Value < DateTime.Now))
                {
                    var config = WeChatCommonService.lstSysWeChatConfig.Find(a => a.WeixinCorpId == CorpId);


                    AutoReplyContentEnum cate = AutoReplyContentEnum.FILE;
                    if (fileInfo.Type == (int)AttachmentsType.IMAGE)
                    {
                        cate = AutoReplyContentEnum.IMAGE;
                        if (fileInfo.FileSize > 2 * 1024 * 1024)
                        {
                            throw new Exception("图片文件不能大于2M!");
                        }
                    }
                    else if (fileInfo.Type == (int)AttachmentsType.VIDEO)
                    {
                        cate = AutoReplyContentEnum.VIDEO;
                        if (fileInfo.FileSize > 10 * 1024 * 1024)
                        {
                            throw new Exception("视频文件不能大于10M!");
                        }
                    }
                    else if (fileInfo.Type == (int)AttachmentsType.AUDIO)
                    {
                        cate = AutoReplyContentEnum.VOICE;
                        if (fileInfo.FileSize > 2 * 1024 * 1024)
                        {
                            throw new Exception("声音文件不能大于210M!");
                        }
                    }
                    else if (fileInfo.Type == (int)AttachmentsType.FILE)
                    {
                        cate = AutoReplyContentEnum.FILE;
                        if (fileInfo.FileSize > 20 * 1024 * 1024)
                        {
                            throw new Exception("普通文件不能大于20M!");
                        }
                    }


                    news.NewsCate = cate.ToString().ToLower();
                    news.AppId    = config.Id;


                    Innocellence.WeChatMain.Common.WechatCommon.GetMediaInfo(cate, news, config.Id);

                    _attachmentsItemService.UpdateMediaId(fileInfo.Id, news.MediaId, DateTimeHelper.GetDateTimeFromXml(news.MediaCreateTime));

                    log.Debug("GetMediaIDByFileID end  GetNewMediaId:   fileID:{0} MediaId:{1}", fileID, news.MediaId);

                    return(news.MediaId);
                }
                else
                {
                    log.Debug("GetMediaIDByFileID end  Use Old MediaId:   fileID:{0}   MediaId:{1}", fileID, news.MediaId);

                    return(fileInfo == null ? string.Empty : fileInfo.MediaId);
                }
            }
        }