wpf版登录窗体完成,服务器端维护BUG修复

This commit is contained in:
dathlin
2017-07-11 17:32:58 +08:00
parent 7873640ede
commit eedc2e5ea4
4 changed files with 277 additions and 18 deletions

View File

@@ -32,7 +32,7 @@
<TextBlock x:Name="TextBlockSoftName" TextAlignment="Center" Style="{DynamicResource MaterialDesignTitleTextBlock}">[您的软件系统]</TextBlock>
</materialDesign:Card>
<Grid Margin="100,8,8,8">
<Grid Margin="8,8,8,8">
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="auto"></RowDefinition>
@@ -43,30 +43,32 @@
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<materialDesign:PackIcon Grid.Row="0" Grid.Column="0" Kind="Account" Foreground="{Binding ElementName=NameTextBox, Path=BorderBrush}" />
<TextBox Grid.Row="0" Grid.Column="1"
<materialDesign:PackIcon Grid.Row="0" Grid.Column="0" Kind="Account" Foreground="{Binding ElementName=NameTextBox, Path=BorderBrush}"
Margin="92,4,0,0"/>
<TextBox Grid.Row="0" Grid.Column="1" Margin="0,2,0,2"
x:Name="NameTextBox" KeyDown="NameTextBox_KeyDown"
materialDesign:HintAssist.Hint="请输入名称">
</TextBox>
<materialDesign:PackIcon Grid.Row="1" Grid.Column="0" Kind="Key" Foreground="{Binding ElementName=PasswordBox, Path=BorderBrush}" HorizontalAlignment="Right" />
<PasswordBox Grid.Row="1" Grid.Column="1"
<materialDesign:PackIcon Grid.Row="1" Grid.Column="0" Kind="Key" Foreground="{Binding ElementName=PasswordBox, Path=BorderBrush}" HorizontalAlignment="Right"
Margin="92,4,0,0"/>
<PasswordBox Grid.Row="1" Grid.Column="1" Margin="0,2,0,2"
x:Name="PasswordBox" KeyDown="PasswordBox_KeyDown"
materialDesign:HintAssist.Hint="请输入密码" />
<Button x:Name="LoginButton" Grid.Column="2" Grid.RowSpan="2" VerticalAlignment="Center" Style="{StaticResource MaterialDesignRaisedLightButton}" Width="100" Height="35"
Click="Button_Click">
登录系统
</Button>
<StackPanel Grid.Row="2" Grid.ColumnSpan="3" Orientation="Horizontal" Margin="92,8,8,8">
<ToggleButton x:Name="Remember" Style="{StaticResource MaterialDesignSwitchToggleButton}" VerticalAlignment="Center"
ToolTip="单击记住密码" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked"/>
<TextBlock Grid.Column="1" Style="{StaticResource Caption}">是否记住密码?</TextBlock>
</StackPanel>
</Grid>
<StackPanel Orientation="Horizontal" Margin="100,2,8,8">
<ToggleButton x:Name="Remember" Style="{StaticResource MaterialDesignSwitchToggleButton}" VerticalAlignment="Center"
ToolTip="单击记住密码" Checked="ToggleButton_Checked" Unchecked="ToggleButton_Unchecked"/>
<TextBlock Grid.Column="1" Style="{StaticResource Caption}">是否记住密码?</TextBlock>
</StackPanel>
<TextBlock x:Name="WindowToolTip" TextAlignment="Center" Visibility="Hidden" Margin="0,10,0,0" Style="{StaticResource Caption}">正在连接服务器...</TextBlock>
<TextBlock x:Name="WindowToolTip" TextAlignment="Center" Visibility="Visible" Margin="0,3,0,0" Style="{StaticResource Caption}">正在连接服务器...</TextBlock>
</StackPanel>

View File

@@ -11,6 +11,12 @@ using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Threading;
using HslCommunication;
using CommonLibrary;
using HslCommunication.BasicFramework;
using Newtonsoft.Json.Linq;
namespace Wpf
{
@@ -26,7 +32,257 @@ namespace 软件系统客户端Wpf
private void Button_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
//启动线程登录
//验证输入
if (string.IsNullOrEmpty(NameTextBox.Text))
{
SetInformationString("请输入用户名");
NameTextBox.Focus();
return;
}
if (string.IsNullOrEmpty(PasswordBox.Password))
{
SetInformationString("请输入密码");
PasswordBox.Focus();
return;
}
SetInformationString("正在验证维护状态...");
UISettings(false);
UserName = NameTextBox.Text;
UserPassword = PasswordBox.Password;
IsChecked = (bool)Remember.IsChecked;
ThreadAccountLogin = new Thread(ThreadCheckAccount);
ThreadAccountLogin.IsBackground = true;
ThreadAccountLogin.Start();
}
#region
private string UserName = string.Empty;
private string UserPassword = string.Empty;
private bool IsChecked = false;
/// <summary>
/// 用于验证的后台线程
/// </summary>
private Thread ThreadAccountLogin = null;
/// <summary>
/// 用户账户验证的后台端
/// </summary>
private void ThreadCheckAccount()
{
//定义委托
Action<string> message_show = delegate (string message)
{
SetInformationString(message);
};
Action start_update = delegate
{
//需要该exe支持否则将无法是实现自动版本控制
string update_file_name = AppDomain.CurrentDomain.BaseDirectory + @"\软件自动更新.exe";
try
{
System.Diagnostics.Process.Start(update_file_name);
Environment.Exit(0);//退出系统
}
catch
{
MessageBox.Show("更新程序启动失败,请检查文件是否丢失,联系管理员获取。");
}
};
Action thread_finish = delegate
{
UISettings(true);
};
//延时
Thread.Sleep(200);
//请求指令头数据,该数据需要更具实际情况更改
OperateResultString result = UserClient.Net_simplify_client.ReadFromServer(CommonHeadCode.SimplifyHeadCode.);
if (result.IsSuccess)
{
byte[] temp = Encoding.Unicode.GetBytes(result.Content);
//例如返回结果为1说明允许登录0则说明服务器处于维护中并将信息显示
if (result.Content != "1")
{
Dispatcher.Invoke(message_show, result.Content.Substring(1));
Dispatcher.Invoke(thread_finish);
return;
}
}
else
{
//访问失败
Dispatcher.Invoke(message_show, result.Message);
Dispatcher.Invoke(thread_finish);
return;
}
//检查账户
Dispatcher.Invoke(message_show, "正在检查账户...");
//延时
Thread.Sleep(200);
//===================================================================================
// 根据实际情况校验,选择数据库校验或是将用户名密码发至服务器校验
// 以下展示了服务器校验的方法如您需要数据库校验请删除下面并改成SQL访问验证的方式
//包装数据
JObject json = new JObject
{
{ UserAccount.UserNameText, new JValue(UserName) },
{ UserAccount.PasswordText, new JValue(UserPassword) }
};
result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode., json.ToString());
if (result.IsSuccess)
{
//服务器应该返回账户的信息
UserAccount account = JObject.Parse(result.Content).ToObject<UserAccount>();
if (!account.LoginEnable)
{
//不允许登录
Dispatcher.Invoke(message_show, account.ForbidMessage);
Dispatcher.Invoke(thread_finish);
return;
}
UserClient.UserAccount = account;
}
else
{
//访问失败
Dispatcher.Invoke(message_show, result.Message);
Dispatcher.Invoke(thread_finish);
return;
}
//登录成功,进行保存用户名称和密码
UserClient.JsonSettings.LoginName = UserName;
UserClient.JsonSettings.Password = IsChecked ? UserPassword : "";
UserClient.JsonSettings.LoginTime = DateTime.Now;
UserClient.JsonSettings.SaveToFile();
//版本验证
Dispatcher.Invoke(message_show, "正在验证版本...");
//延时
Thread.Sleep(200);
result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
if (result.IsSuccess)
{
//服务器应该返回服务器的版本号
SystemVersion sv = new SystemVersion(result.Content);
//系统账户跳过低版本检测
if (UserClient.UserAccount.UserName != "admin")
{
if (UserClient.CurrentVersion != sv)
{
//保存新版本信息
UserClient.JsonSettings.IsNewVersionRunning = true;
UserClient.JsonSettings.SaveToFile();
//和当前系统版本号不一致,启动更新
Dispatcher.Invoke(start_update);
return;
}
}
else
{
if (UserClient.CurrentVersion < sv)
{
//保存新版本信息
UserClient.JsonSettings.IsNewVersionRunning = true;
UserClient.JsonSettings.SaveToFile();
//和当前系统版本号不一致,启动更新
Dispatcher.Invoke(start_update);
return;
}
}
}
else
{
//访问失败
Dispatcher.Invoke(message_show, result.Message);
Dispatcher.Invoke(thread_finish);
return;
}
//================================================================================
//验证结束后,根据需要是否下载服务器的数据,或是等到进入主窗口下载也可以
//如果有参数决定主窗口的显示方式,那么必要在下面向服务器请求数据
//以下展示了初始化参数的功能
Dispatcher.Invoke(message_show, "正在下载参数...");
//延时
Thread.Sleep(200);
result = UserClient.Net_simplify_client.ReadFromServer(CommonLibrary.CommonHeadCode.SimplifyHeadCode.);
if (result.IsSuccess)
{
//服务器返回初始化的数据,此处进行数据的提取,有可能包含了多个数据
json = Newtonsoft.Json.Linq.JObject.Parse(result.Content);
//例如公告数据
UserClient.Announcement = SoftBasic.GetValueFromJsonObject(json, nameof(UserClient.Announcement), "");
}
else
{
//访问失败
Dispatcher.Invoke(message_show, result.Message);
Dispatcher.Invoke(thread_finish);
return;
}
//启动主窗口
Dispatcher.Invoke(new Action(() =>
{
DialogResult = true;
return;
}));
}
#endregion
private void UISettings(bool enable)
{
NameTextBox.IsEnabled = enable;
PasswordBox.IsEnabled = enable;
Remember.IsEnabled = enable;
LoginButton.IsEnabled = enable;
}
private void SetInformationString(string str)
{
if (WindowToolTip.Opacity == 1)
{
DoubleAnimation hidden = new DoubleAnimation(1, 0, TimeSpan.FromMilliseconds(100));
hidden.Completed += delegate
{
DoubleAnimation show = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(100));
WindowToolTip.Text = str;
WindowToolTip.BeginAnimation(OpacityProperty, show);
};
WindowToolTip.BeginAnimation(OpacityProperty, hidden);
}
else
{
DoubleAnimation show = new DoubleAnimation(0, 1, TimeSpan.FromMilliseconds(100));
WindowToolTip.Text = str;
WindowToolTip.BeginAnimation(OpacityProperty, show);
}
}
private void ToggleButton_Checked(object sender, RoutedEventArgs e)
@@ -41,6 +297,7 @@ namespace 软件系统客户端Wpf
private void Window_Loaded(object sender, RoutedEventArgs e)
{
WindowToolTip.Opacity = 0;
TextBlockSoftName.Text = CommonLibrary.Resource.StringResouce.SoftName;
TextBlockSoftVersion.Text = UserClient.CurrentVersion.ToString();
TextBlockSoftCopyright.Text = $"本软件著作权归{CommonLibrary.Resource.StringResouce.SoftCopyRight}所有";
@@ -63,12 +320,12 @@ namespace 软件系统客户端Wpf
private void NameTextBox_KeyDown(object sender, KeyEventArgs e)
{
PasswordBox.Focus();
if(e.Key==Key.Enter) PasswordBox.Focus();
}
private void PasswordBox_KeyDown(object sender, KeyEventArgs e)
{
Button_Click(null, new RoutedEventArgs());
if (e.Key == Key.Enter) Button_Click(null, new RoutedEventArgs());
}
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB