@page "/AIChat/{CompanyID:int}" @using Common.Dtos @inject ConversationService conversationService @inject ILocalStorageService localStorageService @inject AuthService authService @inject BaseController baseController @inject ToastService toastService @inject IJSRuntime JS @layout UserPanelLayout @using HushianWebApp.Service @using HushianWebApp.Services @using Microsoft.AspNetCore.Components.Web
@if (isReady) { @if (isLogin) {

گفتگو هوش مصنوعی

@if (messages is not null) {
@foreach (var msg in messages) {
@msg.requestText
@msg.responseText
}
}
} else {

نیاز به ورود دارید

} } else {

در حال بارگذاری ...

}
@code { [Parameter] public int CompanyID { get; set; } List messages = new(); string inputText = string.Empty; bool isLogin = false; bool isReady = false; protected override async Task OnInitializedAsync() { await EnsureAuth(); if (isLogin) { await LoadMessages(); } } private async Task EnsureAuth() { var token = await localStorageService.GetItem("U/key"); if (string.IsNullOrEmpty(token)) { isLogin = false; isReady = true; return; } await baseController.RemoveToken(); await baseController.SetToken(token); isLogin = await authService.IsOnline(); isReady = true; } private async Task LoadMessages() { messages = await conversationService.GetAiCurrentResponses(CompanyID); StateHasChanged(); await JS.InvokeVoidAsync("scrollToBottom", "ai-chat"); } private async Task Send() { if (string.IsNullOrWhiteSpace(inputText)) return; var token = await localStorageService.GetItem("U/key"); if (!isLogin || string.IsNullOrEmpty(token)) { toastService.Notify(new ToastMessage(ToastType.Warning, "ابتدا وارد شوید")); return; } var dto = new aiNewResponseDto { companyId = CompanyID, userId = 0, requestText = inputText }; var okmodel = await conversationService.AiNewResponse(dto); if (okmodel!=null) { messages.Add(okmodel); inputText = string.Empty; StateHasChanged(); await JS.InvokeVoidAsync("scrollToBottom", "ai-chat"); } else { toastService.Notify(new ToastMessage(ToastType.Danger, "ارسال ناموفق بود")); } } private async Task HandleKeyDown(KeyboardEventArgs e) { if (e.Key == "Enter") await Send(); } private async Task Logout() { await baseController.RemoveToken(); await localStorageService.RemoveItem("U/key"); isLogin = false; } }