Esempio n. 1
0
        public JsonResult ReplyComment(int Id)
        {
            string artId         = Request["artId"];
            string editedContent = Request["replyinput"];

            Comment model = new Comment()
            {
                CmtArtId  = Convert.ToInt32(artId),
                ParentId  = Id,
                VisitorId = 197,  //博主我本身
                CmtText   = editedContent,
                Status    = 1,
                SubTime   = DateTime.Now
            };

            int val = commentService.Add(model);

            return(Json(val));
        }
Esempio n. 2
0
        public ActionResult PostCmt(Comment cmt)
        {
            //先判断该文章是否被删除或是否是被禁用的( artService.GetEntity()== null)。
            var list = articleService.GetDataListBy(a => a.Id == cmt.CmtArtId && a.Status == 1);

            if (list != null && list.Count != 0)
            {
                #region 0.0 用于发邮件时记录锚点
                string pageSize = Request["PageSize"];
                #endregion

                //当每次进入Archives/Index时,就根据cookie或者session将用户信息绑定到评论框上的input标签中
                //然后,提交的时候,这儿就获取标签里面的游客信息
                string visitorId    = Request["VId"];   //要么为0要么为其他数值
                string visitorName  = Request["VName"]; //此处trim会出错:因为前台将这俩框设置为disabled了,值不会传过来
                string visitorEmail = Request["VEmail"];

                JsonModel jsonData;

                #region 1.0 如果能取到隐藏域中的visitorId,证明游客存在
                if (!String.IsNullOrEmpty(visitorId))
                {
                    //1.0 执行 model验证 并 插入库中
                    //CmtArtId ParentId和CmtText已进行模型绑定
                    cmt.VisitorId = int.Parse(visitorId);
                    cmt.CmtText   = StringHelper.ClearHtml(cmt.CmtText);
                    cmt.Status    = 1;
                    cmt.SubTime   = DateTime.Now;

                    int val = commentService.Add(cmt);//入库
                    if (val == 1)
                    {
                        int vid = int.Parse(visitorId);
                        //构造json数据
                        jsonData = new JsonModel()
                        {
                            //注意:linq中不能用C#的方法,它不识别,所以先把int.Parse(visitorId);提出来
                            CoreData = GenerateOneComment(visitorService.GetDataListBy(v => v.Id == vid)[0], cmt),
                            Status   = 1,
                            Message  = visitorId,
                        };

                        SendBlogCmtEmail(cmt, pageSize);
                    }
                    else
                    {
                        jsonData = new JsonModel()
                        {
                            CoreData = "",
                            Status   = 0,
                            Message  = "评论失败,请重试",
                            GotoUrl  = Request.UrlReferrer.AbsoluteUri  //获取上一级url 即Archives/Index/博文id
                        };
                    }

                    SetVisitorCookie(visitorId);

                    return(Json(jsonData));
                }
                #endregion
                #region 2.0 如果取不到隐藏域中的访客信息,证明是新游客
                else
                {
                    //如果隐藏域中没有visitorId,证明是新游客来访。visitorName、visitorEmail已在前台验证
                    //将新游客和游客的评论入库
                    //检验visitorName是否重复
                    visitorName  = visitorName.Trim();
                    visitorEmail = visitorEmail.Trim();
                    List <Visitor> checkVisitor = visitorService.GetDataListBy(v => v.VisitorName == visitorName);
                    //如果这个昵称已被注册了,就在check邮箱是否一样,if不一样,就认为是新用户;如果一样,就认为是同一个访客
                    if (checkVisitor != null && checkVisitor.Count != 0)
                    {
                        //如果昵称一样、邮箱也一样,证明是老访客重新登录了(可能是老用户换了浏览器,或cookie过期了,需要重新"登录")
                        Visitor vi = checkVisitor.Find(c => c.VisitorEmail == visitorEmail);
                        if (vi != null)
                        {
                            cmt.VisitorId = vi.Id;
                            cmt.CmtText   = StringHelper.ClearHtml(cmt.CmtText);
                            cmt.Status    = 1;
                            cmt.SubTime   = DateTime.Now;
                            int val = commentService.Add(cmt);//入库
                            if (val == 1)
                            {
                                //构造json数据
                                jsonData = new JsonModel()
                                {
                                    CoreData = GenerateOneComment(visitorService.GetDataListBy(v => v.Id == vi.Id)[0], cmt),
                                    Status   = 1,
                                    Message  = vi.Id.ToString(),
                                };

                                //发送邮件
                                SendBlogCmtEmail(cmt, pageSize);
                            }
                            else
                            {
                                jsonData = new JsonModel()
                                {
                                    CoreData = "",
                                    Status   = 0,
                                    Message  = "评论失败,请重试",
                                    GotoUrl  = Request.UrlReferrer.AbsoluteUri  //获取上一级url 即Archives/Index/博文id
                                };
                            }
                            //向客户端写cookie
                            SetVisitorCookie(vi.Id.ToString());
                        }
                        else
                        {
                            //如果昵称一样,邮箱不一样,则认为是新注册用户,这是提示它昵称已被抢注,请换个昵称
                            jsonData = new JsonModel()
                            {
                                CoreData = cmt.CmtText,
                                Status   = 2,
                                Message  = "该昵称已被占用,再起个吧~"
                            };
                        }
                    }
                    else
                    {
                        //新访客
                        //用户头像,从头像表中随机抽取一个
                        int             iconId   = 0;
                        List <HeadIcon> headList = headService.GetDataListBy(h => h.Status == 1);
                        Random          rom      = new Random();
                        if (headList.Count != 0)
                        {
                            iconId = headList[rom.Next(headList.Count)].Id;
                        }

                        Visitor newVisitor = new Visitor()
                        {
                            VisitorName   = visitorName,
                            VisitorEmail  = visitorEmail,
                            VisitorQQ     = "",
                            VisitorIconId = iconId,  //随机选一个用户头像
                            Status        = 1,
                            SubTime       = DateTime.Now
                        };

                        cmt.CmtText = cmt.CmtText = StringHelper.ClearHtml(cmt.CmtText);;
                        cmt.Status  = 1;
                        cmt.SubTime = DateTime.Now;

                        newVisitor.Comments.Add(cmt);

                        //根据导航属性同时向数据库插入两条数据
                        int val = visitorService.Add(newVisitor);

                        if (val == 2)
                        {
                            jsonData = new JsonModel()
                            {
                                CoreData = GenerateOneComment(newVisitor, cmt),
                                Status   = 1,
                                Message  = (newVisitor.Id).ToString(),
                            };

                            //向客户端写cookie
                            SetVisitorCookie((newVisitor.Id).ToString());
                            //向博主或其他游客发邮件
                            SendBlogCmtEmail(cmt, pageSize);
                        }
                        else
                        {
                            jsonData = new JsonModel()
                            {
                                Status  = 0,
                                Message = "评论失败,请刷新后重试",
                                GotoUrl = Request.UrlReferrer.AbsoluteUri   //获取上一级url 即Archives/Index/博文id
                            };
                        }
                    }

                    return(Json(jsonData));
                }

                #endregion
            }
            else
            {
                JsonModel jsonData = new JsonModel()
                {
                    Status  = 3,
                    Message = "该文章已被删除,暂时不能进行评论、点赞等操作",
                    GotoUrl = "http://127.0.0.1:8081/error404.html"
                };
                return(Json(jsonData));
            }
        }