This commit is contained in:
mmrbnjd
2025-08-09 18:55:06 +03:30
parent 278bf2f601
commit 8d69dbc946
2 changed files with 90 additions and 3 deletions

View File

@@ -76,7 +76,20 @@
}
<div class="d-flex mb-2 @(msg.Type!=Common.Enums.ConversationType.UE ? "justify-content-end" : "justify-content-start")">
<div class="chat-bubble @(msg.Type!=Common.Enums.ConversationType.UE ? "chat-mine": "chat-other")" data-id="@msg.ID"> @msg.text </div>
<div class="chat-bubble @(msg.Type!=Common.Enums.ConversationType.UE ? "chat-mine": "chat-other")" data-id="@msg.ID">
@if (msg.FileContent != null && msg.FileContent.Length > 0 && !string.IsNullOrWhiteSpace(msg.FileType) && msg.FileType.StartsWith("image/"))
{
<img src="@($"data:{msg.FileType};base64,{Convert.ToBase64String(msg.FileContent)}")" alt="image" style="max-width: 220px; border-radius: 8px; display: block;" />
@if (!string.IsNullOrWhiteSpace(msg.text))
{
<div style="margin-top:6px">@msg.text</div>
}
}
else
{
@msg.text
}
</div>
@if (msg.Type == Common.Enums.ConversationType.UE)
{
if (msg.IsRead)
@@ -140,10 +153,18 @@
<div class="message-input-container" id="B2">
<div class="input-wrapper">
<input type="text" @bind-value="MsgInput" class="message-input" placeholder="پیام خود را بنویسید..." @onkeydown="HandleKeyDown" />
<InputFile OnChange="OnImageSelected" accept="image/*" />
<Button Color="ButtonColor.Primary" Size=ButtonSize.Small @onclick="OnClickSendMsg" Class="send-btn">
<Icon Name="IconName.Send" />
</Button>
</div>
@if (SelectedImagePreview != null)
{
<div class="mt-2 d-flex align-items-center gap-2">
<img src="@SelectedImagePreview" alt="preview" style="max-height:60px;border-radius:8px;border:1px solid #e9ecef;" />
<Button Color="ButtonColor.Secondary" Size=ButtonSize.ExtraSmall Outline="true" @onclick="ClearSelectedImage">حذف تصویر</Button>
</div>
}
</div>
}
@@ -185,6 +206,9 @@
List<Read_GroupDto> CompanyGroups = new();
ChatItemDto? LastOpenChat = new();
string MsgInput = string.Empty;
IBrowserFile? SelectedImageFile = null;
byte[]? SelectedImageBytes = null;
string? SelectedImagePreview = null;
bool chatloading = false;
public bool IsLogin { get; set; } = false;
public bool IsEndFirstProcess { get; set; } = false;
@@ -218,12 +242,27 @@
@functions {
async Task OnClickSendMsg()
{
if (!string.IsNullOrEmpty(MsgInput))
if (!string.IsNullOrEmpty(MsgInput) || SelectedImageFile != null)
{
if (LastOpenChat != null)
{
Common.Enums.ConversationType type = Common.Enums.ConversationType.UE;
var model = await chatService.ADDChatResponse(LastOpenChat.ID, MsgInput, type);
ChatItemResponseDto? model;
if (SelectedImageFile != null)
{
var bytes = SelectedImageBytes ?? Array.Empty<byte>();
model = await chatService.ADDChatResponse(
LastOpenChat.ID,
MsgInput,
type,
SelectedImageFile.Name,
SelectedImageFile.ContentType,
bytes);
}
else
{
model = await chatService.ADDChatResponse(LastOpenChat.ID, MsgInput, type);
}
LastOpenChat?.Responses.Add(model);
LastOpenChat.LastText = MsgInput;
@@ -250,6 +289,9 @@
// Scroll to bottom for user's own messages
await JS.InvokeVoidAsync("scrollToBottom", "B1");
MsgInput = string.Empty;
SelectedImageFile = null;
SelectedImageBytes = null;
SelectedImagePreview = null;
}
}
@@ -439,6 +481,33 @@
}
}
@functions {
private async Task OnImageSelected(InputFileChangeEventArgs e)
{
var file = e.File;
if (file is null)
{
SelectedImageFile = null;
SelectedImageBytes = null;
SelectedImagePreview = null;
return;
}
SelectedImageFile = file;
using var memoryStream = new MemoryStream();
await file.OpenReadStream().CopyToAsync(memoryStream);
SelectedImageBytes = memoryStream.ToArray();
SelectedImagePreview = $"data:{file.ContentType};base64,{Convert.ToBase64String(SelectedImageBytes)}";
}
private Task ClearSelectedImage()
{
SelectedImageFile = null;
SelectedImageBytes = null;
SelectedImagePreview = null;
return Task.CompletedTask;
}
}
<style>
.chat-bubble {
padding: 0.5rem 0.75rem;

View File

@@ -63,6 +63,24 @@ namespace HushianWebApp.Service
}
// Send chat response with optional file attachment (e.g., image)
public async Task<ChatItemResponseDto?> ADDChatResponse(int conversationID, string text, ConversationType type,
string? fileName, string? fileType, byte[]? fileContent)
{
var response = await _baseController.Post($"{BaseRoute}ADDChatResponse", new ADD_ConversationResponseDto()
{
ConversationID = conversationID,
Text = text,
Type = type,
FileName = fileName,
FileType = fileType,
FileContent = fileContent
});
if (response.IsSuccessStatusCode)
return await response.Content.ReadFromJsonAsync<ChatItemResponseDto>();
return null;
}
public async Task<bool> OpenChat(int ChatID)
{
var response = await _baseController.Put($"{BaseRoute}OpenChat/{ChatID}");