EPLAN_PROD_Plugin/Sinvo.EplanHpD.Plugin.WPFUI/View/LoginWindow.xaml.cs

242 lines
7.9 KiB
C#

using EPLAN.Harness.Core.Settings;
using Newtonsoft.Json;
using Sinvo.EplanHpD.Plugin.Service;
using Sinvo.EplanHpD.Plugin.WPFUI.Models;
using Sinvo.EplanHpD.Plugin.WPFUI.ViewModel;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
namespace Sinvo.EplanHpD.Plugin.WPFUI.View
{
/// <summary>
/// LoginWindow.xaml 的交互逻辑
/// </summary>
public partial class LoginWindow : Window
{
/// <summary>
/// 展开更多的时候,窗口的高度增加的固定值
/// </summary>
private double expandFixedHeight = 100;
private LoginViewModel viewModel;
public LoginWindow()
{
InitializeComponent();
this.DataContext = viewModel = new LoginViewModel();
}
/// <summary>
/// 强制显示为模态窗口
/// </summary>
public new void Show()
{
this.ShowDialog();
}
#region
private void Expander_Expanded(object sender, RoutedEventArgs e)
{
if (sender is Expander expander)
{
this.Height += expandFixedHeight;
}
}
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
if (sender is Expander expander)
{
this.Height -= expandFixedHeight;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_ = LoadData();
}
private async Task LoadData()
{
var userInfoJson = FormUISettings.Instance.GetValue("UserInfo") as string;
var user = JsonConvert.DeserializeObject<UserLoginModel>(userInfoJson);
if (user != null && !string.IsNullOrEmpty(user.UserName))
{
viewModel.User = user;
viewModel.IsRemember = true;
await this.Dispatcher.BeginInvoke(() =>
{
this.PasswordTxt.Password = user.UserPassword;
LoadGroupAndRole();
});
}
}
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
this.viewModel.User.UserPassword = PasswordTxt.Password;
if (string.IsNullOrWhiteSpace(this.viewModel.User.UserName))
{
this.msgTxt.Text = "请输入用户名!";
this.msgTxt.Foreground = System.Windows.Media.Brushes.Red;
return;
}
if (string.IsNullOrWhiteSpace(this.viewModel.User.UserPassword))
{
this.msgTxt.Text = "请输入密码!";
this.msgTxt.Foreground = System.Windows.Media.Brushes.Red;
return;
}
if (string.IsNullOrWhiteSpace(this.groupCbx.SelectedValue as string))
{
this.msgTxt.Text = "请选择部门!";
this.msgTxt.Foreground = System.Windows.Media.Brushes.Red;
return;
}
if (string.IsNullOrWhiteSpace(this.roleCbx.SelectedValue as string))
{
this.msgTxt.Text = "请选择角色!";
this.msgTxt.Foreground = System.Windows.Media.Brushes.Red;
return;
}
viewModel.User.UserGroup = this.groupCbx.SelectedValue as string;
viewModel.User.UserRole = this.roleCbx.SelectedValue as string;
//try
//{
var result = viewModel.Login();
if(result != null && !string.IsNullOrEmpty(result.GUID))
{
PluginServices.user = new Service.Model.UserInfo
{
Group = viewModel.User.UserGroup,
Role = viewModel.User.UserRole,
//ServerUrl = viewModel.User.ServerUrl,
GUID = result.GUID,
ID = result.ID,
Name = result.Name
};
if (viewModel.IsRemember)
{
FormUISettings.Instance.SetValue("UserInfo", JsonConvert.SerializeObject(viewModel.User));
FormUISettings.Instance.SaveSingleton();
}
else
{
FormUISettings.Instance.SetValue("UserInfo", "{}");
FormUISettings.Instance.SaveSingleton();
}
this.DialogResult = true;
this.Close();
}
else
{
MessageBox.Show("登录失败,请检查用户名和密码是否正确!");
}
// if (result)
// {
// //MessageBox.Show("")
// var serverItem = this.serverCbx.SelectedItem as ServerItemModel;
// //new UserInfo().ShowInfo(viewModel.User.userNickName, serverItem);
// //EplSession.Current.CurServer = serverItem;
// EplSession.Current.SetUser(viewModel.User);
// EplSession.Current.Config.CurrentServer = serverItem;
// EplSession.Current.Config.RememberAccount = this.rememberChk.IsChecked ?? false;
// if (this.rememberChk.IsChecked ?? false)
// {
// viewModel.RememberUser();
// }
// else
// {
// viewModel.ForgetUser();
// }
// // 登录成功,弹出用户信息小窗口
// WindowRouter.ShowWindow("UserInfo", keepAlive: true);
// this.DialogResult = true;
// this.Close();
// }
//}
//catch (System.Exception ex)
//{
// // ERROR 是扩展方法,用于输出错误信息
// ex.ToString().Error();
// //MessageBox.Show(ex.Message);
// msgTxt.Text = ex.Message;
//}
}
private void UsernameTxt_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(UsernameTxt.Text))
{
return;
}
viewModel.User.UserName = UsernameTxt.Text;
LoadGroupAndRole();
}
private void cancelBtn_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private async void groupCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
viewModel.User.UserGroup = this.groupCbx.SelectedValue as string;
await viewModel.GetRolesByCurrentSelect();
if (viewModel.Roles?.Count > 0)
{
await this.Dispatcher.BeginInvoke(() =>
{
this.roleCbx.SelectedIndex = 0;
});
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void serverCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//var serverItem = this.serverCbx.SelectedItem as ServerItemModel;
//if (serverItem != null)
//{
// viewModel.ChangeService(serverItem);
// LoadGroupAndRole();
//}
}
private void LoadGroupAndRole()
{
var task = viewModel.GetUserGroup();
task.ContinueWith(x =>
{
this.Dispatcher.InvokeAsync(() =>
{
this.groupCbx.SelectedIndex = 0;
});
});
}
}
}