Browse Source

Signalr 6

leiy 1 year ago
parent
commit
c34baeefa1

+ 25 - 0
OASystem/OASystem.Api/Controllers/ClientHubController.cs

@@ -1,6 +1,7 @@
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.SignalR;
+using OASystem.API.OAMethodLib;
 using OASystem.API.OAMethodLib.Hub.HubClients;
 using OASystem.API.OAMethodLib.Hub.Hubs;
 using OASystem.API.OAMethodLib.SignalR.Hubs;
@@ -72,5 +73,29 @@ namespace OASystem.API.Controllers
             await _hubContext.Clients.Client(connId).ReceiveMessage(date);
             return Ok("Send Successful!");
         }
+
+        /// <summary>
+        /// 消息 发布And 通知
+        /// </summary>
+        /// <param name="msgTypeEnum"></param>
+        /// <param name="title"></param>
+        /// <param name="content"></param>
+        /// <param name="userIds"></param>
+        /// <returns></returns>
+        [HttpPost("MessageIssuserAndNotification", Name = "MessageIssuserAndNotification")]
+        public async Task<IActionResult> MessageIssuserAndNotification(MessgeTypeEnum msgTypeEnum, string title, string content, List<int> userIds)
+        {
+            var status = await GeneralMethod.MessageIssueAndNotification(msgTypeEnum,title, content, userIds);
+            if (status)
+            {
+                return Ok("Operation Successful!");
+            }
+            else
+            {
+                return Ok("Operation Fail!");
+            }
+            
+        }
+
     }
 }

+ 95 - 5
OASystem/OASystem.Api/OAMethodLib/GeneralMethod.cs

@@ -1,8 +1,13 @@
 using Microsoft.AspNetCore.Authentication;
 using Microsoft.AspNetCore.Authentication.Cookies;
 using Microsoft.AspNetCore.Mvc.TagHelpers;
+using Microsoft.AspNetCore.SignalR;
 using Microsoft.International.Converters.PinYinConverter;
+using NPOI.SS.Formula.PTG;
+using OASystem.API.OAMethodLib.Hub.HubClients;
+using OASystem.API.OAMethodLib.Hub.Hubs;
 using OASystem.API.OAMethodLib.JuHeAPI;
+using OASystem.API.OAMethodLib.SignalR.Hubs;
 using OASystem.Domain;
 using OASystem.Domain.Dtos.Financial;
 using OASystem.Domain.Entities.Customer;
@@ -16,14 +21,14 @@ using OASystem.Domain.ViewModels.JuHeExchangeRate;
 using OASystem.Domain.ViewModels.PersonnelModule;
 using OASystem.Infrastructure.Repositories.CRM;
 using OASystem.Infrastructure.Repositories.Groups;
+using System.Collections;
 using System.Collections.Generic;
 using System.IdentityModel.Tokens.Jwt;
 using System.Security.Claims;
+using static OpenAI.GPT3.ObjectModels.SharedModels.IOpenAiModels;
 
 namespace OASystem.API.OAMethodLib
 {
-
-
     public static class GeneralMethod
     {
         //团组信息
@@ -32,11 +37,96 @@ namespace OASystem.API.OAMethodLib
         private readonly static IJuHeApiService _juHeApiService = AutofacIocManager.Instance.GetService<IJuHeApiService>();
         private readonly static SetDataRepository _setDataRep = AutofacIocManager.Instance.GetService<SetDataRepository>();
         private readonly static TableOperationRecordRepository _tableOperationRecordRep = AutofacIocManager.Instance.GetService<TableOperationRecordRepository>();
-        private readonly static HttpContext _httpContext ;
+        private readonly static MessageRepository _messageRep = AutofacIocManager.Instance.GetService<MessageRepository>();
+        private readonly static IHubContext<ChatHub, IChatClient> _hubContext = AutofacIocManager.Instance.GetService<IHubContext<ChatHub, IChatClient>>();
+
+        #region 消息 
+        /// <summary>
+        /// 消息 发布And 通知
+        /// </summary>
+        /// <param name="msgTypeEnum">
+        /// 消息类型 
+        /// 1 公告通知
+        /// 2 团组流程管控通知
+        /// 1 团组业务操作通知
+        /// 2 团组费用审核消息
+        /// 5 团组签证进度通知
+        /// 6 团组任务进度通知
+        /// </param>
+        /// <param name="title">消息标题</param>
+        /// <param name="content">消息内容</param>
+        /// <param name="userIds"></param>
+        /// <param name="diId">团组id</param>
+        /// <returns></returns>
+        public static async Task<bool> MessageIssueAndNotification(MessgeTypeEnum msgTypeEnum,string title,string content, List<int> userIds,int diId = 0)
+        {
+            
+            MessageDto messageDto = new()
+            { 
+                Type = msgTypeEnum,
+                IssuerId = 4,//管理员
+                DiId = diId,
+                Title = title,
+                Content = content,
+                ReleaseTime = DateTime.Now,
+                UIdList = userIds
+            };
+            var status = await _messageRep.AddMsg(messageDto);//添加消息
+
+            if (status)
+            {
+                //给在线在用户发送消息
+                List<string> connId = UserStore.OnlineUser.Where(it => userIds.Contains(it.UserId)).Select(it => it.ConnectionId).ToList();
+
+                string notificationTypeStr = GetMsgNotificationType(msgTypeEnum);
+                var msg = JsonConvert.SerializeObject(
+                        new {
+                            NotificationType = notificationTypeStr,
+                            Issuer = "管理员",
+                            Title = title,
+                            Content = content,
+                            ReleaseTime = messageDto.ReleaseTime
+                        }
+                    );
+                await _hubContext.Clients.Clients(connId).ReceiveMessage($"您有一条{msg}相关的消息!");
+
+                return true;
+            }
+
+            return false;
+        }
+        
+        /// <summary>
+        /// 根据消息类型 获取 消息通知类型
+        /// </summary>
+        /// <returns></returns>
+        public static string GetMsgNotificationType(MessgeTypeEnum msgTypeEnum)
+        {
+            int notificationType = 0;
+            string notificationStr = "";
+            List<NotificationTypeView> notificationTypeViews = AppSettingsHelper.Get<NotificationTypeView>("MessageNotificationType");
+            notificationType = notificationTypeViews.Where(it => it.MsgTypeIds.Contains((int)msgTypeEnum)).Select(it => it.TypeId).FirstOrDefault();
 
-        #region 消息
+            if (notificationType == 1) notificationStr = "操作";
+            else if (notificationType == 2) notificationStr = "任务";
 
+            return notificationStr;
+        }
 
+        public class NotificationTypeView
+        {
+            /// <summary>
+            /// 通知类型
+            /// 1 操作通知
+            /// 2 任务通知
+            /// </summary>
+            public int TypeId { get; set; }
+
+            /// <summary>
+            /// 消息类型id
+            /// </summary>
+            public List<int> MsgTypeIds { get; set; }
+        }
 
         #endregion
 
@@ -114,7 +204,7 @@ namespace OASystem.API.OAMethodLib
             var indentity = new ClaimsIdentity(claims, "formlogin");
             var principal = new ClaimsPrincipal(indentity);
 
-             await _httpContext.SignInAsync (CookieAuthenticationDefaults.AuthenticationScheme, principal);
+            // await _httpContext.SignInAsync (CookieAuthenticationDefaults.AuthenticationScheme, principal);
 
 
 

+ 21 - 1
OASystem/OASystem.Api/appsettings.json

@@ -172,7 +172,7 @@
     {
       "CTableId": 98, //CtableId 其他款项
       "PageIdDatas": [ //页面Id
-        
+
       ]
     },
     {
@@ -186,5 +186,25 @@
       "PageIdDatas": [ //页面Id
       ]
     }
+  ],
+
+  //消息通知类型
+  "MessageNotificationType": [
+    {
+      "TypeId": 1, //操作通知
+      "MsgTypeIds": [
+        1, // 公告消息
+        2, // 团组流程管控消息
+        3, // 团组业务操作消息
+        4, // 费用审核消息
+        5  // 签证进度更新消息
+      ]
+    },
+    {
+      "TypeId": 2, //任务通知
+      "MsgTypeIds": [
+        6 //任务进度更新消息
+      ]
+    }
   ]
 }

+ 1 - 1
OASystem/OASystem.Domain/Dtos/System/MsgDto.cs

@@ -57,7 +57,7 @@ namespace OASystem.Domain.Dtos.System
     /// 消息设置已读
     /// 请求dto
     /// </summary>
-    public class MsgSetReadDto : DtoBase
+    public class MsgSetReadDto : PortDtoBase
     {
         /// <summary>
         /// MsgAnthId

+ 3 - 2
OASystem/OASystem.Domain/Entities/System/Sys_Message.cs

@@ -28,9 +28,10 @@ namespace OASystem.Domain.Entities.System
 
         /// <summary>
         /// 发布者用户Id
+        /// 4 管理员 Id 
         /// </summary>
-        [SugarColumn(IsNullable = true,ColumnDataType = "int")]
-        public int IssuerId { get; set; }
+        [SugarColumn(IsNullable = true, ColumnDataType = "int")]
+        public int IssuerId { get; set; } = 4;
 
         /// <summary>
         /// 团组Id,可为0