@using Frontend.Models
@using Katheryne.Abstractions
@inject IJSRuntime JsRuntime
@inject IMessageService MessageService
@code
{
private string MessageSending { get; set; } = string.Empty;
private string _username = string.Empty;
private bool _setUsernameVisible = false;
[Parameter]
public List Messages { get; set; } = null!;
[Parameter]
public IChatRobot Robot { get; set; } = null!;
protected override void OnInitialized()
{
if (string.IsNullOrEmpty(_username))
{
_setUsernameVisible = true;
}
base.OnInitialized();
}
protected override void OnAfterRender(bool firstRender)
{
if (!firstRender)
{
JsRuntime.InvokeVoidAsync("scrollToSection");
}
base.OnAfterRender(firstRender);
}
private void SendMessageClicked()
{
if (string.IsNullOrWhiteSpace(MessageSending))
{
return;
}
Messages.Add(new ChatMessage
{
Left = false,
Sender = string.IsNullOrEmpty(_username) ? "default" : _username,
Text = MessageSending
});
foreach (string answer in Robot.ChatNext(MessageSending))
{
Messages.Add(new ChatMessage
{
Left = true,
Sender = Robot.RobotName,
Text = answer
});
}
MessageSending = string.Empty;
}
private void SetUsernameOkClicked()
{
if (string.IsNullOrEmpty(_username))
{
MessageService.Warning("昵称不能为空");
return;
}
_setUsernameVisible = false;
}
private void SetUsernameCancelClicked()
{
_setUsernameVisible = false;
}
}