/// <summary> /// 点赞数,阅读数写回mysql /// </summary> /// <returns></returns> public async Task WriteBackAsync() { try { var articles = await _articleRepository.Table .Where(item => item.Status == 1) .ToListAsync(); foreach (var article in articles) { var likeKey = $"{ArticleCacheConfig.LIKE_CACHE_KEY}{article.Id}"; var viewKey = $"{ArticleCacheConfig.VIEW_CACHE_KEY}{article.Id}"; await WriteBackLikeCacheValue(article, likeKey); await WriteBackViewCacheValue(article, viewKey); await _work.SaveChangesAsync(); await RedisHelper.DelAsync(likeKey); await RedisHelper.DelAsync(viewKey); } _logger.LogInformation("作业成功"); }catch (Exception ex) { _logger.LogError($"定时写回作业异常,message:{ex.Message}"); } }
/// <summary> /// 查询模块配置 /// </summary> /// <param name="config"></param> /// <returns></returns> public async Task <ApiResult <ModuleConfigResponse> > QueryModuleConfigAsync(QueryModuleConfigRequest request) { var response = new ApiResult <ModuleConfigResponse>(); try { var module = await _moduleRepository.Table .FirstOrDefaultAsync(item => item.ModuleName == request.ModuleName && item.Status == 1); if (module == null) { response.Code = Code.Error; response.Message = "模块不存在"; return(response); } var moduleConfig = await _moduleConfigRepository.TableNotTracking .FirstOrDefaultAsync(item => item.ModuleId == module.Id && item.Status == 1); var moduleConfigResponse = moduleConfig.MapTo <ModuleConfigResponse>(); moduleConfigResponse.ModuleName = module.ModuleName; moduleConfigResponse.ModuleSecret = module.ModuleSecret; moduleConfigResponse.Id = module.Id; module.LastLoadTime = DateTime.Now; module.LastLoadIp = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString(); await _work.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "查询成功"; response.Data = moduleConfigResponse; return(response); } catch (Exception ex) { _logger.LogError($"查询模块配置异常;method={nameof(QueryModuleConfigAsync)};param={request.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"查询模块配置异常:{ex.Message}"; return(response); } }
/// <summary> /// 添加评论 /// </summary> /// <param name="request"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <ApiResult <CommentPageResponse> > ArticleCommentAsync(CommentRequest request, long?userId) { var response = new ApiResult <CommentPageResponse>(); try { var comment = new Comment(true) { ArticleId = request.ArticleId, Content = request.Content, CreateTime = DateTime.Now, Status = 1 }; if (userId.HasValue) { comment.UserId = userId.Value; comment.UserName = (await _userRepository.TableNotTracking.FirstOrDefaultAsync(item => item.Id == userId.Value))?.UserName; } else { comment.UserName = request.UserName; } await IncArticleCommentCountAsync(request.ArticleId); await _commentRepository.InsertAsync(comment); await _efContextWork.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "评论成功"; response.Data = comment.MapTo <CommentPageResponse>(); return(response); } catch (Exception ex) { _logger.LogError($"添加评论异常;method={nameof(ArticleCommentAsync)};param={request.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"添加评论异常:{ex.Message}"; return(response); } }
/// <summary> /// 添加分类 /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <ApiResult> AddCategoryAsync(AddCategoryRequest request, long userId) { var response = new ApiResult(); try { var isExist = await _categoryRepository.TableNotTracking .AnyAsync(item => item.CategoryName == request.CategoryName); if (isExist) { response.Code = Code.Error; response.Message = "该分类名称以存在"; return(response); } var user = await _userRepository.TableNotTracking .FirstOrDefaultAsync(item => item.Id == userId); var category = new Category(true) { CategoryName = request.CategoryName, Status = 1, CreateTime = DateTime.Now, Creator = user.UserName, UserId = userId }; var hasDefault = await _categoryRepository.TableNotTracking .AnyAsync(item => item.Status == 1 && item.UserId == userId && item.IsDefault == 1); if (!hasDefault) { category.IsDefault = 1; } await _categoryRepository.InsertAsync(category); await _efContextWork.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "添加成功"; return(response); } catch (Exception ex) { _logger.LogError($"添加分类异常;method={nameof(AddCategoryAsync)};param={request.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"添加分类异常:{ex.Message}"; return(response); } }
/// <summary> /// 添加模块 /// </summary> /// <param name="request"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <ApiResult> AddModuleAsync(AddModuleRequest request, long userId) { var response = new ApiResult(); try { var isExist = await _moduleRepository.TableNotTracking .AnyAsync(item => item.Status == 1 && item.ModuleName == request.ModuleName); if (isExist) { response.Code = Code.Error; response.Message = "模块已存在"; return(response); } var module = new Module(true) { ModuleName = request.ModuleName, ModuleSecret = request.ModuleSecret, Status = 1, CreateTime = DateTime.Now }; await _moduleRepository.InsertAsync(module); await _work.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "添加成功"; return(response); } catch (Exception ex) { _logger.LogError($"添加模块异常;method={nameof(AddModuleAsync)};param={request.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"添加模块异常:{ex.Message}"; return(response); } }
/// <summary> /// 添加文章 /// </summary> /// <param name="request"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <ApiResult> AddArticleAsync(AddArticleRequest request, long userId) { var response = new ApiResult(); try { var defaultCategoryId = default(long); var user = await _userRepository.TableNotTracking .FirstOrDefaultAsync(item => item.Id == userId && item.Status == 1); if (user == null) { response.Code = Code.Error; response.Message = "查无用户"; return(response); } //有传分类Id直接赋值 if (request.CategoryId.HasValue) { defaultCategoryId = request.CategoryId.Value; } else { //没有传分类Id,获取默认分类。若不存在默认分类请新建 var category = await _categoryRepository.TableNotTracking .FirstOrDefaultAsync(item => item.UserId == userId && item.Status == 1 && item.IsDefault == 1); if (category == null) { var newCategory = new Category(true) { IsDefault = 1, Status = 1, CategoryName = "默认分类", CreateTime = DateTime.Now, UserId = userId, Creator = user.UserName }; await _categoryRepository.InsertAsync(newCategory); category = newCategory; } defaultCategoryId = category.Id; } var article = new Article(true) { Title = request.Title, Describe = request.Describe, CategoryId = defaultCategoryId, Status = 1, CreateTime = DateTime.Now, Creator = user.UserName, UserId = userId }; var detail = new ArticleDetail(true) { ArticleId = article.Id, Content = request.Content, ContentType = request.ContentType, CreateTime = DateTime.Now, Creator = user.UserName, Status = 1 }; await _articleRepository.InsertAsync(article); await _articleDetailRepository.InsertAsync(detail); await _work.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "添加成功"; return(response); } catch (Exception ex) { _logger.LogError($"添加文章异常;method={nameof(AddArticleAsync)};param={request?.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"添加文章异常:{ex.Message}"; return(response); } }
/// <summary> /// 更改密码 /// </summary> /// <param name="request"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <ApiResult> ChangePasswordAsync(ChangePasswordRequest request, long userId) { var response = new ApiResult(); try { var user = await _userRepository.Table .FirstOrDefaultAsync(item => item.Id == userId && item.Status == 1); #region 验证 if (user == null) { response.Code = Code.Error; response.Message = "用户不存在"; return(response); } if (!await VerifyPasswordAsync(userId, request.OldPassword)) { response.Code = Code.Error; response.Message = "密码错误"; return(response); } if (request.Password != request.PasswordConfirm) { response.Code = Code.Error; response.Message = "确认密码错误,请重试"; return(response); } #endregion var userPasswords = await _userPasswordRepository.Table .Where(item => item.UserId == userId) .ToListAsync(); #region 验证是否以前设置过的密码 var newHash = PasswordToHash(request.Password); foreach (var password in userPasswords) { if (password.PasswordHash == newHash) { response.Code = Code.Error; response.Message = "以前已设置过该密码,请设置新的密码"; return(response); } } #endregion var newPassword = new UserPassword(true) { UserId = userId, PasswordHash = newHash, PasswordStrength = request.PasswordStrength, Status = 1, CreateTime = DateTime.Now }; userPasswords.ForEach(item => { item.Status = 0; }); user.UpdateTime = DateTime.Now; await _userPasswordRepository.InsertAsync(newPassword); await _work.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "操作成功"; return(response); } catch (Exception ex) { _logger.LogError($"更改密码异常;method={nameof(ChangePasswordAsync)};param={request.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"更改密码异常:{ex.Message}"; return(response); } }
/// <summary> /// 编辑用户信息 /// </summary> /// <param name="request"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <ApiResult> EditUserInfoAsync(EditUserInfoRequest request, long userId) { var response = new ApiResult(); try { var user = await _userRepository.Table .FirstOrDefaultAsync(item => item.Id == userId && item.Status == 1); if (user == null) { response.Code = Code.Error; response.Message = "找不到用户"; return(response); } //设置用户名 if (!string.IsNullOrEmpty(request.UserName)) { var isExist = await _userRepository.TableNotTracking .AnyAsync(item => item.UserName == request.UserName); if (!isExist) { user.UserName = request.UserName; user.UpdateTime = DateTime.Now; } } //设置昵称 if (!string.IsNullOrEmpty(request.NickName)) { user.NickName = request.NickName; user.UpdateTime = DateTime.Now; } //设置邮箱 if (!string.IsNullOrEmpty(request.Email)) { user.Email = request.Email; user.EmailConfirm = 0; user.UpdateTime = DateTime.Now; } //设置手机 if (!string.IsNullOrEmpty(request.Phone)) { user.Phone = request.Phone; user.PhoneConfirm = 0; user.UpdateTime = DateTime.Now; } //头像 if (!string.IsNullOrEmpty(request.HeadUrl)) { user.HeadUrl = request.HeadUrl; user.UpdateTime = DateTime.Now; } //备注 if (!string.IsNullOrEmpty(request.Remark)) { user.Remark = request.Remark; user.UpdateTime = DateTime.Now; } await _work.SaveChangesAsync(); response.Code = Code.Ok; response.Message = "编辑成功"; return(response); } catch (Exception ex) { _logger.LogError($"编辑用户信息异常;method={nameof(EditUserInfoAsync)};param={request.ToJson()};exception messges={ex.Message}"); response.Code = Code.Error; response.Message = $"编辑用户信息异常:{ex.Message}"; return(response); } }