Sfoglia il codice sorgente

消息系统 消息类型 及分页

leiy 1 anno fa
parent
commit
85b84bb3fa

+ 119 - 1
OASystem/OASystem.Api/Controllers/SystemController.cs

@@ -35,6 +35,10 @@ namespace OASystem.API.Controllers
         private readonly SetDataTypeRepository _setDataTypeRep;
         private readonly UserAuthorityRepository _UserAuthorityRepository;
 
+
+        private readonly List<int> _operationTypeList = new List<int>() { 1, 2, 3, 4, 5 }; //操作通知所属类型
+        private readonly List<int> _taskTypeList = new List<int>() { 6 };//任务通知 TaskNotification
+
         public SystemController( CompanyRepository syscom, DepartmentRepository sysDepRep, UsersRepository userRep,
             IMapper mapper,SqlSugarClient sqlSugar, SetDataRepository setDataRepository, CompanyRepository companyRepository,
             SystemMenuPermissionRepository systemMenuPermissionRepository, PageFunctionPermissionRepository pageFunctionPermissionRepository,
@@ -61,7 +65,7 @@ namespace OASystem.API.Controllers
         #region 消息
 
         /// <summary>
-        /// 获取消息列表
+        /// 获取消息列表-整合版
         /// </summary>
         /// <param name="dto"></param>
         /// <returns></returns>
@@ -79,6 +83,120 @@ namespace OASystem.API.Controllers
             return Ok(JsonView(true,"成功", msgData.Data));
         }
 
+        #region 消息列表 - 分开
+
+        /// <summary>
+        /// 系统消息
+        /// 消息类型  2024-03-06 14:37
+        /// </summary>
+        /// <param name="dto"></param>
+        /// <returns></returns>
+        [HttpPost]
+        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> PotsMsgTypeData(MsgTypeDto dto)
+        {
+            if (dto.PortType < 1 || dto.PortType > 3)
+            {
+                return Ok(JsonView(false, "请输入有效的PortType参数。1 Web 2 Android 3 IOS"));
+            }
+
+            if (dto.UserId < 1)
+            {
+                return Ok(JsonView(false, "请输入有效的UserId参数。"));
+            }
+
+            var msgData = await _messageRep.PotsMsgTypeData(dto);
+
+            if (msgData.Code != 0)
+            {
+                return Ok(JsonView(false, msgData.Msg));
+            }
+
+            return Ok(JsonView(true, "成功", msgData.Data));
+        }
+
+        /// <summary>
+        /// 系统消息
+        /// 消息List  2024-03-06 14:54
+        /// </summary>
+        /// <param name="dto"></param>
+        /// <returns></returns>
+        [HttpPost]
+        [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
+        public async Task<IActionResult> PotsMessagePageList(PotsMessagePageListDto dto)
+        {
+            #region 参数验证
+
+            if (dto.PortType < 1 || dto.PortType > 3)
+            {
+                return Ok(JsonView(false, "请输入有效的PortType参数。1 Web 2 Android 3 IOS"));
+            }
+
+            if (dto.Type < 1 || dto.Type > 2)
+            {
+                return Ok(JsonView(false, "请输入有效的Type参数。1 团组操作通知 2 任务操作通知"));
+            }
+            if (dto.UserId < 1)
+            {
+                return Ok(JsonView(false, "请输入有效的UserId参数。"));
+            }
+
+            #endregion
+
+
+            string msgSqlWhere = string.Format(@"Select sm.Id,sm.Type,sm.Title,sm.Content,sd.DepName issuerDep,su.CnName issuerUser, 
+                                                            sm.ReleaseTime,smra.Id AuthId,smra.ReadableUId,smra.IsRead,smra.ReadTime 
+                                                     From Sys_Message sm 
+                                                     Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
+                                                     Inner Join Sys_Users su On sm.IssuerId = su.Id
+                                                     Inner Join Sys_Department sd On su.DepId = sd.Id
+                                                     Inner Join Sys_Users suAuth On smra.ReadableUId = suAuth.Id
+                                                     Where sm.IsDel = 0
+                                                     And smra.IsDel = 0 
+                                                     And smra.ReadableUId = {0}
+                                                     Order By ReleaseTime Desc ", dto.UserId);
+            var _readableMsgList = await _sqlSugar.SqlQueryable<ReadbleMessageView>(msgSqlWhere).ToListAsync();
+            if (_readableMsgList.Count > 0)
+            {
+                int pageSize = dto.PageSize; // 每页显示的记录数量
+                int currentPage = dto.PageIndex; // 当前页码(从1开始)
+
+                if (dto.Type == 1) //团组操作通知
+                {
+                    //操作通知 OperationNotification
+                    var operationNotificationData = _readableMsgList.Where(it => _operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
+                    int operationNotificationDataCount = operationNotificationData.Count;
+                    // 计算起始索引和结束索引
+                    int operationStartIndex = (currentPage - 1) * pageSize;
+                    int operationEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificationDataCount);
+
+                    var operationNotificationDataView = operationNotificationData.Skip(operationStartIndex).Take(pageSize).ToList();
+
+                    return Ok(JsonView(true, "成功", operationNotificationDataView, operationNotificationDataCount));
+                }
+                else if (dto.Type == 2) //2 任务操作通知
+                {
+
+                    //任务通知 TaskNotification
+                    List<int> taskTypeList = new List<int>() { 6 };
+                    var taskNotificationData = _readableMsgList.Where(it => taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
+                    int taskNotificationDataCount = taskNotificationData.Count;
+                    // 计算起始索引和结束索引
+                    int taskStartIndex = (currentPage - 1) * pageSize;
+                    int taskEndIndex = Math.Min(taskStartIndex + pageSize, taskNotificationDataCount);
+
+                    var taskNotificationDataView = taskNotificationData.Skip(taskStartIndex).Take(pageSize).ToList();
+
+                    return Ok(JsonView(true, "成功", taskNotificationDataView, taskNotificationDataCount));
+                }
+            }
+
+            return Ok(JsonView(false));
+        }
+
+
+        #endregion
+
         /// <summary>
         /// 获取消息详细信息
         /// </summary>

+ 2 - 1
OASystem/OASystem.Api/OAMethodLib/SignalR/Hubs/ChatHub.cs

@@ -13,7 +13,8 @@ using static OASystem.API.OAMethodLib.JWTHelper;
 
 namespace OASystem.API.OAMethodLib.Hub.Hubs
 {
-    [Authorize]
+    //[Authorize]
+    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
     public class ChatHub : Hub<IChatClient>
     {
         private readonly ILogger<ChatHub> _logger;

+ 25 - 5
OASystem/OASystem.Api/Program.cs

@@ -67,6 +67,7 @@ builder.Services.AddCors(policy =>
 
     policy.AddPolicy("Cors", opt => opt
           .SetIsOriginAllowed(origin => true)//这个必须加
+          //.AllowAnyOrigin()
           .AllowAnyHeader()
           .WithMethods("GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS")
           .AllowCredentials());//这个一定不能少);
@@ -231,16 +232,35 @@ builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
             {
                 OnMessageReceived = context =>
                 {
-                    // 当我们收到消息时,去获取请求中的access_token字段
-                    var accessToken = context.Request.Query["access_token"];
-                    // 如果没有就去头上找,找到了就放入我们context.token中
-                    if (!string.IsNullOrEmpty(accessToken))
+                    var path = context.HttpContext.Request.Path;
+                    //如果是signalr请求,需要将token转存,否则JWT获取不到token。OPTIONS请求需要过滤到,因为OPTIONS请求获取不到Token,用NGINX过滤掉OPTION请求.
+                    if (path.StartsWithSegments("/ChatHub"))
                     {
-                        context.Token = accessToken;
+                        string accessToken = context.Request.Query["access_token"].ToString();
+                        if (string.IsNullOrWhiteSpace(accessToken))
+                        {
+                            accessToken = context.Request.Headers["Authorization"].ToString();
+                        }
+                        context.Token = accessToken.Replace("Bearer ", "").Trim();
                     }
                     return Task.CompletedTask;
                 }
             };
+
+            //options.Events = new JwtBearerEvents
+            //{
+            //    OnMessageReceived = context =>
+            //    {
+            //        // 当我们收到消息时,去获取请求中的access_token字段
+            //        var accessToken = context.Request.Query["access_token"];
+            //        // 如果没有就去头上找,找到了就放入我们context.token中
+            //        if (!string.IsNullOrEmpty(accessToken))
+            //        {
+            //            context.Token = accessToken;
+            //        }
+            //        return Task.CompletedTask;
+            //    }
+            //};
         });
 #endregion
 

+ 23 - 0
OASystem/OASystem.Domain/Dtos/System/MsgDto.cs

@@ -18,6 +18,29 @@ namespace OASystem.Domain.Dtos.System
         public int UserId { get; set; }
     }
 
+    /// <summary>
+    /// 消息类型
+    /// 请求dto
+    /// </summary>
+    public class MsgTypeDto : PortDtoBase
+    {
+        /// <summary>
+        /// 用户ID
+        /// </summary>
+        public int UserId { get; set; }
+    }
+
+    public class PotsMessagePageListDto : DtoBase
+    {
+        /// <summary>
+        /// 消息类型
+        /// 1 团组操作通知 2 任务操作通知
+        /// </summary>
+        public int Type { get; set; }
+
+        public int UserId { get; set; }
+    }
+
     /// <summary>
     /// 消息详细信息
     /// 请求dto

+ 14 - 0
OASystem/OASystem.Domain/ViewModels/System/MessageView.cs

@@ -128,4 +128,18 @@ namespace OASystem.Domain.ViewModels.System
         /// </summary>
         public DateTime ReadTime { get; set; }
     }
+
+    public class MessageTypeView 
+    {
+
+        public int Id { get; set; }
+        /// <summary>
+        /// 分类名称
+        /// </summary>
+        public string Name { get; set; }
+        /// <summary>
+        /// 未读条数
+        /// </summary>
+        public int UnReadCount { get; set; } = 0;
+    }
 }

+ 92 - 18
OASystem/OASystem.Infrastructure/Repositories/System/MessageRepository.cs

@@ -1,4 +1,5 @@
-using NPOI.POIFS.Crypt.Dsig;
+using Newtonsoft.Json;
+using NPOI.POIFS.Crypt.Dsig;
 using NPOI.SS.Formula.Functions;
 using OASystem.Domain;
 using OASystem.Domain.Dtos.System;
@@ -15,7 +16,15 @@ namespace OASystem.Infrastructure.Repositories.System
 {
     public class MessageRepository : BaseRepository<Sys_Message, MessageView>
     {
-        public MessageRepository(SqlSugarClient sqlSugar) : base(sqlSugar) { }
+        private readonly SetDataRepository _setData; 
+        private readonly List<int> _operationTypeList = new List<int>() { 1, 2, 3, 4, 5 }; //操作通知所属类型
+        private readonly List<int> _taskTypeList = new List<int>() { 6 };//任务通知 TaskNotification
+
+        public MessageRepository(SqlSugarClient sqlSugar, SetDataRepository setData) 
+            : base(sqlSugar) 
+        { 
+            _setData = setData;
+        }
 
         /// <summary>
         /// 发布消息
@@ -121,36 +130,36 @@ namespace OASystem.Infrastructure.Repositories.System
                     
                     //操作通知 OperationNotification
                     List<int> operationTypeList = new List<int>() {1,2,3,4,5 };
-                    var operationNotificatioData = _readableMsgList.Where(it => operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
-                    int operationNotificatioDataCount = operationNotificatioData.Count;
+                    var operationNotificationData = _readableMsgList.Where(it => operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
+                    int operationNotificationDataCount = operationNotificationData.Count;
                     // 计算起始索引和结束索引
                     int operationStartIndex = (currentPage - 1) * pageSize;
-                    int operationEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificatioDataCount);
+                    int operationEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificationDataCount);
 
-                    var operationNotificatioDataView = new {
-                        Count = operationNotificatioDataCount,
-                        UnReadCount = operationNotificatioData.Where(it => it.IsRead == 0).Count(),
-                        OperationNotificatioData = operationNotificatioData.Skip(operationStartIndex).Take(pageSize).ToList()
+                    var operationNotificationDataView = new {
+                        Count = operationNotificationDataCount,
+                        UnReadCount = operationNotificationData.Where(it => it.IsRead == 0).Count(),
+                        OperationNotificatioData = operationNotificationData.Skip(operationStartIndex).Take(pageSize).ToList()
                     };
 
                     //任务通知 TaskNotification
                     List<int> taskTypeList = new List<int>() { 6 };
-                    var taskNotificatioData = _readableMsgList.Where(it => taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
-                    int taskNotificatioDataCount = taskNotificatioData.Count;
+                    var taskNotificationData = _readableMsgList.Where(it => taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
+                    int taskNotificationDataCount = taskNotificationData.Count;
                     // 计算起始索引和结束索引
                     int taskStartIndex = (currentPage - 1) * pageSize;
-                    int taskEndIndex = Math.Min(operationStartIndex + pageSize, operationNotificatioDataCount);
+                    int taskEndIndex = Math.Min(taskStartIndex + pageSize, taskNotificationDataCount);
 
-                    var taskNotificatioDataView = new
+                    var taskNotificationDataView = new
                     {
-                        Count = taskNotificatioDataCount,
-                        UnReadCount = taskNotificatioData.Where(it => it.IsRead == 0).Count(),
-                        TaskNotificatioData = taskNotificatioData.Skip(taskStartIndex).Take(pageSize).ToList()
+                        Count = taskNotificationDataCount,
+                        UnReadCount = taskNotificationData.Where(it => it.IsRead == 0).Count(),
+                        TaskNotificationData = taskNotificationData.Skip(taskStartIndex).Take(pageSize).ToList()
                     };
 
                     var _view = new {
-                        OperationNotificatio = operationNotificatioDataView,
-                        TaskNotificatio = taskNotificatioDataView
+                        OperationNotification = operationNotificationDataView,
+                        TaskNotification = taskNotificationDataView
                     };
 
                     result.Code = 0;
@@ -166,6 +175,71 @@ namespace OASystem.Infrastructure.Repositories.System
             return result;
         }
 
+
+        /// <summary>
+        /// 获取消息列表
+        /// </summary>
+        /// <param name="uId">可读用户Id</param>
+        /// <returns></returns>
+        public async Task<Result> PotsMsgTypeData(MsgTypeDto dto)
+        {
+            Result result = new Result() { Code = -1, Msg = "未知错误", Data = null };
+
+            if (dto.PortType == 1 || dto.PortType == 2 || dto.PortType == 3)  // web/android
+            {
+
+                var msgTypeResult = await _setData.GetSetDataBySTId(_setData, 77);
+                if (msgTypeResult.Code != 0)
+                {
+                    result.Msg = "消息类型不存在!";
+                    return result;
+                }
+                string msgTypeDataStr = JsonConvert.SerializeObject(msgTypeResult.Data);
+                var msgTypeData = JsonConvert.DeserializeObject<List<MessageTypeView>>(msgTypeDataStr);
+
+                string msgSqlWhere = string.Format(@"Select sm.Id,sm.Type,sm.Title,sm.Content,sd.DepName issuerDep,su.CnName issuerUser, 
+                                                            sm.ReleaseTime,smra.Id AuthId,smra.ReadableUId,smra.IsRead,smra.ReadTime 
+                                                     From Sys_Message sm 
+                                                     Inner Join Sys_MessageReadAuth smra On sm.Id = smra.MsgId
+                                                     Inner Join Sys_Users su On sm.IssuerId = su.Id
+                                                     Inner Join Sys_Department sd On su.DepId = sd.Id
+                                                     Inner Join Sys_Users suAuth On smra.ReadableUId = suAuth.Id
+                                                     Where sm.IsDel = 0
+                                                     And smra.IsDel = 0 
+                                                     And smra.ReadableUId = {0}
+                                                     Order By ReleaseTime Desc ", dto.UserId);
+                var _readableMsgList = await _sqlSugar.SqlQueryable<ReadbleMessageView>(msgSqlWhere).ToListAsync();
+                if (_readableMsgList.Count > 0)
+                {
+                    //操作通知 OperationNotification
+                    var operationNotificationData = _readableMsgList.Where(it => _operationTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
+                    int operationNotificationDataCount = operationNotificationData.Count;
+                   
+                    //任务通知
+                    var taskNotificationData = _readableMsgList.Where(it => _taskTypeList.Contains(it.Type)).OrderBy(it => it.IsRead).ToList();
+                    int taskNotificationDataCount = taskNotificationData.Count;
+
+                    foreach (var item in msgTypeData)
+                    {
+                        //1021	团组操作通知 1020	任务操作通知
+                        if (item.Id == 1020) item.UnReadCount = operationNotificationDataCount;
+                        else if (item.Id == 1021) item.UnReadCount = taskNotificationDataCount;
+                    }
+
+                    result.Code = 0;
+                    result.Msg = "成功!";
+                    result.Data = msgTypeData;
+                }
+                else
+                {
+                    result.Msg = "暂无该用户相关消息!";
+                }
+            }
+
+            return result;
+        }
+
+
         /// <summary>
         /// 获取消息未读消息条数
         /// </summary>