feat: 支持用户限制

This commit is contained in:
ccnetcore
2025-06-02 02:12:38 +08:00
parent 8b92cd6bed
commit 629add1e8a
6 changed files with 91 additions and 76 deletions

View File

@@ -8,7 +8,7 @@ namespace Yi.Framework.Bbs.Application.Services
{
public class BbsUserInfoService : ApplicationService, IBbsUserInfoService
{
private BbsUserManager _bbsUserManager;
private readonly BbsUserManager _bbsUserManager;
public BbsUserInfoService(BbsUserManager bbsUserManager)
{
_bbsUserManager = bbsUserManager;

View File

@@ -2,8 +2,10 @@
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Uow;
using Volo.Abp.Users;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Argee;
using Yi.Framework.Bbs.Domain.Entities.Forum;
using Yi.Framework.Bbs.Domain.Managers;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Application.Services.Forum
@@ -13,12 +15,13 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
/// </summary>
public class AgreeService : ApplicationService, IApplicationService
{
public AgreeService(ISqlSugarRepository<AgreeEntity> repository, ISqlSugarRepository<DiscussAggregateRoot> discssRepository)
public AgreeService(ISqlSugarRepository<AgreeEntity> repository, ISqlSugarRepository<DiscussAggregateRoot> discssRepository, BbsUserManager bbsUserManager)
{
_repository = repository;
_discssRepository = discssRepository;
_bbsUserManager = bbsUserManager;
}
private readonly BbsUserManager _bbsUserManager;
private ISqlSugarRepository<AgreeEntity> _repository { get; set; }
private ISqlSugarRepository<DiscussAggregateRoot> _discssRepository { get; set; }
@@ -26,17 +29,17 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
/// <summary>
/// 点赞,返回true为点赞+1返回false为点赞-1
/// Todo: 可放入领域层
/// Todo: 可放入领域层,但是没必要,这个项目太简单了
/// </summary>
/// <returns></returns>
[Authorize]
public async Task<AgreeDto> PostOperateAsync(Guid discussId)
{
await _bbsUserManager.VerifyUserLimitAsync(CurrentUser.GetId());
var entity = await _repository.GetFirstAsync(x => x.DiscussId == discussId && x.CreatorId == CurrentUser.Id);
//判断是否已经点赞过
if (entity is null)
{
//没点赞过,添加记录即可,,修改总点赞数量
await _repository.InsertAsync(new AgreeEntity(discussId));
var discussEntity = await _discssRepository.GetByIdAsync(discussId);

View File

@@ -1,28 +1,20 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Article;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Plate;
using Yi.Framework.Bbs.Application.Contracts.IServices;
using Yi.Framework.Bbs.Domain.Entities.Forum;
using Yi.Framework.Bbs.Domain.Managers;
using Yi.Framework.Bbs.Domain.Repositories;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Bbs.Domain.Shared.Model;
using Yi.Framework.Core.Extensions;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Extensions;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Application.Services.Forum
@@ -36,19 +28,16 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
{
public ArticleService(IArticleRepository articleRepository,
ISqlSugarRepository<DiscussAggregateRoot> discussRepository,
IDiscussService discussService,
ForumManager forumManager) : base(articleRepository)
{
_articleRepository = articleRepository;
_discussRepository = discussRepository;
_discussService = discussService;
_forumManager = forumManager;
}
private ForumManager _forumManager;
private IArticleRepository _articleRepository;
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository;
private IDiscussService _discussService;
private readonly ForumManager _forumManager;
private readonly IArticleRepository _articleRepository;
private readonly ISqlSugarRepository<DiscussAggregateRoot> _discussRepository;
public override async Task<PagedResultDto<ArticleGetListOutputDto>> GetListAsync(ArticleGetListInputVo input)
{
@@ -123,7 +112,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
/// <exception cref="UserFriendlyException"></exception>
[Permission("bbs:article:add")]
[Authorize]
public async override Task<ArticleGetOutputDto> CreateAsync(ArticleCreateInputVo input)
public override async Task<ArticleGetOutputDto> CreateAsync(ArticleCreateInputVo input)
{
await VerifyPermissionAsync(input.DiscussId);
return await base.CreateAsync(input);

View File

@@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Users;
using Yi.Framework.Bbs.Application.Contracts.Dtos.BbsUser;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Comment;
using Yi.Framework.Bbs.Application.Contracts.IServices;
@@ -34,14 +35,11 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
_repository = CommentRepository;
_bbsUserManager = bbsUserManager;
}
private ForumManager _forumManager { get; set; }
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository { get; set; }
private IDiscussService _discussService { get; set; }
/// <summary>
/// 获取改主题下的评论,结构为二维列表,该查询无分页
/// </summary>
@@ -127,7 +125,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
{
throw new UserFriendlyException("评论不能为空");
}
await _bbsUserManager.VerifyUserLimitAsync(CurrentUser.GetId());
var discuess = await _discussRepository.GetFirstAsync(x => x.Id == input.DiscussId);
if (discuess is null)
{

View File

@@ -40,10 +40,12 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
private ISqlSugarRepository<AgreeEntity> _agreeRepository;
private BbsUserManager _bbsUserManager;
private IDiscussLableRepository _discussLableRepository;
public DiscussService(BbsUserManager bbsUserManager, ForumManager forumManager,
ISqlSugarRepository<DiscussTopEntity> discussTopRepository,
ISqlSugarRepository<PlateAggregateRoot> plateEntityRepository, ILocalEventBus localEventBus,
ISqlSugarRepository<AgreeEntity> agreeRepository, IDiscussLableRepository discussLableRepository) : base(forumManager._discussRepository)
ISqlSugarRepository<AgreeEntity> agreeRepository, IDiscussLableRepository discussLableRepository) : base(
forumManager._discussRepository)
{
_forumManager = forumManager;
_plateEntityRepository = plateEntityRepository;
@@ -65,7 +67,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async override Task<DiscussGetOutputDto> GetAsync(Guid id)
public override async Task<DiscussGetOutputDto> GetAsync(Guid id)
{
//查询主题发布 浏览主题 事件,浏览数+1
var output = await _forumManager._discussRepository._DbQueryable
@@ -98,7 +100,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
}, true)
.FirstAsync(discuss => discuss.Id == id);
if (output is null)
if (output is null)
{
throw new UserFriendlyException("该主题不存在", "404");
}
@@ -108,7 +110,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
case DiscussTypeEnum.Article: break;
//查询的是悬赏主题
case DiscussTypeEnum.Reward:
var reward= await _forumManager._discussRewardRepository.GetAsync(x=>x.DiscussId==output.Id);
var reward = await _forumManager._discussRewardRepository.GetAsync(x => x.DiscussId == output.Id);
output.RewardData = reward.Adapt<DiscussRewardGetOutputDto>();
break;
}
@@ -116,7 +118,8 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
//组装点赞
var agreeCreatorList =
(await _agreeRepository._DbQueryable.Where(x => x.DiscussId == output.Id).Select(x=>x.CreatorId).ToListAsync());
(await _agreeRepository._DbQueryable.Where(x => x.DiscussId == output.Id).Select(x => x.CreatorId)
.ToListAsync());
//已登录
if (CurrentUser.Id is not null)
{
@@ -124,17 +127,17 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
}
//组装标签
var lableDic=await _discussLableRepository.GetDiscussLableCacheMapAsync();
var lableDic = await _discussLableRepository.GetDiscussLableCacheMapAsync();
foreach (var lableId in output.DiscussLableIds)
{
if (lableDic.TryGetValue(lableId,out var item))
if (lableDic.TryGetValue(lableId, out var item))
{
output.Lables.Add(item.Adapt<DiscussLableGetOutputDto>());
}
}
//如果没有权限
if (!await _forumManager.VerifyDiscussPermissionAsync(output.Id,CurrentUser.Id, CurrentUser.Roles))
if (!await _forumManager.VerifyDiscussPermissionAsync(output.Id, CurrentUser.Id, CurrentUser.Roles))
{
output.SetNoPermission();
}
@@ -147,6 +150,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
{ DiscussId = output.Id, OldSeeNum = output.SeeNum });
return output;
}
/// <summary>
/// 查询
/// </summary>
@@ -170,7 +174,8 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
// .OrderByIF(input.Type == QueryDiscussTypeEnum.New,
// @"COALESCE(discuss.LastModificationTime, discuss.CreationTime) DESC")
//采用上方写法
.OrderByIF(input.Type == QueryDiscussTypeEnum.New,discuss=>SqlFunc.Coalesce(discuss.LastModificationTime,discuss.CreationTime),OrderByType.Desc)
.OrderByIF(input.Type == QueryDiscussTypeEnum.New, discuss => discuss.CreationTime, OrderByType.Desc)
// .OrderByIF(input.Type == QueryDiscussTypeEnum.New,discuss=>SqlFunc.Coalesce(discuss.LastModificationTime,discuss.CreationTime),OrderByType.Desc)
.OrderByIF(input.Type == QueryDiscussTypeEnum.Host, discuss => discuss.SeeNum, OrderByType.Desc)
.OrderByIF(input.Type == QueryDiscussTypeEnum.Suggest, discuss => discuss.AgreeNum, OrderByType.Desc)
.Select((discuss, user, info) => new DiscussGetListOutputDto
@@ -199,8 +204,8 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
.GroupBy(x => x.DiscussId)
.ToDictionary(x => x.Key, y => y.Select(y => y.CreatorId).ToList());
var levelCacheDic= await _bbsUserManager.GetLevelCacheMapAsync();
var lableDic=await _discussLableRepository.GetDiscussLableCacheMapAsync();
var levelCacheDic = await _bbsUserManager.GetLevelCacheMapAsync();
var lableDic = await _discussLableRepository.GetDiscussLableCacheMapAsync();
//组装等级、是否点赞赋值、标签
items?.ForEach(x =>
@@ -209,7 +214,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
if (CurrentUser.Id is not null)
{
//默认fasle
if (agreeDic.TryGetValue(x.Id,out var userIds))
if (agreeDic.TryGetValue(x.Id, out var userIds))
{
x.IsAgree = userIds.Contains(CurrentUser.Id);
}
@@ -217,12 +222,11 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
foreach (var lableId in x.DiscussLableIds)
{
if (lableDic.TryGetValue(lableId,out var item))
if (lableDic.TryGetValue(lableId, out var item))
{
x.Lables.Add(item.Adapt<DiscussLableGetOutputDto>());
}
}
});
return new PagedResultDto<DiscussGetListOutputDto>(total, items);
}
@@ -264,15 +268,15 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
}
}, true)
.ToListAsync();
var levelCacheDic= await _bbsUserManager.GetLevelCacheMapAsync();
var lableDic=await _discussLableRepository.GetDiscussLableCacheMapAsync();
var levelCacheDic = await _bbsUserManager.GetLevelCacheMapAsync();
var lableDic = await _discussLableRepository.GetDiscussLableCacheMapAsync();
output?.ForEach(x =>
{
x.User.LevelName = levelCacheDic[x.User.Level].Name;
foreach (var lableId in x.DiscussLableIds)
{
if (lableDic.TryGetValue(lableId,out var item))
if (lableDic.TryGetValue(lableId, out var item))
{
x.Lables.Add(item.Adapt<DiscussLableGetOutputDto>());
}
@@ -311,7 +315,9 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
}
}
var entity = await _forumManager.CreateDiscussAsync(await MapToEntityAsync(input),input.RewardData.Adapt<DiscussRewardAggregateRoot>());
await _bbsUserManager.VerifyUserLimitAsync(CurrentUser.GetId());
var entity = await _forumManager.CreateDiscussAsync(await MapToEntityAsync(input),
input.RewardData.Adapt<DiscussRewardAggregateRoot>());
return await MapToGetOutputDtoAsync(entity);
}
@@ -322,22 +328,17 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
/// <exception cref="UserFriendlyException"></exception>
[HttpPut("discuss/reward/resolve/{discussId}")]
[Authorize]
public async Task SetRewardResolvedAsync([FromRoute]Guid discussId)
public async Task SetRewardResolvedAsync([FromRoute] Guid discussId)
{
var reward= await _forumManager._discussRewardRepository.GetFirstAsync(x=>x.DiscussId==discussId);
if (reward is null)
{
throw new UserFriendlyException("未找到该悬赏主题","404");
}
var reward = await _forumManager._discussRewardRepository.GetFirstAsync(x => x.DiscussId == discussId);
if (reward is null)
{
throw new UserFriendlyException("未找到该悬赏主题", "404");
}
//设置已解决
reward.SetResolved();
await _forumManager._discussRewardRepository.UpdateAsync(reward);
}
public override Task<DiscussGetOutputDto> UpdateAsync(Guid id, DiscussUpdateInput input)
{
return base.UpdateAsync(id, input);
//设置已解决
reward.SetResolved();
await _forumManager._discussRewardRepository.UpdateAsync(reward);
}
}
}

View File

@@ -13,25 +13,49 @@ namespace Yi.Framework.Bbs.Domain.Managers
public class BbsUserManager : DomainService
{
public ISqlSugarRepository<UserAggregateRoot> _userRepository;
public ISqlSugarRepository<BbsUserExtraInfoEntity> _bbsUserInfoRepository;
// public Dictionary<int, LevelCacheItem> _levelCacheDic;
private LevelManager _levelManager;
private readonly LevelManager _levelManager;
public BbsUserManager(ISqlSugarRepository<UserAggregateRoot> userRepository,
ISqlSugarRepository<BbsUserExtraInfoEntity> bbsUserInfoRepository,
LevelManager levelManager
)
{
_userRepository = userRepository;
_bbsUserInfoRepository = bbsUserInfoRepository;
_levelManager = levelManager;
}
/// <summary>
/// 校验用户限制
/// </summary>
/// <param name="userId"></param>
/// <exception cref="UserFriendlyException"></exception>
public async Task VerifyUserLimitAsync(Guid userId)
{
var userInfo = await GetBbsUserInfoAsync(userId);
if (userInfo.UserLimit == UserLimitEnum.Ban)
{
throw new UserFriendlyException("你已被禁用,如存疑虑,请联系管理员进行申诉");
}
if (userInfo.UserLimit == UserLimitEnum.Dangerous)
{
throw new UserFriendlyException("您的账号被标记为危险状态,请遵规守法,等待后续解除");
}
}
/// <summary>
/// 获取等级关系
/// </summary>
/// <returns></returns>
public async Task<Dictionary<int, LevelCacheItem>> GetLevelCacheMapAsync()
{
return await _levelManager.GetCacheMapAsync();
}
/// <summary>
/// 获取bbs用户信息
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public async Task<BbsUserInfoDto?> GetBbsUserInfoAsync(Guid userId)
{
var userInfo = await _userRepository._DbQueryable
@@ -50,7 +74,7 @@ namespace Yi.Framework.Bbs.Domain.Managers
}, true)
.FirstAsync(user => user.Id == userId);
var levelCacheDic= await GetLevelCacheMapAsync();
var levelCacheDic = await GetLevelCacheMapAsync();
userInfo.LevelName = levelCacheDic[userInfo.Level].Name;
return userInfo;
}
@@ -73,8 +97,8 @@ namespace Yi.Framework.Bbs.Domain.Managers
DiscussNumber = info.DiscussNumber
}, true)
.ToListAsync();
var levelCacheDic= await GetLevelCacheMapAsync();
userInfos?.ForEach(userInfo => userInfo.LevelName =levelCacheDic[userInfo.Level].Name);
var levelCacheDic = await GetLevelCacheMapAsync();
userInfos?.ForEach(userInfo => userInfo.LevelName = levelCacheDic[userInfo.Level].Name);
return userInfos ?? new List<BbsUserInfoDto>();
}