Exemplo n.º 1
0
        /*      媒體資源下載和管理        */

        /// <summary>
        /// 保存多媒体资源
        /// </summary>
        /// <param name="openId">用户的OpenId</param>
        /// <param name="MediaId">媒体的MediaId</param>
        /// <param name="localId">成为本地资源之后的本地Id</param>
        /// <param name="type">多媒体的类型</param>
        /// <returns></returns>
        public virtual ResultM SaveMediaResource(string openId, string MediaId, UploadMediaFileType Type)
        {
            //结果容器
            ResultM result  = new ResultM();
            int     localId = -1;

            //通过“本地资源”模块,下载资源并获得LocalId
            switch (Type)
            {
            default:
                throw new Exception("目前不支持此类型。" + Type.ToString());

            case UploadMediaFileType.thumb:
            case UploadMediaFileType.image:
            {
                WeChatPictureService service = new WeChatPictureService("QY");
                localId = service.DownLoadResources(MediaId);
                EGExceptionResult pResult = service.GetActionResult();
                if (pResult != null)
                {
                    result.Message = pResult.Message;
                }
            }
            break;

            //case UploadMediaFileType.video:     //视频类型,微信文档写着不支持下载
            //break;

            case UploadMediaFileType.voice:
            {
                WeChatVoiceService service = new WeChatVoiceService("QY");
                localId = service.DownLoadResources(MediaId);
                EGExceptionResult pResult = service.GetActionResult();
                if (pResult != null)
                {
                    result.Message = pResult.Message;
                }
            }
            break;
            }

            //记录到数据库
            if (localId >= 0)
            {
                var affectedRows =
                    MediaDA.SaveMediaResource(new WCR_Media_Resource
                {
                    OpenId      = openId,
                    ID          = MediaId,
                    LocalID     = localId,
                    Type        = (int)Type,
                    Date        = DateTime.Now.Date,
                    CreatedTime = DateTime.Now,
                });

                result.Affected = affectedRows;
            }

            //返回结果
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取素材转换方法——用于转换被动回复消息
        /// 暂时企业号独占
        /// </summary>
        /// <param name="msgType"></param>
        /// <returns></returns>
        public Func <int, string, Senparc.Weixin.QY.Entities.IResponseMessageBase> GetResConvertForResponse(string msgType)
        {
            if (_DicRCForResponse == null || _DicRCForResponse.Count == 0)
            {
                _DicRCForResponse = new Dictionary <string, Func <int, string, Senparc.Weixin.QY.Entities.IResponseMessageBase> >();


                _DicRCForResponse.Add("text", (int lcId, string mType) =>
                {
                    var responseMessage     = new Senparc.Weixin.QY.Entities.ResponseMessageText();
                    responseMessage.Content = "测试";
                    return(responseMessage);
                });
                _DicRCForResponse.Add("image", (int lcId, string mType) =>
                {
                    var pAr  = new WeChatPictureService("QY");
                    var pImg = pAr.LoadResourcesSingleBylcId(lcId);

                    var responseMessage = new Senparc.Weixin.QY.Entities.ResponseMessageImage();
                    if (pImg != null)
                    {
                        responseMessage.Image.MediaId = pImg.media_id;
                    }
                    return(responseMessage);
                });
                _DicRCForResponse.Add("voice", (int lcId, string mType) =>
                {
                    var pAr    = new WeChatVoiceService("QY");
                    var pVoice = pAr.LoadResourcesSingleBylcId(lcId);

                    var responseMessage = new Senparc.Weixin.QY.Entities.ResponseMessageVoice();
                    if (pVoice != null)
                    {
                        responseMessage.Voice.MediaId = pVoice.media_id;
                    }
                    return(responseMessage);
                });
                _DicRCForResponse.Add("video", (int lcId, string mType) =>
                {
                    var pAr    = new WeChatVideoService("QY");
                    var pVideo = pAr.LoadResourcesSingleBylcId(lcId);

                    var responseMessage = new Senparc.Weixin.QY.Entities.ResponseMessageVideo();
                    if (pVideo != null)
                    {
                        responseMessage.Video.Title   = pVideo.lcName;
                        responseMessage.Video.MediaId = pVideo.media_id;
                    }
                    return(responseMessage);
                });
                _DicRCForResponse.Add("news", (int lcId, string mType) =>
                {
                    var pAr = new WeChatArticleService("QY");
                    pAr.ArticleConvertFunc = this.CNews2RspArticle(msgType);
                    List <object> pos      = pAr.LoadResources2News(lcId, mType);
                    if (pos == null || pos.Count == 0)
                    {
                        return(null);
                    }

                    var responseMessage      = new Senparc.Weixin.QY.Entities.ResponseMessageNews();
                    responseMessage.Articles = pos.Select(p => (p as Senparc.Weixin.QY.Entities.Article)).ToList();
                    return(responseMessage);
                });
                _DicRCForResponse.Add("mpnews", (int lcId, string mType) =>
                {
                    var pAr = new WeChatArticleService("QY");
                    pAr.ArticleConvertFunc = this.CNews2RspArticle(msgType);
                    List <object> pos      = pAr.LoadResources2News(lcId, mType);

                    var responseMessage      = new Senparc.Weixin.QY.Entities.ResponseMessageNews();
                    responseMessage.Articles = pos.Select(p => (p as Senparc.Weixin.QY.Entities.Article)).ToList();
                    int idx = 0;
                    foreach (var a in responseMessage.Articles)
                    {
                        a.Url = string.Format("http://{0}/WXArticle/Index?lcid={1}&idx={2}", RHost, lcId, idx);
                        idx  += 1;
                    }
                    return(responseMessage);
                });
            }

            if (!_DicRCForResponse.ContainsKey(msgType))
            {
                return(null);
            }
            return(_DicRCForResponse[msgType]);
        }