Exemplo n.º 1
0
        public string RemoveNormalMaterial(DomainContext domainContext, Guid id, string mediaId)
        {
            RequestApiResult requestApiResult = MaterialApiWrapper.RemoveMaterial(domainContext, mediaId);

            if (requestApiResult.Success == false)
            {
                return(requestApiResult.Message);
            }

            List <CommandParameter> parameterList = new List <CommandParameter>();

            parameterList.Add(new CommandParameter("@id", id));
            parameterList.Add(new CommandParameter("@mediaId", mediaId));

            _dataBase.ExecuteNonQuery("DELETE FROM [NormalMaterial] WHERE [Id] = @id AND [MediaId] = @mediaId",
                                      parameterList);

            return(null);
        }
Exemplo n.º 2
0
        public string RemoveArticleMaterial(DomainContext domainContext, Guid id, string mediaId)
        {
            //未发布的素材无 mediaId
            if (String.IsNullOrEmpty(mediaId) == false)
            {
                RequestApiResult requestApiResult = MaterialApiWrapper.RemoveMaterial(domainContext, mediaId);
                if (requestApiResult.Success == false)
                {
                    return(requestApiResult.Message);
                }
            }

            List <CommandParameter> parameterList = new List <CommandParameter>();

            parameterList.Add(new CommandParameter("@id", id));

            _dataBase.ExecuteNonQuery("DELETE FROM [ArticleMaterial] WHERE [Id] = @id",
                                      parameterList);

            _dataBase.ExecuteNonQuery("DELETE FROM [ArticleMaterialItem] WHERE [ArticleMaterial] = @id",
                                      parameterList);

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 发布图文消息到微信后台
        /// </summary>
        /// <param name="domainContext"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private string AddArticleMaterialToWeixin(DomainContext domainContext, ArticleMaterialEntity args)
        {
            //向微信后台发送
            WeixinAddArticleMaterialArgs weixinArgs = new WeixinAddArticleMaterialArgs();

            weixinArgs.ArticleList = new List <WeixinArticleMaterial>();
            for (int i = 0; i < args.ArticleList.Count; i++)
            {
                ArticleMaterialItemEntity item = args.ArticleList[i];
                item.Index = i;

                //替换其中的图片地址为上传到微信服务器的地址
                if (item.ImgMappingList != null)
                {
                    foreach (var imgMappingItem in item.ImgMappingList)
                    {
                        if (String.IsNullOrEmpty(imgMappingItem.FileUrl) || String.IsNullOrEmpty(imgMappingItem.WeixinUrl))
                        {
                            continue;
                        }

                        item.Content =
                            item.Content.Replace(imgMappingItem.FileUrl, imgMappingItem.WeixinUrl);
                    }
                }

                weixinArgs.ArticleList.Add(item);
            }

            RequestApiResult <WeixinAddArticleMaterialResult> addArticleMaterialResult =
                MaterialApiWrapper.AddArticleMaterial(domainContext, weixinArgs);

            if (addArticleMaterialResult.Success)
            {
                //得到图文素材的MediaId
                string mediaId = addArticleMaterialResult.ApiResult.MediaId;
                args.MediaId = mediaId;

                //更新图文素材表中的MediaId字段
                List <CommandParameter> parameterList = new List <CommandParameter>();
                parameterList.Add(new CommandParameter("@id", args.Id));
                parameterList.Add(new CommandParameter("@mediaId", mediaId));

                _dataBase.ExecuteNonQuery("UPDATE [ArticleMaterial] SET [MediaId] = @mediaId,[WeixinStatus] = 1 WHERE [Id] = @id",
                                          parameterList);

                //因为微信API在添加图文后不返回URL,所以需要通过获取素材接口再次获取以便得到URL
                //GetArticleMaterial
                RequestApiResult <WeixinArticleMaterialListItemContent> getArticleMaterialResult =
                    MaterialApiWrapper.GetArticleMaterial(domainContext, mediaId);

                if (getArticleMaterialResult.Success)
                {
                    for (int i = 0; i < getArticleMaterialResult.ApiResult.ItemList.Count; i++)
                    {
                        WeixinArticleMaterial item = getArticleMaterialResult.ApiResult.ItemList[i];

                        parameterList = new List <CommandParameter>();
                        parameterList.Add(new CommandParameter("@articleMaterial", args.Id));
                        parameterList.Add(new CommandParameter("@index", i));
                        parameterList.Add(new CommandParameter("@url", item.Url));

                        _dataBase.ExecuteNonQuery("UPDATE [ArticleMaterialItem] SET [Url] = @url WHERE [ArticleMaterial] = @articleMaterial AND [Index] = @index",
                                                  parameterList);
                    }

                    _dataBase.ExecuteNonQuery("UPDATE [ArticleMaterial] SET [WeixinStatus] = 2 WHERE [Id] = @articleMaterial",
                                              parameterList);
                }
                else
                {
                    return(getArticleMaterialResult.Message);
                }
            }
            else
            {
                return(addArticleMaterialResult.Message);
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 对于已经提交微信后台的图文素材,只能按篇更新现有文章
        /// </summary>
        /// <param name="domainContext"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public string UpdateArticleMaterialItem(DomainContext domainContext, ArticleMaterialEntity args)
        {
            //受限于微信接口,更新图文素材只能删除原素材重新添加
            //因为重新添加会导致media_id发生变化,所以本地在使用时,要用自己的ID并维护不变
            //但是!如果把微信素材库的素材删除,会导致已经发送给关注者的连接失效,打不开
            //所以这里只删除本地的,微信后台的保持不删除
            //但是这种方式还有一个问题,就是修改是假修改,已经发给用户的素材事实上不会变化
            //而在微信后台修改图文内容是可以实现真修改的。
            //目前只能把本地保存和发布微信后台分开,一旦发布了微信后台,就不再允许添加删除文章

            if (args == null || args.ArticleList == null)
            {
                return("参数错误");
            }

            for (int i = 0; i < args.ArticleList.Count; i++)
            {
                ArticleMaterialItemEntity article = args.ArticleList[i];
                article.Index = i;
                if (article.ImgMappingList != null)
                {
                    article.ImgMapping = JsonHelper.Serializer(article.ImgMappingList);
                }
                _dataBase.Update(article);
            }

            //发布微信后台
            for (int i = 0; i < args.ArticleList.Count; i++)
            {
                ArticleMaterialItemEntity article = args.ArticleList[i];

                //替换其中的图片地址为上传到微信服务器的地址
                if (article.ImgMappingList != null)
                {
                    foreach (var imgMappingItem in article.ImgMappingList)
                    {
                        if (String.IsNullOrEmpty(imgMappingItem.FileUrl) || String.IsNullOrEmpty(imgMappingItem.WeixinUrl))
                        {
                            continue;
                        }

                        article.Content =
                            article.Content.Replace(imgMappingItem.FileUrl, imgMappingItem.WeixinUrl);
                    }
                }

                WeixinUpdateArticleMaterialArgs weixinArgs = new WeixinUpdateArticleMaterialArgs();
                weixinArgs.MediaId = args.MediaId;
                weixinArgs.Index   = i;
                weixinArgs.Article = article;

                RequestApiResult updateArticleMaterialResult =
                    MaterialApiWrapper.UpdateArticleMaterial(domainContext, weixinArgs);

                if (updateArticleMaterialResult.Success == false)
                {
                    return(updateArticleMaterialResult.Message);
                }
            }

            return(null);
        }