Exemplo n.º 1
0
        public void Execute(string id)
        {
            if (Broker == null)
            {
                Broker = PersistBrokerFactory.GetPersistBroker();
            }

            Broker.ExecuteTransaction(() =>
            {
                blog data      = Broker.Retrieve <blog>(id);
                var contentUrl = $"{SystemConfig.Config.Protocol}://{SystemConfig.Config.Domain}/#/blog/{id}";
                AssertUtil.CheckIsNullOrEmpty <SpException>(data.surfaceid, "请上传博客封面", "4365FB1F-2EE7-40CF-852C-F6CFA71E8DE2");

                // 如果封面素材未上传则创建封面素材
                string mediaId = Broker.Retrieve <wechat_material>("SELECT * FROM wechat_material WHERE sys_fileid = @id", new Dictionary <string, object>()
                {
                    { "@id", data.surfaceid }
                })?.media_id;
                if (string.IsNullOrEmpty(mediaId))
                {
                    mediaId = new WeChatMaterialService(Broker).CreateData(MaterialType.image, data.big_surfaceid)?.media_id;
                }

                // 未创建图文素材则创建,已创建则更新
                if (string.IsNullOrEmpty(data.wechat_newsid))
                {
                    data.wechat_newsid = new WeChatNewsService(Broker).CreateData(data.title, mediaId, data.createdByName, "", true, data.html_content, contentUrl, true, false);
                }
                else
                {
                    // 如果图文素材已经被删除,则创建
                    var news = Broker.Retrieve <wechat_news>(data.wechat_newsid);
                    if (news == null)
                    {
                        data.wechat_newsid = new WeChatNewsService(Broker).CreateData(data.title, mediaId, data.createdByName, "", true, data.html_content, contentUrl, true, false);
                    }
                    else
                    {
                        news.html_content       = data.html_content;
                        news.thumb_media_id     = mediaId;
                        news.content_source_url = contentUrl;
                        new WeChatNewsService(Broker).UpdateData(news);
                    }
                }

                // 图文素材id回写到博客
                Broker.Update(data);
            });
        }
Exemplo n.º 2
0
        public override void Executing(IJobExecutionContext context)
        {
            var broker = PersistBrokerFactory.GetPersistBroker();

            Logger.Debug("开始同步微信公众号素材");
            var user = broker.Retrieve <user_info>("5B4A52AF-052E-48F0-82BB-108CC834E864");

            try
            {
                var images = new WeChatMaterialService(broker).GetMaterial(MaterialType.image.ToMaterialTypeString(), 1, 5000);
                if (images.item_count == 0)
                {
                    Logger.Debug($"未发现图片待同步");
                }
                else
                {
                    Logger.Debug($"发现共{images.item_count}张图片待同步");
                    images.item.ForEach(item =>
                    {
                        var data = broker.Retrieve <wechat_material>(item.media_id);
                        if (data == null)
                        {
                            var material = new wechat_material()
                            {
                                wechat_materialId = item.media_id,
                                media_id          = item.media_id,
                                url            = item.url,
                                name           = item.name,
                                type           = MaterialType.image.ToMaterialTypeString(),
                                createdBy      = user.Id,
                                createdByName  = user.name,
                                modifiedBy     = user.Id,
                                modifiedByName = user.name,
                                modifiedOn     = DateTime.Now,
                                createdOn      = DateTime.Now
                            };
                            broker.Create(material);
                        }
                        Logger.Debug($"同步图片{item.name}成功");
                    });
                    Logger.Debug($"微信图片素材同步成功,共同步{images.item_count}个");
                }


                var voices = new WeChatMaterialService(broker).GetMaterial(MaterialType.voice.ToMaterialTypeString(), 1, 5000);
                if (voices.item_count == 0)
                {
                    Logger.Debug($"未发现语音待同步");
                }
                else
                {
                    Logger.Debug($"发现共{images.item_count}个语音待同步");
                    voices.item.ForEach(item =>
                    {
                        var data = broker.Retrieve <wechat_material>(item.media_id);
                        if (data == null)
                        {
                            var material = new wechat_material()
                            {
                                wechat_materialId = item.media_id,
                                media_id          = item.media_id,
                                url            = item.url,
                                sys_fileid     = "",
                                name           = item.name,
                                type           = MaterialType.voice.ToMaterialTypeString(),
                                createdBy      = user.Id,
                                createdByName  = user.name,
                                modifiedBy     = user.Id,
                                modifiedByName = user.name,
                                modifiedOn     = DateTime.Now,
                                createdOn      = DateTime.Now
                            };
                            broker.Create(material);
                        }
                        Logger.Debug($"同步语音{item.name}成功");
                    });
                    Logger.Debug($"微信语音素材同步成功,共同步{images.item_count}个");
                }


                var videos = new WeChatMaterialService(broker).GetMaterial(MaterialType.video.ToMaterialTypeString(), 1, 5000);
                if (voices.item_count == 0)
                {
                    Logger.Debug($"未发现视频待同步");
                }
                else
                {
                    Logger.Debug($"发现共{images.item_count}个视频待同步");
                    videos.item.ForEach(item =>
                    {
                        var data = broker.Retrieve <wechat_material>(item.media_id);
                        if (data == null)
                        {
                            var material = new wechat_material()
                            {
                                wechat_materialId = item.media_id,
                                media_id          = item.media_id,
                                url            = item.url,
                                sys_fileid     = "",
                                name           = item.name,
                                type           = MaterialType.video.ToMaterialTypeString(),
                                createdBy      = user.Id,
                                createdByName  = user.name,
                                modifiedBy     = user.Id,
                                modifiedByName = user.name,
                                modifiedOn     = DateTime.Now,
                                createdOn      = DateTime.Now
                            };
                            broker.Create(material);
                            Logger.Debug($"同步视频{item.name}成功");
                        }
                    });
                    Logger.Debug($"微信视频素材同步成功,共同步{videos.item_count}个");
                }
            }
            catch (Exception ex)
            {
                Logger.Debug($"微信素材同步失败");
                Logger.Error($"{ex.Message}\r\n{ex.StackTrace}");
            }
        }