From 2863559ba06d17b876923c478b06a67d1b517e41 Mon Sep 17 00:00:00 2001 From: zpc Date: Thu, 11 Sep 2025 01:47:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../User/ICoreCmsUserBlacklistRepository.cs | 6 +- .../User/ICoreCmsUserRepository.cs | 2 + .../User/ICoreCmsUserBlacklistServices.cs | 23 +++++- .../User/ICoreCmsUserServices.cs | 2 + .../ExecutionTimeMiddleware.cs | 30 ++++++++ CoreCms.Net.Middlewares/MiddlewareHelpers.cs | 10 +++ .../Entities/SQ/SQReservations.cs | 10 --- .../SQ/SQReservationParticipantsDto.cs | 65 +++++++++++++++++ .../ViewModels/SQ/SQReservationsDto.cs | 6 ++ .../User/CoreCmsUserBlacklistRepository.cs | 1 + .../User/CoreCmsUserBlacklistServices.cs | 70 ++++++++++++++++++- .../Controllers/SQController.cs | 68 +++++++++++++----- CoreCms.Net.Web.WebApi/Doc.xml | 2 +- CoreCms.Net.Web.WebApi/Program.cs | 10 +++ 14 files changed, 270 insertions(+), 35 deletions(-) create mode 100644 CoreCms.Net.Middlewares/ExecutionTimeMiddleware.cs diff --git a/CoreCms.Net.IRepository/User/ICoreCmsUserBlacklistRepository.cs b/CoreCms.Net.IRepository/User/ICoreCmsUserBlacklistRepository.cs index b0fc35d..784fdb3 100644 --- a/CoreCms.Net.IRepository/User/ICoreCmsUserBlacklistRepository.cs +++ b/CoreCms.Net.IRepository/User/ICoreCmsUserBlacklistRepository.cs @@ -12,15 +12,17 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; + using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.ViewModels.Basics; using CoreCms.Net.Model.ViewModels.UI; + using SqlSugar; namespace CoreCms.Net.IRepository { - /// + /// /// 工厂接口 /// public interface ICoreCmsUserBlacklistRepository : IBaseRepository @@ -99,5 +101,7 @@ namespace CoreCms.Net.IRepository Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false); + + } } diff --git a/CoreCms.Net.IRepository/User/ICoreCmsUserRepository.cs b/CoreCms.Net.IRepository/User/ICoreCmsUserRepository.cs index 7249859..6c30152 100644 --- a/CoreCms.Net.IRepository/User/ICoreCmsUserRepository.cs +++ b/CoreCms.Net.IRepository/User/ICoreCmsUserRepository.cs @@ -59,5 +59,7 @@ namespace CoreCms.Net.IRepository /// /// Task> StatisticsOrder(int day); + + } } \ No newline at end of file diff --git a/CoreCms.Net.IServices/User/ICoreCmsUserBlacklistServices.cs b/CoreCms.Net.IServices/User/ICoreCmsUserBlacklistServices.cs index 8148590..13577df 100644 --- a/CoreCms.Net.IServices/User/ICoreCmsUserBlacklistServices.cs +++ b/CoreCms.Net.IServices/User/ICoreCmsUserBlacklistServices.cs @@ -12,14 +12,16 @@ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; + using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.ViewModels.Basics; using CoreCms.Net.Model.ViewModels.UI; + using SqlSugar; namespace CoreCms.Net.IServices { - /// + /// /// 服务工厂接口 /// public interface ICoreCmsUserBlacklistServices : IBaseServices @@ -63,7 +65,7 @@ namespace CoreCms.Net.IServices #endregion - + #region 获取缓存的所有数据========================================================== /// @@ -95,5 +97,22 @@ namespace CoreCms.Net.IServices Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false); #endregion + + + /// + /// 获取用户拉黑名单 + /// + /// + /// + Task> GetUserBlacklists(int userId); + + /// + /// 用户拉黑某人 + /// + /// + /// + /// + /// + Task UserBlackUser(int userId, int toUserId, int type); } } diff --git a/CoreCms.Net.IServices/User/ICoreCmsUserServices.cs b/CoreCms.Net.IServices/User/ICoreCmsUserServices.cs index 40f2cbb..e5e5497 100644 --- a/CoreCms.Net.IServices/User/ICoreCmsUserServices.cs +++ b/CoreCms.Net.IServices/User/ICoreCmsUserServices.cs @@ -131,5 +131,7 @@ namespace CoreCms.Net.IServices /// /// Task> StatisticsOrder(int day); + + } } \ No newline at end of file diff --git a/CoreCms.Net.Middlewares/ExecutionTimeMiddleware.cs b/CoreCms.Net.Middlewares/ExecutionTimeMiddleware.cs new file mode 100644 index 0000000..5b1289a --- /dev/null +++ b/CoreCms.Net.Middlewares/ExecutionTimeMiddleware.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Http; + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CoreCms.Net.Middlewares; + +/// +/// 方法执行时间 +/// +public class ExecutionTimeMiddleware(RequestDelegate _next) +{ + public async Task Invoke(HttpContext context) + { + // 开始计时 + var sw = Stopwatch.StartNew(); + //在将响应标头发送到之前添加要调用的委托客户此处注册的回调按相反顺序运行。 + context.Response.OnStarting(() => + { + sw.Stop(); + context.Response.Headers.TryAdd("X-Request-Duration", $"{sw.Elapsed.TotalMilliseconds} ms"); + return Task.CompletedTask; + }); + await _next(context); + } +} diff --git a/CoreCms.Net.Middlewares/MiddlewareHelpers.cs b/CoreCms.Net.Middlewares/MiddlewareHelpers.cs index 1856667..dc848c3 100644 --- a/CoreCms.Net.Middlewares/MiddlewareHelpers.cs +++ b/CoreCms.Net.Middlewares/MiddlewareHelpers.cs @@ -11,6 +11,7 @@ ***********************************************************************/ using CoreCms.Net.Core; + using Microsoft.AspNetCore.Builder; namespace CoreCms.Net.Middlewares @@ -62,6 +63,15 @@ namespace CoreCms.Net.Middlewares return app.UseMiddleware(); } + /// + /// 接口执行时间中间件 + /// + /// + /// + public static IApplicationBuilder UseExecutionTimeMiddleware(this IApplicationBuilder app) + { + return app.UseMiddleware(); + } /// /// 用户访问接口日志中间件 diff --git a/CoreCms.Net.Model/Entities/SQ/SQReservations.cs b/CoreCms.Net.Model/Entities/SQ/SQReservations.cs index ae9f726..de2c8fa 100644 --- a/CoreCms.Net.Model/Entities/SQ/SQReservations.cs +++ b/CoreCms.Net.Model/Entities/SQ/SQReservations.cs @@ -210,11 +210,6 @@ namespace CoreCms.Net.Model.Entities /// 最大年龄限制,0=不限 /// [Display(Name = "最大年龄限制,0=不限")] - - - - - public System.Int32? max_age { get; set; } @@ -222,11 +217,6 @@ namespace CoreCms.Net.Model.Entities /// 鸽子费(保证金) /// [Display(Name = "鸽子费(保证金)")] - - - - - public System.Decimal? deposit_fee { get; set; } diff --git a/CoreCms.Net.Model/ViewModels/SQ/SQReservationParticipantsDto.cs b/CoreCms.Net.Model/ViewModels/SQ/SQReservationParticipantsDto.cs index 00e996a..c5e71f6 100644 --- a/CoreCms.Net.Model/ViewModels/SQ/SQReservationParticipantsDto.cs +++ b/CoreCms.Net.Model/ViewModels/SQ/SQReservationParticipantsDto.cs @@ -2,8 +2,11 @@ using CoreCms.Net.Model.Entities; +using SqlSugar; + using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -26,5 +29,67 @@ namespace CoreCms.Net.Model.ViewModels.SQ /// 用户头像 /// public string AvatarImage { get; set; } + + /// + /// 用户是否拉黑 0否 1是 + /// + public int UserBlackStatus { get; set; } + } + + [AutoMap(typeof(SQReservationParticipants))] + public class SQReservationParticipantsApiDto + { + + /// + /// 参与记录ID + /// + public System.Int32 id { get; set; } + + + /// + /// 预约ID + /// + public System.Int32 reservation_id { get; set; } + + + /// + /// 参与者ID(关联用户表,暂时占位) + /// + public System.Int32 user_id { get; set; } + + + /// + /// 角色:0=参与者,1=发起者 + /// + [Display(Name = "角色:0=参与者,1=发起者")] + public System.Int32 role { get; set; } + + + /// + /// 加入时间 + /// + [Display(Name = "加入时间")] + public System.DateTime join_time { get; set; } + + /// + /// 状态,0正常,1已退出 + /// + [Display(Name = "状态,0正常,1已退出")] + public System.Int32 status { get; set; } + + /// + /// 用户昵称 + /// + public string UserName { get; set; } + + /// + /// 用户头像 + /// + public string AvatarImage { get; set; } + + /// + /// 用户是否拉黑 0否 1是 + /// + public int UserBlackStatus { get; set; } } } diff --git a/CoreCms.Net.Model/ViewModels/SQ/SQReservationsDto.cs b/CoreCms.Net.Model/ViewModels/SQ/SQReservationsDto.cs index 50d2abc..de3da48 100644 --- a/CoreCms.Net.Model/ViewModels/SQ/SQReservationsDto.cs +++ b/CoreCms.Net.Model/ViewModels/SQ/SQReservationsDto.cs @@ -16,6 +16,12 @@ namespace CoreCms.Net.Model.ViewModels.SQ { public List Participants { get; set; } } + + [AutoMap(typeof(SQReservations))] + public class SQReservationsApiDto : SQReservations + { + public List Participants { get; set; } + } /// /// /// diff --git a/CoreCms.Net.Repository/User/CoreCmsUserBlacklistRepository.cs b/CoreCms.Net.Repository/User/CoreCmsUserBlacklistRepository.cs index 206708e..f3f5937 100644 --- a/CoreCms.Net.Repository/User/CoreCmsUserBlacklistRepository.cs +++ b/CoreCms.Net.Repository/User/CoreCmsUserBlacklistRepository.cs @@ -229,6 +229,7 @@ namespace CoreCms.Net.Repository var list = new PageList(page, pageIndex, pageSize, totalCount); return list; } + #endregion diff --git a/CoreCms.Net.Services/User/CoreCmsUserBlacklistServices.cs b/CoreCms.Net.Services/User/CoreCmsUserBlacklistServices.cs index e3eea4e..19d6b35 100644 --- a/CoreCms.Net.Services/User/CoreCmsUserBlacklistServices.cs +++ b/CoreCms.Net.Services/User/CoreCmsUserBlacklistServices.cs @@ -10,8 +10,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; + +using CoreCms.Net.Caching.AutoMate.RedisCache; using CoreCms.Net.Configuration; using CoreCms.Net.IRepository; using CoreCms.Net.IRepository.UnitOfWork; @@ -19,6 +22,7 @@ using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.ViewModels.Basics; using CoreCms.Net.Model.ViewModels.UI; + using SqlSugar; @@ -31,12 +35,15 @@ namespace CoreCms.Net.Services { private readonly ICoreCmsUserBlacklistRepository _dal; private readonly IUnitOfWork _unitOfWork; + private readonly IRedisOperationRepository _redisOperationRepository; - public CoreCmsUserBlacklistServices(IUnitOfWork unitOfWork, ICoreCmsUserBlacklistRepository dal) + public CoreCmsUserBlacklistServices(IUnitOfWork unitOfWork, ICoreCmsUserBlacklistRepository dal, + IRedisOperationRepository redisOperationRepository) { this._dal = dal; base.BaseDal = dal; _unitOfWork = unitOfWork; + _redisOperationRepository = redisOperationRepository; } #region 实现重写增删改查操作========================================================== @@ -91,6 +98,9 @@ namespace CoreCms.Net.Services return await _dal.DeleteByIdsAsync(ids); } + + + #endregion #region 获取缓存的所有数据========================================================== @@ -114,7 +124,7 @@ namespace CoreCms.Net.Services #endregion - #region 重写根据条件查询分页数据 + #region 重写根据条件查询分页数据 /// /// 重写根据条件查询分页数据 /// @@ -131,6 +141,62 @@ namespace CoreCms.Net.Services { return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock); } + + public async Task> GetUserBlacklists(int userId) + { + if (userId == 0) + { + return new List(); + } + string key = $"user:black:{userId}"; + var list = await _redisOperationRepository.Get>(key); + if (list == null) + { + + var userList = await _dal.QueryListByClauseAsync(it => it.UserId == userId); + if (userList.Count == 0) + { + list = new List(); + } + else + { + list = userList.Select(it => it.BlockedUserId).ToList(); + } + await _redisOperationRepository.Set(key, list, TimeSpan.FromMinutes(60)); + } + return list; + } + + public async Task UserBlackUser(int userId, int toUserId, int type) + { + if (userId == 0) + { + return false; + } + + if (type == 0) + { + CoreCmsUserBlacklist coreCmsUserBlacklist = new CoreCmsUserBlacklist() + { + UserId = userId, + CreatedTime = DateTime.Now, + BlockedUserId = toUserId, + Reason = "", + }; + await _dal.InsertAsync(coreCmsUserBlacklist); + } + else + { + var bluser = await _dal.QueryByClauseAsync(it => it.UserId == userId && it.BlockedUserId == toUserId); + if (bluser != null) + { + await _dal.DeleteByIdAsync(bluser.Id); + } + } + string key = $"user:black:{userId}"; + await _redisOperationRepository.Remove(key); + return true; + } #endregion } diff --git a/CoreCms.Net.Web.WebApi/Controllers/SQController.cs b/CoreCms.Net.Web.WebApi/Controllers/SQController.cs index 0f81185..6a170f4 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/SQController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/SQController.cs @@ -39,6 +39,7 @@ public class SQController : ControllerBase private readonly ICoreCmsUserServices _userServices; private readonly IHttpContextUser _user; private readonly SqlSugarScope _dbBase; + private readonly ICoreCmsUserBlacklistServices _coreCmsUserBlacklistServices; /// /// 构造函数 @@ -53,6 +54,7 @@ public class SQController : ControllerBase , ICoreCmsUserServices userServices , IHttpContextUser user , IUnitOfWork unitOfWork + , ICoreCmsUserBlacklistServices coreCmsUserBlacklistServices ) { _webHostEnvironment = webHostEnvironment; @@ -65,6 +67,7 @@ public class SQController : ControllerBase _userServices = userServices; _user = user; _dbBase = unitOfWork.GetDbClient(); + _coreCmsUserBlacklistServices = coreCmsUserBlacklistServices; } /// @@ -95,7 +98,7 @@ public class SQController : ControllerBase /// /// [HttpGet] - public async Task GetReservationList([FromQuery] int pageIndex, [FromQuery] int pageSize = 20) + public async Task GetReservationList([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 20) { var userId = _user.ID; var now = DateTime.Now; @@ -104,36 +107,63 @@ public class SQController : ControllerBase where = where.And(it => it.end_time > now); where = where.And(it => it.status < 2); var list = await _SQReservationsServices.QueryPageAsync(where, it => it.start_time, OrderByType.Asc, pageIndex, pageSize, true); - var pageList = _mapper.Map>(list); + var pageList = _mapper.Map>(list); var rIds = list.Select(it => it.id).ToList(); if (rIds != null && rIds.Count > 0) { - var participants = await _SQReservationParticipantsServices.QueryListByClauseAsync(it => rIds.Contains(it.reservation_id), "", true); - List userList = new List(); + string sqlWhere = string.Join(",", rIds); + var participants = await _dbBase.Ado.SqlQueryAsync($"select p.id,p.reservation_id,p.status,p.join_time,p.user_id, p.role,u.nickName UserName,u.avatarImage AvatarImage from SQReservationParticipants p inner join CoreCmsUser u on p.user_id=u.id where p.status=0 and p.reservation_id in ({sqlWhere})"); if (participants != null && participants.Count > 0) { - var userIds = participants.Select(it => it.user_id).ToList(); - userList = await _userServices.QueryListByClauseAsync(it => userIds.Contains(it.id), "", true); - } - foreach (var item in pageList) - { - var temp = participants.Where(it => it.reservation_id == item.id).OrderBy(it => it.role).ThenBy(it => it.status).ToList(); - var dto = _mapper.Map>(temp); - if (userList != null && userList.Count > 0) + var userBlacklist = new List(); + //如果用户登录了 + if (userId > 0) { - foreach (var p in dto) + + userBlacklist = await _coreCmsUserBlacklistServices.GetUserBlacklists(userId); + //并且用户有黑名单数据 + if (userBlacklist.Count > 0) { - var u = userList.FirstOrDefault(it => it.id == p.user_id); - if (u != null) + //查找并修改黑名单用户 + foreach (var participant in participants.Where(it => userBlacklist.Contains(it.user_id))) { - p.UserName = u.nickName; - p.AvatarImage = u.avatarImage; + participant.UserBlackStatus = 1; } } - } - item.Participants = dto; + foreach (var item in pageList) + { + var temp = participants.Where(it => it.reservation_id == item.id).OrderBy(it => it.role).ThenBy(it => it.status).ToList(); + var dto = _mapper.Map>(temp); + item.Participants = dto; + } } + //var participants = await _SQReservationParticipantsServices.QueryListByClauseAsync(it => rIds.Contains(it.reservation_id), "", true); + //List userList = new List(); + //if (participants != null && participants.Count > 0) + //{ + // var userIds = participants.Select(it => it.user_id).ToList(); + // userList = await _userServices.QueryListByClauseAsync(it => userIds.Contains(it.id), "", true); + //} + //foreach (var item in pageList) + //{ + // var temp = participants.Where(it => it.reservation_id == item.id).OrderBy(it => it.role).ThenBy(it => it.status).ToList(); + // var dto = _mapper.Map>(temp); + // if (userList != null && userList.Count > 0) + // { + // foreach (var p in dto) + // { + // var u = userList.FirstOrDefault(it => it.id == p.user_id); + // if (u != null) + // { + // p.UserName = u.nickName; + // p.AvatarImage = u.avatarImage; + // } + // } + + // } + // item.Participants = dto; + //} } return new WebApiDto() { diff --git a/CoreCms.Net.Web.WebApi/Doc.xml b/CoreCms.Net.Web.WebApi/Doc.xml index 01fde95..3c3f97b 100644 --- a/CoreCms.Net.Web.WebApi/Doc.xml +++ b/CoreCms.Net.Web.WebApi/Doc.xml @@ -762,7 +762,7 @@ 预约接口 - + 构造函数 diff --git a/CoreCms.Net.Web.WebApi/Program.cs b/CoreCms.Net.Web.WebApi/Program.cs index 6c3f734..638197b 100644 --- a/CoreCms.Net.Web.WebApi/Program.cs +++ b/CoreCms.Net.Web.WebApi/Program.cs @@ -1,7 +1,9 @@ using System; using System.Linq; + using Autofac; using Autofac.Extensions.DependencyInjection; + using CoreCms.Net.Auth; using CoreCms.Net.Configuration; using CoreCms.Net.Core.AutoFac; @@ -15,11 +17,15 @@ using CoreCms.Net.Swagger; using CoreCms.Net.Task; using CoreCms.Net.Web.WebApi.Infrastructure; using CoreCms.Net.WeChat.Service.Mediator; + using Essensoft.Paylink.Alipay; using Essensoft.Paylink.WeChatPay; + using Hangfire; using Hangfire.Dashboard.BasicAuthorization; + using MediatR; + using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpOverrides; @@ -30,8 +36,10 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; + using Newtonsoft.Json; using Newtonsoft.Json.Serialization; + using NLog.Web; var builder = WebApplication.CreateBuilder(args); @@ -151,6 +159,8 @@ app.UseRequestResponseLog(); app.UseRecordAccessLogsMildd(); // ¼ip (ע⿪ȨޣȻ޷д) app.UseIpLogMildd(); +// ¼ʱ +app.UseExecutionTimeMiddleware(); // SwaggerȨ¼ app.UseSwaggerAuthorizedMildd(); //ǿʾ