public static Dictionary <string, object> AddBaseInfo <T>(Dictionary <string, object> dic, string token, bool isAdd, ref OpertionUser opt)
        {
            var user = UtilConst.GetLoginOpt(token);

            var propertys = typeof(T).GetProperties();

            if (propertys.Count(x => x.Name == "CreateTime") > 0 && isAdd)
            {
                dic.Add("CreateTime", DateTime.Now);;
            }
            if (propertys.Count(x => x.Name == "CreateUserId") > 0 && isAdd)
            {
                dic.Add("CreateUserId", user.UserId);
            }
            if (propertys.Count(x => x.Name == "CreateUserName") > 0 && isAdd)
            {
                dic.Add("CreateUserName", user.UserName);
            }
            if (propertys.Count(x => x.Name == "UpdateUserId") > 0)
            {
                dic.Add("UpdateUserId", user.UserId);
            }
            if (propertys.Count(x => x.Name == "UpdateUserName") > 0)
            {
                dic.Add("UpdateUserName", user.UserName);
            }
            if (propertys.Count(x => x.Name == "UpdateTime") > 0)
            {
                dic.Add("UpdateTime", DateTime.Now);
            }
            opt.UserId       = user.UserId;
            opt.UserName     = user.UserName;
            opt.UserClientIp = GetIP();

            return(dic);
        }
Exemple #2
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
                });
            }
        }