You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
8.6 KiB
C#

using CommonFunc;
using CommonFunc.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.Core.Converters;
using XGL.Data;
using XGL.FormItem;
using XGL.Models;
namespace XGL
{
/// <summary>
/// LoginPage.xaml 的交互逻辑
/// </summary>
public partial class LoginPage : Window
{
private BackgroundWorker bw = new BackgroundWorker();
private UserLogin ul;
Main main;
public static LoginPage loginpage;
string UserCarNo = "";
public LoginPage()
{
InitializeComponent();
CommonFunc.Common.ReadConfig();//读取配置文件
bw.WorkerReportsProgress = true;
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgessChanged);//进度改变事件
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);//进度完成事件
//this.Hide();
//Main mainWindow = new Main();
//mainWindow.Show();
}
private void btnlogin_Click(object sender, RoutedEventArgs e)
{
try
{
//this.dldh.IsActive = true;//遮罩层可见
this.dldh.Visibility = Visibility.Visible;//遮罩层可见
zzdl.Visibility = Visibility.Visible;
ul = new UserLogin
{
username = username.Text.Trim(),
pwd = userpwd.Password.Trim(),
IsChecked = rememberpwd.IsChecked
};
main = new Main();
bw.RunWorkerAsync();
}
catch (Exception ex)
{
LogHelper.instance.log.Error( "登陆异常:" + ex.Message);
}
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
JudgeLogin();
}
private int LoginStatus = 0;
private int JudgeLogin()
{
DateTime dt1 = DateTime.Now;
try
{
if (string.IsNullOrEmpty(ul.username))
{
LoginStatus = -3;
return LoginStatus;
}
if (string.IsNullOrEmpty(ul.pwd))
{
LoginStatus = -2;
return LoginStatus;
}
userDB usdb = new userDB();
bw.ReportProgress(1);
bool islogin = usdb.Login(ul.username, ul.pwd);
if (islogin)
{
if (ul.IsChecked == true)
{
Common.IniWriteValue("登录明细", "loginUser", ul.username);
Common.IniWriteValue("登录明细", "loginpwd", ul.pwd);
}
else
{
Common.IniWriteValue("登录明细", "loginUser", "");
Common.IniWriteValue("登录明细", "loginpwd", "");
}
bw.ReportProgress(3);
//LoadAreaTree();
LoginStatus = 200;
//MainWindow main = new MainWindow();
//main.Show();
//this.Close();
}
else
{
LoginStatus = -1;
//MessageBox.Show("用户名或密码错误", "提示");
}
}
catch (Exception ex)
{
LoginStatus = -5;
LogHelper.instance.log.Error("登录异常:" + ex.Message);
}
DateTime dt2 = DateTime.Now;
TimeSpan ts1 = dt2.Subtract(dt1);
LogHelper.instance.log.Info("登录时间差:" + ts1.Seconds);
return LoginStatus;
}
private void bgWorker_ProgessChanged(object sender, ProgressChangedEventArgs e)
{
if (e.ProgressPercentage == 1)
{
zzdl.Content = "校验用户";
}
if (e.ProgressPercentage == 2)
{
zzdl.Content = "正在加载菜单";
}
if (e.ProgressPercentage == 3)
{
zzdl.Content = "校验成功,正在登陆";
}
}
void bw_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
switch (LoginStatus)
{
case 200:
main.Show();
this.Close();
break;
case 0:
CustomMessageBox.Show("登录失败", CustomMessageBoxIcon.Error);
//this.Dispatcher.Invoke(
//new Action(() => { zzdl.Content = "登录失败"; zzdl.Foreground = Brushes.Red; }
//),
//System.Windows.Threading.DispatcherPriority.Render) ;
break;
case -1:
//this.Dispatcher.Invoke(
//new Action(() => { zzdl.Content = "账号或密码错误"; zzdl.Foreground = Brushes.Red; }
//),
//System.Windows.Threading.DispatcherPriority.Render);
CustomMessageBox.Show("账号或密码错误", CustomMessageBoxIcon.Error);
break;
case -2:
//zzdl.Content = "请输入密码";
//zzdl.Foreground = Brushes.Red;
CustomMessageBox.Show("请输入密码", CustomMessageBoxIcon.Error);
break;
case -3:
//zzdl.Content = "请输入账号";
//zzdl.Foreground = Brushes.Red;
CustomMessageBox.Show("请输入账号", CustomMessageBoxIcon.Error);
break;
case -4:
//zzdl.Content = "加载菜单异常,请重试";
//zzdl.Foreground = Brushes.Red;
CustomMessageBox.Show("加载菜单异常,请重试", CustomMessageBoxIcon.Error);
break;
case -5:
// this.Dispatcher.Invoke(
//new Action(() => zzdl.Content = "登录异常,请重试"
//),
//System.Windows.Threading.DispatcherPriority.Render);
CustomMessageBox.Show("登录异常,请重试", CustomMessageBoxIcon.Error);
break;
}
//dldh.IsActive = false;
this.dldh.Visibility = Visibility.Hidden;//遮罩层可见
zzdl.Visibility = Visibility.Collapsed;
}
private void btnloginout_Click(object sender, RoutedEventArgs e)
{
Environment.Exit(0);
Application.Current.Shutdown();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
username.Focus();
//dldh.IsActive = false;
this.dldh.Visibility = Visibility.Hidden;//遮罩层可见
zzdl.Visibility = Visibility.Collapsed;
string name = Common.IniReadValue("登录明细", "loginUser");
username.Text = name;
string pwd = Common.IniReadValue("登录明细", "loginpwd");
userpwd.Password = pwd;
}
catch (Exception ex)
{
MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();
Common.Log.Error(method.DeclaringType.FullName + "-" + method.Name + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();//窗体可移动
}
private void btnCheckUpdate_Click(object sender, RoutedEventArgs e)
{
}
}
public class UserLogin
{
public string username { get; set; }
public string pwd { get; set; }
public bool? IsChecked { get; set; }
}
}