public JsonResponse SendQrcode([FromBody] JsonRequest model) { try { GetQrcodeView view = model.Data?.ToString()?.DeserialObject <GetQrcodeView>(); if (string.IsNullOrEmpty(view.UserAccount) || string.IsNullOrEmpty(view.QrcodeKey)) { return(new JsonResponse { Code = 1, Msg = "参数不合法" }); } var rand = new Random(); string key = $"{view.UserAccount}_{view.QrcodeKey}"; string checkcode = CacheHelper.GetCacheItem(key)?.ToString(); if (!string.IsNullOrEmpty(checkcode)) { return(new JsonResponse { Code = 0, Msg = $"验证码已存在{checkcode}" }); } int qrcode = rand.Next(1000, 9999); CacheHelper.AddCacheItem(key, qrcode.ToString(), DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration, CacheItemPriority.High); return(new JsonResponse { Code = 0, Msg = $"hello 验证码是{qrcode}" }); } catch (Exception ex) { LogHelper.WriteLog(ex, "MemberController/SendQrcode"); return(new JsonResponse { Code = 1, Msg = "程序好像开小差了" + ex.Message }); } }
public HttpResponseMessage GetAuthCode(string authCodeKey) { ValidateCode vCode = new ValidateCode(5) { ImageWidth = 70, ImageHeight = 35 }; string code = string.Empty; if (!string.IsNullOrWhiteSpace(UtilConst.TestCode)) { code = UtilConst.TestCode; } else { code = vCode.CreateValidateCode(); } CacheHelper.AddCacheItem(authCodeKey, code, DateTime.Now.AddMinutes(15), Cache.NoSlidingExpiration, CacheItemPriority.High); byte[] bytes = vCode.CreateValidateGraphic(code); var resp = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(bytes) }; resp.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpg"); return(resp); }
public JsonResponse GetQiNiuUploadToken() { try { string qiniutoken = "QiNiuUploadToken"; string token = CacheHelper.GetCacheItem(qiniutoken)?.ToString(); if (string.IsNullOrWhiteSpace(token)) { Mac mac = new Mac(ConfigUtil.QiNiuAccessKey, ConfigUtil.QiNiuSecretKey); PutPolicy putPolicy = new PutPolicy(); // 设置要上传的目标空间 putPolicy.Scope = ConfigUtil.QiNiuBucket; // 上传策略的过期时间(单位:秒) putPolicy.SetExpires(2 * 60 * 60); // 生成上传token token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString()); CacheHelper.AddCacheItem(qiniutoken, token, DateTime.Now.AddSeconds(2 * 60 * 50), Cache.NoSlidingExpiration, CacheItemPriority.High); } return(new JsonResponse() { Code = 0, Msg = $"请求成功", Data = token }); } catch (Exception ex) { LogHelper.WriteLog(ex, "/Comm/GetQiNiuUploadToken"); return(new JsonResponse() { Code = 1, Msg = $"请求失败:{ex.Message}" }); } }
public JsonResponse GetListBlogTypes([FromBody] PrenextModel model) { try { var dic = new Dictionary <string, object> { { nameof(Category.IsDeleted), 0 }, { nameof(Category.States), 0 } }; string key = ConfigUtil.BlogTypeListCacheKey; List <Category> retlist = new List <Category>(); var cacheobj = CacheHelper.GetCacheItem(key)?.ToString() ?? ""; if (!string.IsNullOrEmpty(cacheobj)) { retlist = CacheHelper.GetCacheItem(key)?.DeserialObjectToList <Category>(); } if (retlist == null || retlist.Count == 0) { retlist = BlogHelper.GetList_Category(dic); CacheHelper.AddCacheItem(key, retlist.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } retlist = retlist?.OrderByDescending(x => x.CreateTime)?.OrderByDescending(x => x.Sort)?.ToList(); return(new JsonResponse { Code = retlist != null ? 0 : 1, Data = retlist }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetListBlogTypes"); return(new JsonResponse { Code = 1, Msg = "程序好像开小差了" + ex.Message }); } }
public JsonResponse GetListBlog([FromBody] BaseViewModel model) { try { var dicwhere = new Dictionary <string, object>() { { nameof(Bloginfo.IsDeleted), 0 }, { nameof(Bloginfo.States), 0 }, }; if (model.KID > 0) { dicwhere.Add(nameof(Bloginfo.Type), model.KID); } string key = ConfigUtil.BlogListCacheKey; var cacheobj = CacheHelper.GetCacheItem(key)?.ToString() ?? ""; List <Bloginfo> alllist = new List <Bloginfo>(); if (!string.IsNullOrEmpty(cacheobj)) { alllist = CacheHelper.GetCacheItem(key)?.DeserialObjectToList <Bloginfo>(); } int cut = 0; if (alllist == null || alllist.Count == 0) { var chaxun = BlogHelper.GetJsonListPage_Bloginfo(1, 1000, "CreateTime desc", new Dictionary <string, object>() { { nameof(Bloginfo.IsDeleted), 0 }, { nameof(Bloginfo.States), 0 } }); if (chaxun.code.Toint() != 0) { return(new JsonResponse { Code = chaxun.code.Toint(), Data = "", Msg = chaxun.msg }); } if (chaxun.data != null && chaxun.data.Count > 0) { alllist = chaxun.data; CacheHelper.AddCacheItem(ConfigUtil.BlogListCacheKey, chaxun.data.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } } List <Bloginfo> retlist = null; if (model.KID > 0) { retlist = alllist.Where(x => x.Type == model.KID)?.ToList(); } else { retlist = alllist; } cut = retlist.Count; retlist = retlist?.Skip((model.Page - 1) * model.Limit).Take(model.Limit).ToList(); #region 统计访问地址信息 Task.Run(() => { if (model.KID <= 0) { var adddic = new Dictionary <string, object>() { { nameof(Access.AccessType), 0 }, { nameof(Access.IpAddress), GetIP() }, { nameof(Access.CreateTime), DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, { nameof(Access.CreateUserId), 1 }, { nameof(Access.CreateUserName), "system" } }; BlogHelper.Add_Access(adddic, new OpertionUser { }); } }); #endregion return(new JsonResponse { Code = 0, Data = retlist, Count = cut }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetListBlog"); return(new JsonResponse { Code = 1, Msg = "程序好像开小差了" + ex.Message }); } }
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 }); } }
public JsonResponse GetItemBlog([FromBody] BaseViewModel model) { try { if (string.IsNullOrEmpty(model.Num)) { return(new JsonResponse { Code = 1, Msg = "参数不合法" }); } string key = $"{CJJ.Blog.Apiv2.Models.ConfigUtil.BlogItemCacheKeyPrefix}{model.Num}"; BloginfoView bloginfoView = CacheHelper.GetCacheItem(key)?.ToString()?.DeserialObject <BloginfoView>(); if (bloginfoView == null) { bloginfoView = BlogHelper.GetModelByNum(model.Num); CacheHelper.AddCacheItem(key, bloginfoView.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } bloginfoView.Views += 1; #region 处理list 和 item 缓存 Task.Run(() => { //CacheHelper.DelCacheItem(key); 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 == model.Num); if (cachelist != null && cachelist.Count > 0 && info != null && info.KID > 0) { cachelist.FirstOrDefault(x => x.BlogNum == model.Num).Views += 1; //CacheHelper.DelCacheItem(alllistkey); CacheHelper.AddCacheItem(alllistkey, cachelist.SerializObject(), DateTime.Now.AddDays(2), Cache.NoSlidingExpiration, CacheItemPriority.High); } }); #endregion #region 异步添加访问次数,统计访问地址信息 Task.Run(() => { try { var dic = new Dictionary <string, object>() { { nameof(Bloginfo.BlogNum), model.Num }, { nameof(Bloginfo.IsDeleted), 0 } }; var bloginfomodel = BlogHelper.GetModelByWhere_Bloginfo(dic); BlogHelper.UpdateNums_Bloginfo(nameof(Bloginfo.Views), 1, dic, new Service.Models.View.OpertionUser()); var adddic = new Dictionary <string, object>() { { nameof(Access.AccessType), 1 }, { nameof(Access.IpAddress), GetIP() }, { nameof(Access.BlogName), bloginfoView.Title }, { nameof(Access.BlogNum), model.Num }, { nameof(Access.CreateTime), DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") }, { nameof(Access.CreateUserId), 1 }, { nameof(Access.CreateUserName), "system" } }; BlogHelper.Add_Access(adddic, new OpertionUser { }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetItemBlog 访问次数"); } }); #endregion return(new JsonResponse { Code = bloginfoView != null ? 0 : 1, Data = bloginfoView != null ? bloginfoView : null }); } catch (Exception ex) { LogHelper.WriteLog(ex, "BlogController/GetListBlog"); return(new JsonResponse { Code = 1, Msg = "程序好像开小差了" + ex.Message }); } }
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 }); } }