using Furion.DataEncryption;
using GDZZ.Core.Entity;
using GDZZ.Core.Service;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace GDZZ.Core;
///
/// 聊天集线器
///
public class ChatHub : Hub
{
private readonly ISysCacheService _cache;
private readonly SqlSugarRepository _sysOnlineUerRep; // 在线用户表仓储
public ChatHub(ISysCacheService cache, SqlSugarRepository sysOnlineUerRep)
{
_sysOnlineUerRep = sysOnlineUerRep;
_cache = cache;
}
//RuntimeMethodHandle-rf
///
/// 连接
///
///
public override async Task OnConnectedAsync()
{
var token = Context.GetHttpContext().Request.Query["access_token"];
var claims = JWTEncryption.ReadJwtToken(token)?.Claims;
var userId = claims.FirstOrDefault(e => e.Type == ClaimConst.CLAINM_USERID)?.Value;
var account= claims.FirstOrDefault(e => e.Type == ClaimConst.CLAINM_ACCOUNT)?.Value;
var name= claims.FirstOrDefault(e => e.Type == ClaimConst.CLAINM_NAME)?.Value;
var tenantId= claims.FirstOrDefault(e => e.Type == ClaimConst.TENANT_ID)?.Value;
var ip = HttpNewUtil.Ip;
if (_sysOnlineUerRep.Any(m => m.Account == account && m.LastLoginIp == ip)) {
await _sysOnlineUerRep.DeleteAsync(m => m.Account == account && m.LastLoginIp == ip);
}
OnlineUser user = new OnlineUser() {
ConnectionId = Context.ConnectionId,
UserId = long.Parse(userId),
LastTime = DateTime.Now,
LastLoginIp= ip,
Account= account,
Name=name,
TenantId=Convert.ToInt64(tenantId)
};
await _sysOnlineUerRep.InsertAsync(user);
}
///
/// 断开
///
///
///
public override async Task OnDisconnectedAsync(Exception exception)
{
if (!string.IsNullOrEmpty(Context.ConnectionId))
{
await _sysOnlineUerRep.DeleteAsync(m => m.ConnectionId == Context.ConnectionId);
}
}
}