示例#1
0
        public WxJsonResult UploadNews(Wx_App app, string newsIds, int openComment = 1)
        {
            DateTime now  = DateTime.Now;
            var      news = GetNews(newsIds).Select(n =>
            {
                GetThumbMedia(app, n);

                return(new NewsModel
                {
                    title = n.Title,
                    author = n.Author,
                    content = GetWxHtml(app, n),
                    content_source_url = App.GetWebPath(n.SourceUrl, true),
                    digest = n.Digest,
                    show_cover_pic = "0",
                    thumb_media_id = n.ThumbMediaId,
                    need_open_comment = openComment
                });
            }).ToArray();

            var result = MediaApi.UploadNews(app.AppId, 300000, news);

            if (result.ErrorCodeValue == 0)
            {
                UploadNews(app.Id, newsIds, news.Length, result.media_id, result.url);
            }
            return(result);
        }
示例#2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                string           accessToken = Wx.accessToken;
                List <NewsModel> News        = new List <NewsModel>();
                int count;
                int.TryParse(Request.Form["lineCount"], out count);
                string Author   = "计算机应用技术2班20150301241梁阔雄";
                string AuthorID = Request.Form["AuthorID"];
                for (int i = 0; i < count; ++i)
                {
                    var file = Request.Files["Img" + i.ToString()];
                    if (file != null && !string.IsNullOrEmpty(file.FileName))
                    {
                        var r = Media2.UploadForeverMedia(Wx.accessToken, file);
                        News.Add(new NewsModel
                        {
                            title              = Request.Form["Title" + i.ToString()],
                            content            = Request.Form["Content" + i.ToString()],
                            content_source_url = Request.Form["Url" + i.ToString()],
                            author             = Author,
                            thumb_media_id     = r.media_id
                        });
                    }
                }

                if (News.Count > 0)
                {
                    var r1 = MediaApi.UploadNews(Wx.accessToken, News.ToArray());
                    var r2 = GroupMessageApi.SendGroupMessagePreview(Wx.accessToken, GroupMessageType.mpnews, r1.media_id, AuthorID);
                    ClientScript.RegisterStartupScript(GetType(), "1", "alert('成功发送消息');", true);
                }
            }
        }
示例#3
0
        private async Task <string> AddFileToWx(MpMediaArticleDto article, int mpid, string mediaID)
        {
            if (article == null)
            {
                return(mediaID);
            }
            var account = _accountRepository.FirstOrDefault(m => m.Id == mpid);

            var access_token = await _accessTokenContainer.TryGetAccessTokenAsync(account.AppId, account.AppSecret);

            //var access_token = Senparc.Weixin.MP.Containers.AccessTokenContainer.TryGetAccessToken(account.AppId, account.AppSecret);

            var news = new NewsModel()
            {
                title                 = article.Title,
                author                = article.Author,
                digest                = article.Description,
                content               = article.WxContent,
                content_source_url    = article.AUrl,
                show_cover_pic        = article.ShowPic,
                thumb_media_id        = article.PicMediaID,
                need_open_comment     = article.EnableComment,
                only_fans_can_comment = article.OnlyFansComment
            };

            return(MediaApi.UploadNews(access_token, Senparc.Weixin.Config.TIME_OUT, news).media_id);
        }
示例#4
0
        // POST: api/News
        public async Task <IHttpActionResult> Post([FromBody] List <PostNewsViewModel> news)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var newsList = new List <NewsModel>();

            foreach (var model in news)
            {
                var newsModel = new NewsModel
                {
                    author             = model.Author,
                    content            = model.Text,
                    content_source_url = model.OriginalLink,
                    digest             = model.Summary,
                    show_cover_pic     = model.IsShowInText ? "1" : "0",
                    title          = model.Title,
                    thumb_media_id = model.ThumbMediaId
                };
                newsList.Add(newsModel);
            }
            var result = MediaApi.UploadNews(AccessToken, Config.TIME_OUT, newsList.ToArray());

            if (result.errcode != ReturnCode.请求成功)
            {
                return(BadRequest(result.errmsg));
            }
            return(Ok(news));
        }
示例#5
0
        /// <summary>
        /// 新增永久图文素材
        /// </summary>
        /// <param name="keyValue">NewsId</param>
        public UploadForeverMediaResult UploadForeverNews(string keyValue)
        {
            long   id            = Convert.ToInt64(keyValue);
            string appId         = WxOperatorProvider.Provider.GetCurrent().AppId;
            var    specification = new Specification <WxNews>().FetchStrategy.Include(p => p.WxNewsItems.Select(e => e.Thumb));
            WxNews wxNews        = wxNewsRepository.Get(id, specification);

            NewsModel[] newsModel = new NewsModel[wxNews.WxNewsItems.Count()];
            foreach (WxNewsItem wxNewsItem in wxNews.WxNewsItems)
            {
                int index = wxNews.WxNewsItems.IndexOf(wxNewsItem);
                newsModel[index]                       = new NewsModel();
                newsModel[index].title                 = wxNewsItem.Title;
                newsModel[index].author                = wxNewsItem.Author;
                newsModel[index].content               = wxNewsItem.Content;
                newsModel[index].content_source_url    = wxNewsItem.ContentSourceUrl;
                newsModel[index].digest                = wxNewsItem.Digest;
                newsModel[index].need_open_comment     = wxNewsItem.NeedOpenComment;
                newsModel[index].only_fans_can_comment = wxNewsItem.OnlyFansCanComment;
                newsModel[index].show_cover_pic        = wxNewsItem.ShowCoverPic.ToString();
                newsModel[index].thumb_media_id        = wxNewsItem.Thumb.MediaId;
                newsModel[index].thumb_url             = wxNewsItem.Thumb.MediaUrl;
            }
            var uploadForeverMediaResult = MediaApi.UploadNews(appId, 10000, newsModel);

            if (uploadForeverMediaResult.ErrorCodeValue == 0)
            {
                wxNews.MediaId = uploadForeverMediaResult.media_id;
                wxNewsRepository.Update(wxNews);
            }
            return(uploadForeverMediaResult);
        }
        private void SaveNewsToWeChat(Guid newsId)
        {
            var news =
                db.Site_News.Include(w => w.Articles)
                //.Include("Articles.Article")
                //.Include("Articles.FrontCoverImage")
                .FirstOrDefault(p => p.Id == newsId);

            //为了兼容MySQL
            foreach (var item in news.Articles)
            {
                item.Article         = db.Site_Articles.Find(item.SiteArticleId);
                item.FrontCoverImage = db.Site_Images.Find(item.SiteImageId);
            }
            var newsList = new List <NewsModel>();

            foreach (var model in news.Articles)
            {
                var newsModel = new NewsModel
                {
                    author             = model.Article.Author,
                    content            = model.Article.Content,
                    content_source_url = model.Article.OriginalUrl,
                    digest             = model.Article.Summary,
                    show_cover_pic     = model.IsShowInText ? "1" : "0",
                    title          = model.Article.Name,
                    thumb_media_id = model.FrontCoverImage.MediaId
                };
                newsList.Add(newsModel);
            }
            if (string.IsNullOrWhiteSpace(news.MediaId))
            {
                var result = MediaApi.UploadNews(AccessToken, Config.TIME_OUT, newsList.ToArray());
                news.MediaId            = result.media_id;
                news.Url                = result.url;
                news.FrontCoverImageUrl = news.Articles.First().FrontCoverImage.SiteUrl;
                db.SaveChanges();
            }
            else
            {
                MediaApi.DeleteForeverMedia(AccessToken, news.MediaId);
                var result = MediaApi.UploadNews(AccessToken, Config.TIME_OUT, newsList.ToArray());
                news.MediaId            = result.media_id;
                news.Url                = result.url;
                news.FrontCoverImageUrl = news.Articles.First().FrontCoverImage.SiteUrl;
                db.SaveChanges();
                //TODO:受微信接口限制,无法实现新增图文,故删除再建
                //var index = 0;
                //foreach (var item in newsList)
                //{
                //    var result = MediaApi.UpdateForeverNews(AccessToken, news.MediaId, index, item);
                //    index++;
                //}
            }
        }
示例#7
0
        public void AfterDeleteNewsTest()
        {
            var accessToken = AccessTokenContainer.GetAccessToken(_appId);

            var file   = @"E:\1.jpg";
            var result = MediaApi.UploadForeverMedia(accessToken, file);

            Assert.IsNotNull(result.media_id);

            var new1 = new NewsModel()
            {
                author             = "test",
                content            = "test",
                content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
                digest             = "test",
                show_cover_pic     = "1",
                thumb_media_id     = result.media_id,
                title = "test"
            };

            var new2 = new NewsModel()
            {
                author             = "test",
                content            = "test111",
                content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
                digest             = "test",
                show_cover_pic     = "1",
                thumb_media_id     = result.media_id,
                title = "test"
            };

            var result1 = MediaApi.UploadNews(accessToken, 10000, new1, new2);

            Assert.IsNotNull(result1.media_id);

            var clientMsgId = SystemTime.Now.Ticks.ToString();

            GroupMessageApi.SendGroupMessageByOpenId(accessToken, GroupMessageType.mpnews, result1.media_id, clientMsgId, 10000, "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg", "o3IHxjrPzMVZIJOgYMH1PyoTW_Tg");
            //var result2 = MediaApi.UpdateForeverNews(accessToken, result1.media_id, 0, 10000, new2);

            MediaApi.DeleteForeverMedia(accessToken, result1.media_id);
            //Assert.AreEqual(result2.errcode, ReturnCode.请求成功);
        }
示例#8
0
        //[TestMethod]
        private string UploadAndUpdateNewsTest(string accessToken)
        {
            var file   = @"E:\1.jpg";
            var result = MediaApi.UploadForeverMedia(accessToken, file);

            Assert.IsNotNull(result.media_id);

            var new1 = new NewsModel()
            {
                author             = "test",
                content            = "test",
                content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
                digest             = "test",
                show_cover_pic     = "1",
                thumb_media_id     = result.media_id,
                title = "test"
            };

            var new2 = new NewsModel()
            {
                author             = "test",
                content            = "test111",
                content_source_url = "http://qy.weiweihi.com/Content/Images/app/qyhelper.png",
                digest             = "test",
                show_cover_pic     = "1",
                thumb_media_id     = result.media_id,
                title = "test"
            };

            var result1 = MediaApi.UploadNews(accessToken, 10000, new1, new2);

            Assert.IsNotNull(result1.media_id);

            //var result2 = MediaApi.UpdateForeverNews(accessToken, result1.media_id, 0, 10000, new2);

            //Assert.AreEqual(result2.errcode, ReturnCode.请求成功);

            return(result1.media_id);
        }
示例#9
0
        private void SendGroupMessage(string tempStr, string accessToken, MediaList_OthersResult imgResult, string strAction)
        {
            string sendCountTxt = Server.MapPath("sendcount.txt");
            var    sendCount    = int.Parse(File.ReadAllText(sendCountTxt));

            NewsModel[] newsList   = new NewsModel[sendCount];
            var         dataSevice = new DataAccessSerive();
            var         jobList    = dataSevice.GetTopJobInfoList();

            if (jobList.Count > 0)
            {
                List <Article> articles = new List <Article>();
                int            i        = 0;
                string         imgName;
                foreach (var job in jobList)
                {
                    if (i == sendCount)
                    {
                        break;
                    }
                    imgName = "send" + i.ToString() + ".jpg";
                    string imgUrl    = string.Format("{0}/image/{1}", WebConfigurationManager.AppSettings["domain"], imgName);
                    var    jobDetail = dataSevice.GetJobDetail(job.JobId);
                    var    news      = new NewsModel();
                    news.author  = "";
                    news.content = tempStr.Replace("[ComName]", job.ComName)
                                   .Replace("[ComBrief]", jobDetail.ComBrief)
                                   .Replace("[DetailPalce]", jobDetail.DetailPalce)
                                   .Replace("[LinkMan]", jobDetail.LinkMan)
                                   .Replace("[Phone]", jobDetail.Phone)
                                   .Replace("[JobName]", jobDetail.JobName)
                                   .Replace("[JobType]", jobDetail.JobType)
                                   .Replace("[LowMoney]", jobDetail.LowMoney)
                                   .Replace("[HrNum]", jobDetail.HrNum)
                                   .Replace("[Edu]", jobDetail.Edu)
                                   .Replace("[RegisterDate]", jobDetail.RegisterDate)
                                   .Replace("[EffectDate]", jobDetail.EffectDate)
                                   .Replace("[Other]", jobDetail.Other)
                                   .Replace("[Sex]", jobDetail.Sex)
                    ;
                    //news.content_source_url = WebConfigurationManager.AppSettings["domain"] + "/html/detail.html?id=" + job.JobId;
                    news.content_source_url = WebConfigurationManager.AppSettings["website"];
                    news.digest             = job.ComName + "诚聘" + job.JobName;
                    news.show_cover_pic     = "0";
                    news.thumb_media_id     = imgResult.item.Find(p => p.name == imgName).media_id;
                    news.title  = job.ComName + "诚聘" + job.JobName;
                    newsList[i] = news;
                    i++;
                }
                UploadForeverMediaResult mediaResult = MediaApi.UploadNews(accessToken, 100000, newsList);
                try
                {
                    GroupMessageApi.SendGroupMessageByGroupId
                        (accessToken, "-1", mediaResult.media_id, GroupMessageType.mpnews, true);
                    txtResult.Text += "提交成功一次" + strAction + " 推送成功";
                }
                catch (Exception ex)
                {
                    txtResult.Text += "提交成功一次订" + strAction + " 推送失败 " + ex.Message;
                }
            }
        }
        public void PushNews(string accessToken, string webPath, string tempStr)
        {
            OpenIdResultJson json = UserApi.Get(accessToken, "");
            var imgResult         = MediaApi.GetOthersMediaList(accessToken, UploadMediaFileType.image, 0, 10000);
            var DircetPath        = Environment.CurrentDirectory + "/image/";

            if (!Directory.Exists(DircetPath))
            {
                Directory.CreateDirectory(DircetPath);
            }

            for (int i = 0; i < 6; i++)
            {
                string imgName = string.Format("send{0}.jpg", i);
                if (imgResult.item.Find(p => p.name == imgName) == null)
                {
                    var imgPath  = webPath + "/image/" + imgName;
                    var filePath = DircetPath + "/image/" + imgName;
                    CommonUtility.HttpUtility.DownloadFile(imgPath, filePath);
                    var mediaId = MediaApi.UploadForeverMedia(accessToken, filePath).media_id;
                }
            }
            imgResult = MediaApi.GetOthersMediaList(accessToken, UploadMediaFileType.image, 0, 10000);


            string sendCountTxtUrl = webPath + "/" + "sendcount.txt";
            var    sendCountTxt    = CommonUtility.HttpUtility.Get(sendCountTxtUrl);
            var    sendCount       = int.Parse(sendCountTxt);

            NewsModel[] newsList = new NewsModel[sendCount];

            var dataSevice = new DataAccessSerive();
            var jobList    = dataSevice.GetTopJobInfoList();

            if (jobList.Count > 0)
            {
                List <Article> articles = new List <Article>();
                int            i        = 0;
                string         imgName;
                foreach (var job in jobList)
                {
                    if (i == sendCount)
                    {
                        break;
                    }
                    string domain  = ConfigurationManager.AppSettings.Get("domain");
                    string website = ConfigurationManager.AppSettings.Get("website");
                    imgName = "send" + i.ToString() + ".jpg";
                    string imgUrl    = string.Format("{0}/image/{1}", domain, imgName);
                    var    jobDetail = dataSevice.GetJobDetail(job.JobId);
                    var    news      = new NewsModel()
                    {
                        author  = "",
                        content = tempStr.Replace("[ComName]", job.ComName)
                                  .Replace("[ComBrief]", jobDetail.ComBrief)
                                  .Replace("[DetailPalce]", jobDetail.DetailPalce)
                                  .Replace("[LinkMan]", jobDetail.LinkMan)
                                  .Replace("[Phone]", jobDetail.Phone)
                                  .Replace("[JobName]", jobDetail.JobName)
                                  .Replace("[JobType]", jobDetail.JobType)
                                  .Replace("[LowMoney]", jobDetail.LowMoney)
                                  .Replace("[HrNum]", jobDetail.HrNum)
                                  .Replace("[Edu]", jobDetail.Edu)
                                  .Replace("[RegisterDate]", jobDetail.RegisterDate)
                                  .Replace("[EffectDate]", jobDetail.EffectDate)
                                  .Replace("[Other]", jobDetail.Other)
                                  .Replace("[Sex]", jobDetail.Sex)
                        ,
                        //content_source_url = domain + "/html/detail.html?id=" + job.JobId,
                        content_source_url = website,
                        digest             = job.ComName + "诚聘" + job.JobName,
                        show_cover_pic     = "0",
                        thumb_media_id     = imgResult.item.Find(p => p.name == imgName).media_id,
                        title = job.ComName + "诚聘" + job.JobName
                    };
                    newsList[i] = news;
                    i++;
                }
                UploadForeverMediaResult mediaResult = MediaApi.UploadNews(accessToken, 100000, newsList);
                try
                {
                    if (isDebug != "1")
                    {
                        GroupMessageApi.SendGroupMessageByGroupId(accessToken, "-1", mediaResult.media_id, GroupMessageType.mpnews, true);
                    }
                    Console.WriteLine("素材提交推送  推送成功");
                    logger.Info(DateTime.Now.ToString() + " 素材提交推送  推送成功");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("素材提交推送  推送失败 " + ex.Message);
                    var MailService = new MailSendFunc();
                    MailService.SendMail("*****@*****.**", ex.Message, "素材提交推送异常", mailCfg);
                    logger.ErrorException(DateTime.Now.ToString() + "素材提交推送失败 " + ex.Message, ex);
                }
            }
        }