コード例 #1
0
        public JsonResponse AddPraise([FromBody] JsonRequest model)
        {
            var res = new Result {
                IsSucceed = false
            };

            try
            {
                CommentView item = model?.Data.ToString()?.DeserialObject <CommentView>();
                if (item == null || string.IsNullOrEmpty(item.BlogNum))
                {
                    return(new JsonResponse {
                        Code = 1, Msg = "参数不合法"
                    });
                }
                var mem = UtilConst.GetLoginOpt(model.Token);

                var dic = new Dictionary <string, object>
                {
                    { nameof(ArticlePraise.MemberId), mem.UserId },
                    { nameof(ArticlePraise.BlogNum), item.BlogNum },
                    { nameof(ArticlePraise.IsDeleted), 0 }
                };
                var dodic = new Dictionary <string, object>
                {
                    { nameof(ArticlePraise.MemberId), mem.UserId },
                    { nameof(ArticlePraise.BlogNum), item.BlogNum },
                };
                var  ap    = BlogHelper.GetModelByWhere_ArticlePraise(dodic);
                var  opt   = new OpertionUser();
                bool isadd = true;//记录点赞还是取消点赞

                #region 点赞处理
                //取消点赞
                if (ap != null && ap.KID > 0)
                {
                    isadd = false;
                    dodic.Add(nameof(ArticlePraise.IsDeleted), 1);
                    dodic = UtilConst.AddBaseInfo <ArticlePraise>(dodic, model.Token, false, ref opt);
                    res   = BlogHelper.Update_ArticlePraise(dodic, ap.KID, opt);
                }
                //点赞
                else
                {
                    dodic.Add(nameof(ArticlePraise.IpAddress), GetIP());
                    dodic = UtilConst.AddBaseInfo <ArticlePraise>(dodic, model.Token, true, ref opt);
                    res   = BlogHelper.Add_ArticlePraise(dodic, opt);
                }
                #endregion

                #region 处理list 和 item 缓存
                Task.Run(() =>
                {
                    if (res.IsSucceed)
                    {
                        string key = $"{CJJ.Blog.Apiv2.Models.ConfigUtil.BlogItemCacheKeyPrefix}{item.BlogNum}";
                        BloginfoView bloginfoView = CacheHelper.GetCacheItem(key)?.ToString()?.DeserialObject <BloginfoView>();
                        bloginfoView.Start       += isadd ? 1 : -1;
                        CacheHelper.AddCacheItem(key, bloginfoView.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High);
                        string alllistkey         = ConfigUtil.BlogListCacheKey;
                        string allinfo            = CacheHelper.GetCacheItem(alllistkey)?.ToString();
                        List <Bloginfo> cachelist = allinfo?.DeserialObjectToList <Bloginfo>();
                        Bloginfo info             = cachelist.FirstOrDefault(x => x.BlogNum == item.BlogNum);
                        if (cachelist != null && cachelist.Count > 0 && info != null && info.KID > 0)
                        {
                            cachelist.FirstOrDefault(x => x.BlogNum == item.BlogNum).Start += isadd ? 1 : -1;
                            CacheHelper.AddCacheItem(alllistkey, cachelist.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High);
                        }
                    }
                });
                #endregion

                return(new JsonResponse {
                    Code = res.IsSucceed ? 0 : 1, Data = res
                });
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex, "BlogController/AddPraise");
                return(new JsonResponse {
                    Code = 1, Msg = "程序好像开小差了" + ex.Message
                });
            }
        }
コード例 #2
0
        public JsonResponse AddItem(CommentView model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.Token))
                {
                    return(new JsonResponse()
                    {
                        Code = 1, Msg = "请从新登录"
                    });
                }

                var opt     = new OpertionUser();
                var member  = UtilConst.Memberinfo(model.Token);
                var comment = new Dictionary <string, object>()
                {
                    { nameof(Comment.Memberid), member.MemberModel.KID },
                    { nameof(Comment.MemberName), member.MemberModel.UserName },
                    { nameof(Comment.ToMemberid), model.ToMemberid },
                    { nameof(Comment.Content), model.Content },
                    { nameof(Comment.BlogNum), model.BlogNum },
                    { nameof(Comment.Avatar), member.MemberModel.UserIcon },
                };
                var sessionid = model.Commentid;
                if (!string.IsNullOrEmpty(model.Commentid))
                {
                    comment.Add(nameof(Comment.Commentid), model.Commentid);
                }
                else
                {
                    sessionid = Guid.NewGuid().ToString("N");
                    comment.Add(nameof(Comment.Commentid), sessionid);
                }
                var    dic = UtilConst.AddBaseInfo <Comment>(comment, model.Token, true, ref opt);
                Result res = BlogHelper.Add_Comment(dic, opt);

                #region 处理list、item缓存问题

                Task.Run(() =>
                {
                    if (res.IsSucceed && model.ToMemberid.Toint() == 0)
                    {
                        List <Bloginfo> bloglist = CacheHelper.GetCacheItem(ConfigUtil.BlogListCacheKey)?.ToString()?.DeserialObjectToList <Bloginfo>();
                        Bloginfo info            = bloglist.FirstOrDefault(x => x.BlogNum == model.BlogNum);
                        if (bloglist != null && bloglist.Count > 0 && info != null && info.KID > 0)
                        {
                            bloglist.FirstOrDefault(x => x.BlogNum == model.BlogNum).Comments += 1;
                            CacheHelper.AddCacheItem(ConfigUtil.BlogListCacheKey, bloglist.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High);
                        }
                        var blogview = CacheHelper.GetCacheItem($"{ConfigUtil.BlogItemCacheKeyPrefix}{model.BlogNum}")?.DeserialObject <BloginfoView>();
                        if (blogview != null && blogview.KID > 0)
                        {
                            blogview.Comments += 1;
                            CacheHelper.AddCacheItem($"{ConfigUtil.BlogItemCacheKeyPrefix}{model.BlogNum}", blogview.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High);
                        }
                    }
                });

                #endregion

                return(new JsonResponse()
                {
                    Code = res.IsSucceed ? 0 : 1, Data = sessionid
                });
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex, "CommentController/additem");
                return(new JsonResponse()
                {
                    Code = 1, Msg = "程序视乎开小差了" + ex.Message
                });
            }
        }