Files
BuYuHouDuan/CustomerSrc/CustomerApi/Hubs/CustomerHub.cs
T
2026-10-12 16:22:04 +08:00

159 lines
6.2 KiB
C#

using Microsoft.AspNetCore.SignalR;
using CustomerService.Customer;
namespace CustomerApi.Hubs;
public class CustomerHub : Hub
{
private readonly ICustomerService _customerService;
public CustomerHub(ICustomerService customerService)
{
_customerService = customerService;
}
public async Task JoinConversation(string conversationId)
{
await Groups.AddToGroupAsync(Context.ConnectionId, conversationId);
}
public async Task LeaveConversation(string conversationId)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, conversationId);
}
public async Task JoinCustomerQueue(string customerId)
{
if (!string.IsNullOrEmpty(customerId))
await Groups.AddToGroupAsync(Context.ConnectionId, "customer_" + customerId);
}
public async Task LeaveCustomerQueue(string customerId)
{
if (!string.IsNullOrEmpty(customerId))
await Groups.RemoveFromGroupAsync(Context.ConnectionId, "customer_" + customerId);
}
public async Task SendMessage(string conversationId, int senderType, string senderId, string senderName, string? senderAvatar, int contentType, string content, string guId = "")
{
try
{
if (string.IsNullOrEmpty(conversationId) && senderType == 1)
{
var startResult = await _customerService.StartConversation(senderId, senderName, senderAvatar);
if (startResult.isOk && startResult.conversation != null)
{
conversationId = startResult.conversation.Id;
await Groups.AddToGroupAsync(Context.ConnectionId, conversationId);
if (!string.IsNullOrEmpty(startResult.conversation.CustomerId))
{
await Clients.Group("customer_" + startResult.conversation.CustomerId).SendAsync("OnNewConversation", new
{
conversationId = startResult.conversation.Id,
customerId = startResult.conversation.CustomerId,
customerName = startResult.conversation.CustomerName,
userId = startResult.conversation.UserId,
userName = startResult.conversation.UserName,
status = startResult.conversation.Status,
createTime = startResult.conversation.CreateTime
});
}
}
else
{
await Clients.Caller.SendAsync("ErrorMessage", startResult.msg);
}
}
await Groups.AddToGroupAsync(Context.ConnectionId, conversationId);
var result = await _customerService.SendMessage(conversationId, senderType, senderId, senderName, contentType, content, senderAvatar);
if (result.isOk)
{
if (string.IsNullOrWhiteSpace(guId))
guId = Guid.NewGuid().ToString("N");
await Clients.Group(conversationId).SendAsync("OnMessageReceived", new
{
GuId = guId,
ConversationId = conversationId,
SenderType = senderType,
SenderId = senderId,
SenderName = senderName,
SenderAvatar = senderAvatar,
ContentType = contentType,
Content = content,
CreateTime = DateTime.Now
});
}
}
catch (Exception ex)
{
await Clients.Caller.SendAsync("ErrorMessage", ex.Message);
}
}
public async Task StartConversation(string userId, string userName)
{
try
{
if (string.IsNullOrEmpty(userId) || string.IsNullOrEmpty(userName))
{
await Clients.Caller.SendAsync("ErrorMessage", "参数错误");
return;
}
var result = await _customerService.StartConversation(userId, userName);
if (result.isOk && result.conversation != null)
{
await Clients.Caller.SendAsync("OnConversationStarted", result.conversation);
if (!string.IsNullOrEmpty(result.conversation.Id))
{
await Groups.AddToGroupAsync(Context.ConnectionId, result.conversation.Id);
}
if (!string.IsNullOrEmpty(result.conversation.CustomerId))
{
await Clients.Group("customer_" + result.conversation.CustomerId).SendAsync("OnNewConversation", new
{
conversationId = result.conversation.Id,
customerId = result.conversation.CustomerId,
customerName = result.conversation.CustomerName,
userId = result.conversation.UserId,
userName = result.conversation.UserName,
status = result.conversation.Status,
createTime = result.conversation.CreateTime
});
}
}
else
{
await Clients.Caller.SendAsync("ErrorMessage", result.msg);
}
}
catch (Exception ex)
{
await Clients.Caller.SendAsync("ErrorMessage", ex.Message);
}
}
public override async Task OnConnectedAsync()
{
var userId = Context.User?.Claims?.FirstOrDefault(c => c.Type == "uid")?.Value;
if (!string.IsNullOrEmpty(userId))
{
await Groups.AddToGroupAsync(Context.ConnectionId, "customer_" + userId);
}
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception? exception)
{
await base.OnDisconnectedAsync(exception);
}
}