优化用户登录和聊天消息处理逻辑
在 `chatapi.cs` 中为 `LoginResultData` 添加 `username` 属性。 更新 `MainWindow.xaml` 中的 `TextBlock` 绑定属性为 `UserName`,并调整头像的 `Margin` 属性。 在 `MainWindow.xaml.cs` 中优化登录成功后的 `UserName` 赋值逻辑,改进聊天消息的创建方式,注释掉旧逻辑,更新默认消息内容为 `(无内容)`,提升代码可读性和用户体验。
This commit is contained in:
parent
c3496de067
commit
e82ae53a42
@ -20,6 +20,7 @@ namespace chatclient.Data
|
|||||||
public string? status { get; set; }
|
public string? status { get; set; }
|
||||||
public string? message { get; set; }
|
public string? message { get; set; }
|
||||||
public string? token { get; set; }
|
public string? token { get; set; }
|
||||||
|
public string? username { get; set; }
|
||||||
}
|
}
|
||||||
internal class SignData
|
internal class SignData
|
||||||
{
|
{
|
||||||
|
@ -14,10 +14,10 @@
|
|||||||
<TabControl VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}">
|
<TabControl VerticalContentAlignment="Bottom" materialDesign:ColorZoneAssist.Mode="PrimaryMid" Style="{StaticResource MaterialDesignNavigationRailTabControl}">
|
||||||
<materialDesign:NavigationRailAssist.FloatingContent>
|
<materialDesign:NavigationRailAssist.FloatingContent>
|
||||||
<StackPanel>
|
<StackPanel>
|
||||||
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12" >
|
<Button Style="{StaticResource MaterialDesignFloatingActionMiniLightButton}" Margin="12">
|
||||||
<Image Source="/resource/user.png" />
|
<Image Source="/resource/user.png" />
|
||||||
</Button>
|
</Button>
|
||||||
<TextBlock Text="{Binding Username}"/>
|
<TextBlock Text="{Binding UserName}" TextAlignment="Center" MaxWidth="64" MaxHeight="64" TextWrapping="Wrap"/>
|
||||||
</StackPanel>
|
</StackPanel>
|
||||||
</materialDesign:NavigationRailAssist.FloatingContent>
|
</materialDesign:NavigationRailAssist.FloatingContent>
|
||||||
<TabItem>
|
<TabItem>
|
||||||
@ -69,7 +69,7 @@
|
|||||||
<ColumnDefinition Width="Auto"/>
|
<ColumnDefinition Width="Auto"/>
|
||||||
</Grid.ColumnDefinitions>
|
</Grid.ColumnDefinitions>
|
||||||
<!-- 左侧头像(仅当Alignment=Left时显示) -->
|
<!-- 左侧头像(仅当Alignment=Left时显示) -->
|
||||||
<Border x:Name="leftAvatar" Grid.Column="0" Width="40" Height="40" Margin="10,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
<Border x:Name="leftAvatar" Grid.Column="0" Width="40" Height="40" Margin="10,0,5,0" CornerRadius="20" VerticalAlignment="Top">
|
||||||
<!-- 图片头像 -->
|
<!-- 图片头像 -->
|
||||||
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||||
<Image.Clip>
|
<Image.Clip>
|
||||||
@ -86,7 +86,7 @@
|
|||||||
</StackPanel>
|
</StackPanel>
|
||||||
</materialDesign:Card>
|
</materialDesign:Card>
|
||||||
<!-- 右侧头像(仅当Alignment=Right时显示) -->
|
<!-- 右侧头像(仅当Alignment=Right时显示) -->
|
||||||
<Border x:Name="rightAvatar" Grid.Column="2" Width="40" Height="40" Margin="10,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
<Border x:Name="rightAvatar" Grid.Column="2" Width="40" Height="40" Margin="5,0,10,0" CornerRadius="20" VerticalAlignment="Top">
|
||||||
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
<Image Source="{Binding Image}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Center">
|
||||||
<Image.Clip>
|
<Image.Clip>
|
||||||
<EllipseGeometry Center="20,20" RadiusX="20" RadiusY="20"/>
|
<EllipseGeometry Center="20,20" RadiusX="20" RadiusY="20"/>
|
||||||
|
@ -135,6 +135,7 @@ namespace chatclient
|
|||||||
if (LoginResponse!.status == "success" && LoginResponse != null)
|
if (LoginResponse!.status == "success" && LoginResponse != null)
|
||||||
{
|
{
|
||||||
token = LoginResponse.token;
|
token = LoginResponse.token;
|
||||||
|
UserName = LoginResponse.username ?? "Unnamed";
|
||||||
Application.Current.Dispatcher.Invoke(() =>
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||||
@ -181,18 +182,37 @@ namespace chatclient
|
|||||||
Application.Current.Dispatcher.Invoke(() =>
|
Application.Current.Dispatcher.Invoke(() =>
|
||||||
{
|
{
|
||||||
// 处理聊天消息
|
// 处理聊天消息
|
||||||
var chatmessage = new ChatMessage
|
if (chat.user == UserName)
|
||||||
{
|
{
|
||||||
Sender = chat.user ?? "未知用户",
|
var chatmessage = new ChatMessage
|
||||||
Type = MessageType.Text,
|
{
|
||||||
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
|
Sender = chat.user ?? "未知用户",
|
||||||
Content = chat.message ?? "无内容",
|
Type = MessageType.Text,
|
||||||
Timestamp = chat.timestamp,
|
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
|
||||||
Alignment = HorizontalAlignment.Left,
|
Content = chat.message ?? "(无内容)",
|
||||||
SenderColor = new SolidColorBrush(Colors.Black)
|
Timestamp = DateTime.Now,
|
||||||
};
|
Alignment = HorizontalAlignment.Right,
|
||||||
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
SenderColor = new SolidColorBrush(Colors.Blue)
|
||||||
mainWindow?.Messages.Add(chatmessage);
|
};
|
||||||
|
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||||
|
mainWindow?.Messages.Add(chatmessage);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var chatmessage = new ChatMessage
|
||||||
|
{
|
||||||
|
Sender = chat.user ?? "未知用户",
|
||||||
|
Type = MessageType.Text,
|
||||||
|
Image = new BitmapImage(new Uri(chat.image ?? "pack://application:,,,/resource/user.png", UriKind.Absolute)),
|
||||||
|
Content = chat.message ?? "(无内容)",
|
||||||
|
Timestamp = chat.timestamp,
|
||||||
|
Alignment = HorizontalAlignment.Left,
|
||||||
|
SenderColor = new SolidColorBrush(Colors.Black)
|
||||||
|
};
|
||||||
|
var mainWindow = Application.Current.Windows.OfType<MainWindow>().FirstOrDefault();
|
||||||
|
mainWindow?.Messages.Add(chatmessage);
|
||||||
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -234,21 +254,22 @@ namespace chatclient
|
|||||||
// 判断是否为群组,若是则收件人设为“所有人”,否则为联系人显示名
|
// 判断是否为群组,若是则收件人设为“所有人”,否则为联系人显示名
|
||||||
//string recipient = contact?.IsGroup == true ? "所有人" : contact?.DisplayName;
|
//string recipient = contact?.IsGroup == true ? "所有人" : contact?.DisplayName;
|
||||||
|
|
||||||
|
// 弃用的方法
|
||||||
// 创建新消息
|
// 创建新消息
|
||||||
var newMessage = new ChatMessage
|
//var newMessage = new ChatMessage
|
||||||
{
|
//{
|
||||||
Sender = "我",
|
// Sender = "我",
|
||||||
Type = MessageType.Text,
|
// Type = MessageType.Text,
|
||||||
Image = new BitmapImage(new Uri("pack://application:,,,/resource/user.png", UriKind.Absolute)), // 默认头像
|
// Image = new BitmapImage(new Uri("pack://application:,,,/resource/user.png", UriKind.Absolute)), // 默认头像
|
||||||
Content = txtMessage.Text,
|
// Content = txtMessage.Text,
|
||||||
Timestamp = DateTime.Now,
|
// Timestamp = DateTime.Now,
|
||||||
Alignment = HorizontalAlignment.Right, // 自己发送的消息靠右
|
// Alignment = HorizontalAlignment.Right, // 自己发送的消息靠右
|
||||||
SenderColor = new SolidColorBrush(Colors.Blue)
|
// SenderColor = new SolidColorBrush(Colors.Blue)
|
||||||
};
|
//};
|
||||||
var newChatMessage = new ChatData
|
var newChatMessage = new ChatData
|
||||||
{
|
{
|
||||||
type = "chat",
|
type = "chat",
|
||||||
message = newMessage.Content
|
message = txtMessage.Text
|
||||||
};
|
};
|
||||||
string ChatJsonData = JsonSerializer.Serialize(newChatMessage);
|
string ChatJsonData = JsonSerializer.Serialize(newChatMessage);
|
||||||
byte[] dataBytes = Encoding.UTF8.GetBytes(ChatJsonData);
|
byte[] dataBytes = Encoding.UTF8.GetBytes(ChatJsonData);
|
||||||
@ -272,14 +293,10 @@ namespace chatclient
|
|||||||
log.Error(ex);
|
log.Error(ex);
|
||||||
Client.Close();
|
Client.Close();
|
||||||
}
|
}
|
||||||
//finally
|
|
||||||
//{
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 添加到消息列表
|
// 添加到消息列表
|
||||||
Messages.Add(newMessage);
|
//Messages.Add(newMessage);
|
||||||
// 清空输入框
|
// 清空输入框
|
||||||
txtMessage.Clear();
|
txtMessage.Clear();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user