You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

627 lines
23 KiB

2 years ago
  1. using EasyBL;
  2. using Entity.Sugar;
  3. using Microsoft.AspNet.SignalR;
  4. using Microsoft.AspNet.SignalR.Hubs;
  5. using Newtonsoft.Json;
  6. using SqlSugar.Base;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using WebApp.Models;
  12. using WebApp.Models.Hub;
  13. namespace WebApp.Hubs
  14. {
  15. [HubName("msgHub")]
  16. public class MsgHub : Hub
  17. {
  18. public static Online Euro_Online = new Online(); // 在線用户列表
  19. public class Online
  20. {
  21. public Online()
  22. {
  23. Users = new List<UserInfo>(); //在線用户列表
  24. Rooms = new List<RoomInfo>(); //房間(群組)
  25. }
  26. //用戶集合
  27. public List<UserInfo> Users { get; set; }
  28. //房間集合
  29. public List<RoomInfo> Rooms { get; set; }
  30. }
  31. /// <summary>
  32. /// 连线时调用
  33. /// </summary>
  34. /// <returns></returns>
  35. public override Task OnConnected()
  36. {
  37. //Console.WriteLine("客戶端連接,連接ID是:{0},當前在綫人數為{1}", Context.ConnectionId, Euro_Online.Users.Count + 1);
  38. return base.OnConnected();
  39. }
  40. /// <summary>
  41. /// 断线时调用
  42. /// </summary>
  43. /// <param name="stopCalled"></param>
  44. /// <returns></returns>
  45. public override Task OnDisconnected(bool stopCalled)
  46. {
  47. var user = Euro_Online.Users.Find(u => u.ConnectionId == Context.ConnectionId);
  48. // 判断用户是否存在,存在則刪除
  49. if (user == null)
  50. {
  51. return base.OnDisconnected(stopCalled);
  52. }
  53. // 刪除用戶
  54. Euro_Online.Users.Remove(user);
  55. var onlineUsers = Euro_Online.Users.Where(x => x.OrgId == user.OrgId).ToList();
  56. var sIds = HubsTool.GetConnectionIdsByOrgID(user.OrgId);
  57. Clients.Clients(sIds).onUserDisconnected(user.ConnectionId, onlineUsers, user.OrgId, user.UserId, user.UserName, false);//調用客戶端用戶離線通知
  58. var sTransferOrgID = Common.ConfigGetValue("", "TransferOrgID");
  59. var sTransferUerID = Common.ConfigGetValue("", "TransferUerID");
  60. var transfer = Euro_Online.Users.Find(u => (u.OrgId == sTransferOrgID && u.UserId == sTransferUerID));
  61. if (transfer != null)
  62. {
  63. Clients.Client(transfer.ConnectionId).onUserDisconnected(user.ConnectionId, Euro_Online.Users, user.OrgId, user.UserId, user.UserName, false);
  64. }
  65. //Console.WriteLine("客戶端斷線,連接ID是:{0},當前在綫人數為{1}", Context.ConnectionId, OnlineUsers.Count);
  66. try
  67. {
  68. var db = SugarBase.DB;
  69. var iEf = db.Deleteable<OTB_SYS_OnlineUsers>()
  70. .Where(it => it.OrgID == user.OrgId && it.UserID == user.UserId).ExecuteCommand();
  71. }
  72. catch (Exception)
  73. {
  74. throw;
  75. }
  76. return base.OnDisconnected(stopCalled);
  77. }
  78. public override Task OnReconnected()
  79. {
  80. return base.OnReconnected();
  81. }
  82. /// <summary>
  83. /// 登入連線
  84. /// </summary>
  85. /// <param name="orgid">組織Id</param>
  86. /// <param name="userid"></param>
  87. /// <param name="username"></param>
  88. /// <param name="islogin"></param>
  89. public void Register(string orgid, string userid, string username, bool islogin)
  90. {
  91. var connnectId = Context.ConnectionId;
  92. var db = SugarBase.GetIntance();
  93. if (Euro_Online.Users.Count(x => x.ConnectionId == connnectId) == 0)
  94. {
  95. if (Euro_Online.Users.Any(x => (x.UserId == userid /*&& x.OrgId == orgid*/)))
  96. {
  97. var item = Euro_Online.Users.First(x => (x.UserId == userid /*&& x.OrgId == orgid*/));
  98. var oTicket = db.Queryable<OTB_SYS_TicketAuth>().First(it => it.UserID == userid /*&& it.OrgID == orgid*/);
  99. if (oTicket != null)
  100. {
  101. // 20180311 修改帳號登入不同單位自動登出
  102. //var users = Euro_Online.Users.Where(x => x.OrgId == orgid).ToList();
  103. var users = Euro_Online.Users.ToList();
  104. //var _sIds = HubsTool.GetConnectionIdsByOrgID(orgid);
  105. var _sIds = Euro_Online.Users.Select(p => p.ConnectionId).ToList();
  106. Clients.Clients(_sIds).onUserDisconnected(item.ConnectionId, users, item.OrgId, item.UserId, item.UserName, islogin, "IP:" + oTicket.LoginIp + "<br>DATETIME:" + oTicket.LoginTime);
  107. }
  108. Euro_Online.Users.RemoveAll(x => (x.UserId == userid && x.OrgId == orgid));
  109. }
  110. var Info = new UserInfo
  111. {
  112. ConnectionId = connnectId,
  113. OrgId = orgid,
  114. UserId = userid,
  115. UserName = username,
  116. LastLoginTime = DateTime.Now
  117. };
  118. //string sWSUrl = EasyBL.Common.GetAppSettings("WebServiceUrl");
  119. //string[] Params = new string[1];
  120. //Params[0] = "gstrUserId";
  121. //string sParams = JsonConvert.SerializeObject(Params, Formatting.Indented); //把list轉成Json字串
  122. //string sValue = (string)EasyBL.WebServiceUtil.InvokeWebService(sWSUrl, "GetSession", new object[] { Params });
  123. //Dictionary<string, object> dic_Session = JsonConvert.DeserializeObject<Dictionary<string, object>>(sValue);
  124. //if (dic_Session["gstrUserId"] != null && dic_Session["gstrUserId"].ToString() != "")
  125. //{
  126. // //添加在線人员
  127. // Euro_Online.Users.Add(Info);
  128. //}
  129. //添加在線人员
  130. Euro_Online.Users.Add(Info);
  131. //CreateRoom(usertype);
  132. //AddRoom(usertype);
  133. //Groups.Add(connnectId, usertype);
  134. try
  135. {
  136. var oNlineUser = new OTB_SYS_OnlineUsers
  137. {
  138. OrgID = orgid,
  139. UserID = userid
  140. };
  141. var saOnlineUser = db.Queryable<OTB_SYS_OnlineUsers>().Where(it => it.OrgID == orgid && it.UserID == userid).ToList();
  142. if (saOnlineUser.Count == 0)
  143. {
  144. var iEf = db.Insertable(oNlineUser).ExecuteCommand();
  145. }
  146. }
  147. catch (Exception)
  148. {
  149. throw;
  150. }
  151. }
  152. // 所有客戶端同步在線用户
  153. var onlineUsers = Euro_Online.Users.Where(x => x.OrgId == orgid).ToList();
  154. var sIds = HubsTool.GetConnectionIdsByOrgID(orgid);
  155. Clients.Clients(sIds).onConnected(connnectId, username, onlineUsers);
  156. var sTransferOrgID = Common.ConfigGetValue("", "TransferOrgID");
  157. var sTransferUerID = Common.ConfigGetValue("", "TransferUerID");
  158. var transfer = Euro_Online.Users.Find(u => (u.OrgId == sTransferOrgID && u.UserId == sTransferUerID));
  159. if (transfer != null)
  160. {
  161. Clients.Client(transfer.ConnectionId).onConnected(connnectId, username, Euro_Online.Users);
  162. }
  163. }
  164. /// <summary>
  165. /// 登录连线
  166. /// </summary>
  167. /// <param name="orgid">組織Id</param>
  168. /// <param name="userId">用户Id</param>
  169. /// <param name="userName">用户名</param>
  170. public void Offline()
  171. {
  172. var user = Euro_Online.Users.Find(u => u.ConnectionId == Context.ConnectionId);
  173. // 判断用户是否存在,存在則刪除
  174. if (user != null)
  175. {
  176. // 删除用户
  177. var sIds = HubsTool.GetConnectionIdsByOrgID(user.OrgId);
  178. Euro_Online.Users.Remove(user);
  179. var onlineUsers = Euro_Online.Users.Where(x => x.OrgId == user.OrgId).ToList();
  180. Clients.Clients(sIds).onUserDisconnected(user.ConnectionId, onlineUsers, user.OrgId, user.UserId, user.UserName, true);//調用客戶端用戶離線通知
  181. var sTransferOrgID = Common.ConfigGetValue("", "TransferOrgID");
  182. var sTransferUerID = Common.ConfigGetValue("", "TransferUerID");
  183. var transfer = Euro_Online.Users.Find(u => (u.OrgId == sTransferOrgID && u.UserId == sTransferUerID));
  184. if (transfer != null)
  185. {
  186. Clients.Client(transfer.ConnectionId).onUserDisconnected(user.ConnectionId, Euro_Online.Users, user.OrgId, user.UserId, user.UserName, true);
  187. }
  188. {
  189. var db = SugarBase.DB;
  190. var iEf = db.Deleteable<OTB_SYS_OnlineUsers>()
  191. .Where(it => it.OrgID == user.OrgId && it.UserID == user.UserId).ExecuteCommand();
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// 加入群組
  197. /// </summary>
  198. /// <param name="groupName">組名稱</param>
  199. /// <param name="connectionId">當前連線ID</param>
  200. public void JoinGroup(string groupName, string connectionId)
  201. {
  202. if (string.IsNullOrEmpty(connectionId))
  203. {
  204. connectionId = Context.ConnectionId;
  205. }
  206. Groups.Add(connectionId, groupName);
  207. Clients.All.hubMessage(connectionId + " joined group " + groupName);
  208. }
  209. public void LeaveGroup(string groupName, string connectionId)
  210. {
  211. if (string.IsNullOrEmpty(connectionId))
  212. {
  213. connectionId = Context.ConnectionId;
  214. }
  215. Groups.Remove(connectionId, groupName);
  216. Clients.All.hubMessage(connectionId + " left group " + groupName);
  217. }
  218. //加入聊天室
  219. public void AddRoom(string roomName)
  220. {
  221. //查询聊天室
  222. var room = Euro_Online.Rooms.Find(a => a.RoomName == roomName);
  223. //存在則加入
  224. if (room != null)
  225. {
  226. //查找房間中是否存在此用户
  227. var isUser = room.Users.Find(w => w.ConnectionId == Context.ConnectionId);
  228. //不存在則加入
  229. if (isUser == null)
  230. {
  231. var user = Euro_Online.Users.Find(a => a.ConnectionId == Context.ConnectionId);
  232. user.Rooms.Add(room);
  233. room.Users.Add(user);
  234. Groups.Add(Context.ConnectionId, roomName);
  235. //註冊加入聊天室的addRoom方法
  236. Clients.Client(Context.ConnectionId).addRoom(roomName);
  237. }
  238. else
  239. {
  240. Clients.Client(Context.ConnectionId).showMessage("请勿重复加入房间");
  241. }
  242. }
  243. }
  244. /// <summary>
  245. /// 更新所有用户的房间列表
  246. /// </summary>
  247. private void GetRooms()
  248. {
  249. var rooms = JsonConvert.SerializeObject(Euro_Online.Rooms.Select(p => p.RoomName).ToList());
  250. Clients.All.getRoomList(rooms);
  251. }
  252. /// <summary>
  253. /// 创建聊天室
  254. /// </summary>
  255. /// <param name="roomName"></param>
  256. public void CreateRoom(string roomName)
  257. {
  258. var room = Euro_Online.Rooms.Find(a => a.RoomName == roomName);
  259. if (room == null)
  260. {
  261. var r = new RoomInfo() { RoomName = roomName };
  262. //将房间加入列表
  263. Euro_Online.Rooms.Add(r);
  264. AddRoom(roomName);
  265. Clients.Client(Context.ConnectionId).showMessage("房间创建完成");
  266. GetRooms();
  267. }
  268. else
  269. {
  270. Clients.Client(Context.ConnectionId).showMessage("房间名重复");
  271. }
  272. }
  273. /// <summary>
  274. /// 退出聊天室
  275. /// </summary>
  276. /// <param name="roomName"></param>
  277. public void ExitRoom(string roomName)
  278. {
  279. //查找房间是否存在
  280. var room = Euro_Online.Rooms.Find(a => a.RoomName == roomName);
  281. //存在则删除
  282. if (room != null)
  283. {
  284. //查找要删除的用户
  285. var user = room.Users.Find(p => p.ConnectionId == Context.ConnectionId);
  286. //移除此用户
  287. room.Users.Remove(user);
  288. //如果房间人数为0,怎删除房间
  289. if (room.Users.Count == 0)
  290. {
  291. Euro_Online.Rooms.Remove(room);
  292. }
  293. //Groups Remove移除分组方法
  294. Groups.Remove(Context.ConnectionId, roomName);
  295. //提示客户端
  296. Clients.Client(Context.ConnectionId).removeRoom("退出成功");
  297. }
  298. }
  299. /// <summary>
  300. /// 發送給自己
  301. /// </summary>
  302. /// <param name="value"></param>
  303. public void SendToMe(string value)
  304. {
  305. Clients.Caller.hubMessage(value);
  306. }
  307. /// <summary>
  308. /// 檢核當前頁面是否有人在操作
  309. /// </summary>
  310. /// <param name="prgid"></param>
  311. /// <param name="dataid"></param>
  312. public void CheckEdit(string prgid, string dataid)
  313. {
  314. var connnectId = Context.ConnectionId;
  315. var bEdit = true;
  316. var sUserName = "";
  317. foreach (UserInfo user in Euro_Online.Users)
  318. {
  319. if (user.ConnectionId != connnectId && user.Prgs.Count(p => p.FnId == prgid && p.DataId == dataid) > 0)
  320. {
  321. sUserName = user.UserName;
  322. bEdit = false;
  323. break;
  324. }
  325. }
  326. if (bEdit && Euro_Online.Users.Count(x => x.ConnectionId == connnectId) > 0)
  327. {
  328. foreach (UserInfo user in Euro_Online.Users)
  329. {
  330. if (user.ConnectionId == connnectId && user.Prgs.Count(p => p.FnId == prgid && p.DataId == dataid) == 0)
  331. {
  332. var prg = new EditPrgInfo
  333. {
  334. FnId = prgid,
  335. DataId = dataid
  336. };
  337. user.Prgs.Add(prg);
  338. break;
  339. }
  340. }
  341. }
  342. Clients.Caller.checkedit(bEdit, prgid, sUserName);
  343. }
  344. /// <summary>
  345. /// 檢核當前頁面是否有人在操作
  346. /// </summary>
  347. /// <param name="prgid"></param>
  348. public void RemoveEditPrg(string prgid)
  349. {
  350. var connnectId = Context.ConnectionId;
  351. foreach (UserInfo user in Euro_Online.Users)
  352. {
  353. if (user.ConnectionId == connnectId)
  354. {
  355. var prg = user.Prgs.Find(p => p.FnId == prgid);
  356. if (prg != null)
  357. {
  358. user.Prgs.Remove(prg);
  359. break;
  360. }
  361. }
  362. }
  363. //Clients.Caller.checkEdit(bEdit);
  364. }
  365. /// <summary>
  366. /// 發送給指定人
  367. /// </summary>
  368. /// <param name="userId"></param>
  369. /// <param name="value"></param>
  370. public void SendToUser(string userId, string value)
  371. {
  372. Clients.User(userId).message(value);
  373. }
  374. public void SendToUsers(IList<string> userIds, string value)
  375. {
  376. Clients.Users(userIds).message(value);
  377. }
  378. public void SendToConnectionId(string connectionId, string value)
  379. {
  380. Clients.Client(connectionId).hubMessage(value);
  381. }
  382. public void SendToAll(string value)
  383. {
  384. Clients.All.hubMessage(value);
  385. }
  386. public void SendToGroup(string groupName, string value)
  387. {
  388. Clients.Group(groupName).hubMessage(value);
  389. }
  390. public void Broadcast(OTB_SYS_Announcement msg)
  391. {
  392. //string sConnectionId = Context.ConnectionId;
  393. Clients.All.broadcast(msg); // 向所有客户端廣播
  394. }
  395. public void Send(Message msg)
  396. {
  397. switch (msg.Type)
  398. {
  399. case MessageType.SendToMe:
  400. {
  401. Clients.Caller.receive(msg);
  402. break;
  403. }
  404. case MessageType.SendToConnectionId:
  405. {
  406. Clients.Client(msg.ConnectionId).receive(msg); // 特定的客户端,这个方法也就是我们实现端对端聊天的关键
  407. break;
  408. }
  409. case MessageType.SendToUser:
  410. {
  411. var sId = HubsTool.GetConnectionId(msg.ToOrgId, msg.ToUserId);
  412. if (sId != "")
  413. {
  414. Clients.Client(sId).receive(msg); // 特定的客户端,这个方法也就是我们实现端对端聊天的关键
  415. }
  416. break;
  417. }
  418. case MessageType.SendToUsers:
  419. {
  420. var sIds = HubsTool.GetConnectionIds(msg.ToUserIds);
  421. if (sIds.Count > 0)
  422. {
  423. Clients.Clients(sIds).receive(msg); // 特定的客户端,这个方法也就是我们实现端对端聊天的关键
  424. }
  425. break;
  426. }
  427. case MessageType.SendBroadcast:
  428. {
  429. Clients.All.Broadcast(msg);
  430. break;
  431. }
  432. case MessageType.SendToGroup:
  433. {
  434. Clients.Group(msg.GroupName).receive(msg); // 指定客户端组,这个也是实现群聊的关键所在
  435. break;
  436. }
  437. case MessageType.Throw:
  438. {
  439. throw new InvalidOperationException("Client does not receive this error");
  440. } //break;
  441. default:
  442. {
  443. break;
  444. }
  445. }
  446. }
  447. public void PushTip(string orgId, string userId)
  448. {
  449. var sId = HubsTool.GetConnectionId(orgId, userId);
  450. if (sId != "")
  451. {
  452. Clients.Client(sId).pushtips();
  453. }
  454. }
  455. public void PushTips(IList<string> userIds)
  456. {
  457. var sIds = HubsTool.GetConnectionIds(userIds);
  458. if (sIds.Count > 0)
  459. {
  460. Clients.Clients(sIds).pushtips();
  461. }
  462. }
  463. public void PushMsg(string orgId, string userId)
  464. {
  465. var sId = HubsTool.GetConnectionId(orgId, userId);
  466. if (sId != "")
  467. {
  468. Clients.Client(sId).pushmsgs();
  469. }
  470. }
  471. public void PushMsgs(IList<string> userIds)
  472. {
  473. var sIds = HubsTool.GetConnectionIds(userIds);
  474. if (sIds.Count > 0)
  475. {
  476. Clients.Clients(sIds).pushmsgs();
  477. }
  478. }
  479. public void PushTransfer(string orgId, string userId, string data, int index)
  480. {
  481. var sTransferOrgID = Common.ConfigGetValue("", "TransferOrgID");
  482. var sTransferUerID = Common.ConfigGetValue("", "TransferUerID");
  483. var sId = HubsTool.GetConnectionId(sTransferOrgID, sTransferUerID);
  484. if (sId != "")
  485. {
  486. var sTransferData = "";
  487. switch (index)
  488. {
  489. case 1://抓取要拋轉的賬單資料
  490. {
  491. sTransferData = MsgHubService.GetBillsString(orgId, data);
  492. break;
  493. }
  494. case 2://抓取要拋轉的客戶資料
  495. {
  496. sTransferData = MsgHubService.GetCustomersString(orgId, data);
  497. break;
  498. }
  499. case 3://抓取要拋轉的專案資料
  500. {
  501. sTransferData = MsgHubService.GetExhibitionsString(orgId, data);
  502. break;
  503. }
  504. default:
  505. {
  506. break;
  507. }
  508. }
  509. if (sTransferData != "[]")
  510. {
  511. Clients.Client(sId).pushtransfer(orgId, userId, sTransferData, index);
  512. }
  513. }
  514. else
  515. {
  516. Clients.Caller.transfertips();
  517. }
  518. }
  519. public void TransferBack(string data, int index)
  520. {
  521. switch (index)
  522. {
  523. case 1://賬單移除
  524. {
  525. MsgHubService.RemoveBills(data);
  526. break;
  527. }
  528. case 2://客戶移除
  529. {
  530. MsgHubService.RemoveCustomers(data);
  531. break;
  532. }
  533. case 3://專案移除
  534. {
  535. MsgHubService.RemoveExhibitions(data);
  536. break;
  537. }
  538. default:
  539. {
  540. break;
  541. }
  542. }
  543. }
  544. public void ExistTrasfer(string data, string userId)
  545. {
  546. var bInstall = true;
  547. if (Euro_Online.Users.Count(x => x.UserId == userId) == 0)
  548. {
  549. bInstall = false;
  550. }
  551. Clients.Caller.existtrasfer(data, bInstall);
  552. }
  553. public void GetOnlineUsers()
  554. {
  555. var user = Euro_Online.Users.Find(u => u.ConnectionId == Context.ConnectionId);
  556. var sTransferOrgID = Common.ConfigGetValue("", "TransferOrgID");
  557. var sTransferUerID = Common.ConfigGetValue("", "TransferUerID");
  558. if (user != null)
  559. {
  560. if (user.UserId == sTransferUerID)
  561. {
  562. Clients.Caller.onlineusers(Euro_Online.Users);
  563. }
  564. else
  565. {
  566. var onlineUsers = Euro_Online.Users.Where(x => x.OrgId == user.OrgId).ToList();
  567. var sIds = HubsTool.GetConnectionIdsByOrgID(user.OrgId);
  568. Clients.Clients(sIds).onlineusers(onlineUsers);
  569. }
  570. }
  571. }
  572. public void Hello()
  573. {
  574. Clients.All.hello();
  575. }
  576. }
  577. }