コード例 #1
0
        public async Task <ActionResult> Write(PostInputDto post, DateTime?timespan, bool schedule = false)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!CategoryService.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }

            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }

            if (!post.IsWordDocument)
            {
                post.ResourceName = null;
            }

            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }

            post.Status     = Status.Pended;
            post.PostDate   = DateTime.Now;
            post.ModifyDate = DateTime.Now;
            Post p = post.Mapper <Post>();

            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.IP            = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                tmp.ForEach(s =>
                {
                    var id          = s.ToInt32();
                    Seminar seminar = SeminarService.GetById(id);
                    p.Seminar.Add(new SeminarPost()
                    {
                        Post      = p,
                        PostId    = p.Id,
                        Seminar   = seminar,
                        SeminarId = seminar.Id
                    });
                });
            }

            if (schedule)
            {
                if (timespan.HasValue && timespan.Value > DateTime.Now)
                {
                    p.Status     = Status.Schedule;
                    p.PostDate   = timespan.Value;
                    p.ModifyDate = timespan.Value;
                    HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.PublishPost), args: p);
                    return(ResultData(p.Mapper <PostOutputDto>(), message: $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!"));
                }

                return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
            }

            PostService.AddEntity(p);
            bool b = SearchEngine.SaveChanges() > 0;

            if (!b)
            {
                return(ResultData(null, false, "文章发表失败!"));
            }

            if ("true" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
            {
                return(ResultData(null, true, "文章发表成功!"));
            }
            var    cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
            string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;

            cast.ForEach(c =>
            {
                var ts         = DateTime.Now.GetTotalMilliseconds();
                string content = System.IO.File.ReadAllText(HostEnvironment.WebRootPath + "/template/broadcast.html")
                                 .Replace("{{link}}", link + "?email=" + c.Email)
                                 .Replace("{{time}}", post.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                 .Replace("{{title}}", post.Title).Replace("{{author}}", post.Author)
                                 .Replace("{{content}}", post.Content.RemoveHtmlTag(150))
                                 .Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new
                {
                    c.Email,
                    act      = "cancel",
                    validate = c.ValidateCode,
                    timespan = ts,
                    hash     = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(AppConfig.BaiduAK)
                }, Request.Scheme));
                BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
            });
            return(ResultData(null, true, "文章发表成功!"));
        }
コード例 #2
0
        public async Task <ActionResult> Write(PostCommand post, DateTime?timespan, bool schedule = false)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!CategoryService.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }

            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }

            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }

            post.Status = Status.Published;
            Post p = post.Mapper <Post>();

            p.Modifier      = p.Author;
            p.ModifierEmail = p.Email;
            p.IP            = ClientIP;
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                foreach (var s in tmp)
                {
                    var     id      = s.ToInt32();
                    Seminar seminar = await SeminarService.GetByIdAsync(id);

                    p.Seminar.Add(new SeminarPost()
                    {
                        Post      = p,
                        PostId    = p.Id,
                        Seminar   = seminar,
                        SeminarId = seminar.Id
                    });
                }
            }

            if (schedule)
            {
                if (!timespan.HasValue || timespan.Value <= DateTime.Now)
                {
                    return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
                }

                p.Status     = Status.Schedule;
                p.PostDate   = timespan.Value;
                p.ModifyDate = timespan.Value;
                HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.PublishPost), args: p);
                return(ResultData(p.Mapper <PostDto>(), message: $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!"));
            }

            PostService.AddEntity(p);
            bool b = await SearchEngine.SaveChangesAsync() > 0;

            if (!b)
            {
                return(ResultData(null, false, "文章发表失败!"));
            }

            return(ResultData(null, true, "文章发表成功!"));
        }
コード例 #3
0
        public async Task <ActionResult> Edit(PostInputDto post, bool notify = true, bool reserve = true)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!CategoryService.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }

            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }

            if (!post.IsWordDocument)
            {
                post.ResourceName = null;
            }

            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }

            Post p = PostService.GetById(post.Id);

            if (reserve)
            {
                var history = p.Mapper <PostHistoryVersion>();
                p.PostHistoryVersion.Add(history);
                post.ModifyDate = DateTime.Now;
                var user = HttpContext.Session.Get <UserInfoOutputDto>(SessionKey.UserInfo);
                p.Modifier      = user.NickName;
                p.ModifierEmail = user.Email;
            }

            p.IP = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            Mapper.Map(post, p);
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                p.Seminar.Clear();
                tmp.ForEach(s =>
                {
                    var seminar = SeminarService.Get(e => e.Title.Equals(s));
                    if (seminar != null)
                    {
                        p.Seminar.Add(new SeminarPost()
                        {
                            Post      = p,
                            Seminar   = seminar,
                            PostId    = p.Id,
                            SeminarId = seminar.Id
                        });
                    }
                });
            }

            bool b = SearchEngine.SaveChanges() > 0;

            if (!b)
            {
                return(ResultData(null, false, "文章修改失败!"));
            }

#if !DEBUG
            if (notify && "false" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
            {
                var    cast = BroadcastService.GetQuery(c => c.Status == Status.Subscribed).ToList();
                string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;
                cast.ForEach(c =>
                {
                    var ts         = DateTime.Now.GetTotalMilliseconds();
                    string content = System.IO.File.ReadAllText(Path.Combine(HostEnvironment.WebRootPath, "template", "broadcast.html"))
                                     .Replace("{{link}}", link + "?email=" + c.Email)
                                     .Replace("{{time}}", post.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                     .Replace("{{title}}", post.Title)
                                     .Replace("{{author}}", post.Author)
                                     .Replace("{{content}}", post.Content.RemoveHtmlTag(150))
                                     .Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new
                    {
                        c.Email,
                        act      = "cancel",
                        validate = c.ValidateCode,
                        timespan = ts,
                        hash     = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(AppConfig.BaiduAK)
                    }, Request.Scheme));
                    BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
                });
            }
#endif
            return(ResultData(p.Mapper <PostOutputDto>(), message: "文章修改成功!"));
        }
コード例 #4
0
        public ActionResult Write(PostInputDto post, DateTime?timespan, bool schedule = false)
        {
            post.Content = CommonHelper.ReplaceImgSrc(Regex.Replace(post.Content.Trim(), @"<img\s+[^>]*\s*src\s*=\s*['""]?(\S+\.\w{3,4})['""]?[^/>]*/>", "<img src=\"$1\"/>")).Replace("/thumb150/", "/large/"); //提取img标签,提取src属性并重新创建个只包含src属性的img标签
            if (!CategoryService.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }

            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }

            if (!post.IsWordDocument)
            {
                post.ResourceName = null;
            }

            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }

            post.Status     = Status.Pended;
            post.PostDate   = DateTime.Now;
            post.ModifyDate = DateTime.Now;
            Post p = post.Mapper <Post>();

            p.IP = HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString();
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                tmp.ForEach(s =>
                {
                    var id          = s.ToInt32();
                    Seminar seminar = SeminarService.GetById(id);
                    p.Seminar.Add(new SeminarPost()
                    {
                        Post      = p,
                        PostId    = p.Id,
                        Seminar   = seminar,
                        SeminarId = seminar.Id
                    });
                });
            }

            p.PostAccessRecord.Add(new PostAccessRecord()
            {
                AccessTime = DateTime.Today,
                ClickCount = 0
            });
            if (schedule)
            {
                if (timespan.HasValue && timespan.Value > DateTime.Now)
                {
                    p.Status     = Status.Schedule;
                    p.PostDate   = timespan.Value;
                    p.ModifyDate = timespan.Value;
                    HangfireHelper.CreateJob(typeof(IHangfireBackJob), nameof(HangfireBackJob.PublishPost), args: p);
                    return(ResultData(p.Mapper <PostOutputDto>(), message: schedule ? $"文章于{timespan.Value:yyyy-MM-dd HH:mm:ss}将会自动发表!" : "文章发表成功!"));
                }

                return(ResultData(null, false, "如果要定时发布,请选择正确的一个将来时间点!"));
            }

            bool b = PostService.AddEntitySaved(p) != null;

            if (b)
            {
                if ("false" == CommonHelper.SystemSettings["DisabledEmailBroadcast"])
                {
                    var    cast = BroadcastService.LoadEntities(c => c.Status == Status.Subscribed).ToList();
                    string link = Request.Scheme + "://" + Request.Host + "/" + p.Id;
                    cast.ForEach(c =>
                    {
                        var ts         = DateTime.Now.GetTotalMilliseconds();
                        string content = System.IO.File.ReadAllText(_hostingEnvironment.WebRootPath + "/template/broadcast.html")
                                         .Replace("{{link}}", link + "?email=" + c.Email)
                                         .Replace("{{time}}", post.ModifyDate.ToString("yyyy-MM-dd HH:mm:ss"))
                                         .Replace("{{title}}", post.Title).Replace("{{author}}", post.Author)
                                         .Replace("{{content}}", post.Content.RemoveHtmlTag(150))
                                         .Replace("{{cancel}}", Url.Action("Subscribe", "Subscribe", new
                        {
                            c.Email,
                            act      = "cancel",
                            validate = c.ValidateCode,
                            timespan = ts,
                            hash     = (c.Email + "cancel" + c.ValidateCode + ts).AESEncrypt(AppConfig.BaiduAK)
                        }, Request.Scheme));
                        BackgroundJob.Schedule(() => CommonHelper.SendMail(CommonHelper.SystemSettings["Title"] + "博客有新文章发布了", content, c.Email), (p.ModifyDate - DateTime.Now));
                    });
                }

                return(ResultData(null, true, "文章发表成功!"));
            }

            return(ResultData(null, false, "文章发表失败!"));
        }
コード例 #5
0
        public async Task <ActionResult> Edit(PostCommand post, bool reserve = true)
        {
            post.Content = await ImagebedClient.ReplaceImgSrc(post.Content.Trim().ClearImgAttributes());

            if (!CategoryService.Any(c => c.Id == post.CategoryId && c.Status == Status.Available))
            {
                return(ResultData(null, message: "请选择一个分类"));
            }

            if (string.IsNullOrEmpty(post.Label?.Trim()) || post.Label.Equals("null"))
            {
                post.Label = null;
            }
            else if (post.Label.Trim().Length > 50)
            {
                post.Label = post.Label.Replace(",", ",");
                post.Label = post.Label.Trim().Substring(0, 50);
            }
            else
            {
                post.Label = post.Label.Replace(",", ",");
            }

            if (string.IsNullOrEmpty(post.ProtectContent) || post.ProtectContent.Equals("null", StringComparison.InvariantCultureIgnoreCase))
            {
                post.ProtectContent = null;
            }

            Post p = await PostService.GetByIdAsync(post.Id);

            if (reserve && p.Status == Status.Published)
            {
                var history = p.Mapper <PostHistoryVersion>();
                p.PostHistoryVersion.Add(history);
                p.ModifyDate = DateTime.Now;
                var user = HttpContext.Session.Get <UserInfoDto>(SessionKey.UserInfo);
                p.Modifier      = user.NickName;
                p.ModifierEmail = user.Email;
            }

            p.IP = ClientIP;
            Mapper.Map(post, p);
            if (!string.IsNullOrEmpty(post.Seminars))
            {
                var tmp = post.Seminars.Split(',').Distinct();
                p.Seminar.Clear();
                foreach (var s in tmp)
                {
                    var seminar = await SeminarService.GetAsync(e => e.Title.Equals(s));

                    if (seminar != null)
                    {
                        p.Seminar.Add(new SeminarPost()
                        {
                            Post      = p,
                            Seminar   = seminar,
                            PostId    = p.Id,
                            SeminarId = seminar.Id
                        });
                    }
                }
            }

            bool b = await SearchEngine.SaveChangesAsync() > 0;

            if (!b)
            {
                return(ResultData(null, false, "文章修改失败!"));
            }

            return(ResultData(p.Mapper <PostDto>(), message: "文章修改成功!"));
        }
コード例 #6
0
        public ActionResult GetAll()
        {
            var list = SeminarService.GetAll <string, SeminarDto>(s => s.Title).ToList();

            return(ResultData(list));
        }
コード例 #7
0
        public ActionResult Save(Seminar seminar)
        {
            if (seminar.Id > 0 ? SeminarService.Any(s => s.Id != seminar.Id && s.Title == seminar.Title) : SeminarService.Any(s => s.Title == seminar.Title))
            {
                return(ResultData(null, false, $"{seminar.Title} 已经存在了"));
            }

            var  entry = SeminarService.GetById(seminar.Id);
            bool b;

            if (entry is null)
            {
                b = SeminarService.AddEntitySaved(seminar) != null;
            }
            else
            {
                entry.Description = seminar.Description;
                entry.Title       = seminar.Title;
                entry.SubTitle    = seminar.SubTitle;
                b = SeminarService.SaveChanges() > 0;
            }

            return(ResultData(null, b, b ? "保存成功" : "保存失败"));
        }
コード例 #8
0
        public ActionResult GetPageData([Range(1, int.MaxValue, ErrorMessage = "页码必须大于0")] int page = 1, [Range(1, 50, ErrorMessage = "页大小必须在0到50之间")] int size = 15)
        {
            var list = SeminarService.GetPages <int, SeminarDto>(page, size, s => true, s => s.Id, false);

            return(Ok(list));
        }
コード例 #9
0
        public async Task <ActionResult> Get(int id)
        {
            Seminar seminar = await SeminarService.GetByIdAsync(id);

            return(ResultData(seminar.Mapper <SeminarDto>()));
        }
コード例 #10
0
        public async Task <ActionResult> Delete(int id)
        {
            bool b = await SeminarService.DeleteByIdAsync(id) > 0;

            return(ResultData(null, b, b ? "删除成功" : "删除失败"));
        }
コード例 #11
0
        public ActionResult Get(int id)
        {
            Seminar seminar = SeminarService.GetById(id);

            return(ResultData(seminar.Mapper <SeminarOutputDto>()));
        }
コード例 #12
0
        public ActionResult Delete(int id)
        {
            bool b = SeminarService.DeleteByIdSaved(id);

            return(ResultData(null, b, b ? "删除成功" : "删除失败"));
        }