using CommonFunc;
using CommonFunc.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Input;
using XGL.Data;
using XGL.Data.DBService;
using XGL.Models;
using XGLFinishPro.FormItem;
namespace XGLFinishPro
{
///
/// LoginPage.xaml 的交互逻辑
///
public partial class LoginPage : Window
{
userDB usdb = new userDB();
FormingMachineService formingMachineService = new FormingMachineService();
private BackgroundWorker bw = new BackgroundWorker();
private UserLogin ul;
Main main;
public static LoginPage loginpage;
string UserCarNo = "";
List listShift;
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();
InitVersion();
//InitTeamInfo();
InitShiftInfo();
}
private void InitShiftInfo()
{
DataTable dt = formingMachineService.GetShiftList();
if (dt == null)
{
CustomMessageBox.Show("没有查询到班次信息,请联系管理员!", CustomMessageBoxIcon.Warning);
return;
}
listShift = Utils.ToDataList(dt);
//comboDryRoomInfo.SelectedValuePath = "quipName";
//comboDryRoomInfo.DisplayMemberPath = "quipCode";
//comboDryRoomInfo.ItemsSource = listEquip;
//绑定
comboBoxShift.ItemsSource = listShift;
comboBoxShift.SelectedValuePath = "shift_id";
comboBoxShift.DisplayMemberPath = "shift_desc";
}
//private void InitTeamInfo()
//{
// DataTable dt = formingMachineService.GetTeamList();
// if (dt == null)
// {
// CustomMessageBox.Show("没有查询到班组信息,请联系管理员!", CustomMessageBoxIcon.Warning);
// return;
// }
// List listTeam = Utils.ToDataList(dt);
// //绑定
// comboBoxTeam.ItemsSource = listTeam;
// comboBoxTeam.SelectedValuePath = "team_id";
// comboBoxTeam.DisplayMemberPath = "team_desc";
//}
private void InitVersion()
{
string fileName = "vsion.txt"; // 指定要操作的文件名
string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 构建完整的文件路径
string fileContent = File.ReadAllText(filePath);
lbVersion.Content = fileContent;
LoginUser.Version = fileContent;
}
private void btnlogin_Click(object sender, RoutedEventArgs e)
{
try
{
//this.dldh.IsActive = true;//遮罩层可见
this.dldh.Visibility = Visibility.Visible;//遮罩层可见
zzdl.Visibility = Visibility.Visible;
//if (comboBoxTeam.SelectedValue == null || string.IsNullOrEmpty(comboBoxTeam.SelectedValue.ToString()))
//{
// CustomMessageBox.Show("登录失败,请选择班组", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
// comboBoxTeam.Focus();
// return;
//}
if (comboBoxShift.SelectedValue == null || string.IsNullOrEmpty(comboBoxShift.SelectedValue.ToString()))
{
CustomMessageBox.Show("登录失败,请选择班次", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
comboBoxShift.Focus();
return;
}
if (string.IsNullOrEmpty(datepickerWorkDate.Text))
{
CustomMessageBox.Show("登录失败,请选择工作日期", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
datepickerWorkDate.Focus();
return;
}
var lst = listShift.FindLast(t => t.shift_id == Convert.ToInt32(comboBoxShift.SelectedValue.ToString()));
ul = new UserLogin
{
username = username.Text.Trim(),
pwd = userpwd.Password.Trim(),
IsChecked = rememberpwd.IsChecked,
//teamID = comboBoxTeam.SelectedValue.ToString(),
shiftID = comboBoxShift.SelectedValue.ToString(),
workDate = datepickerWorkDate.Text,
startTime = lst.Shift_Start_Time,
endTime = lst.Shift_End_Time
};
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;
}
bw.ReportProgress(1);
bool islogin = usdb.Login(ul.username, ul.pwd, ul.shiftID,ul.teamID,ul.workDate, ul.startTime, ul.endTime);
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("登录失败", CustomMessageBoxButton.OK, 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("账号或密码错误", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -2:
//zzdl.Content = "请输入密码";
//zzdl.Foreground = Brushes.Red;
CustomMessageBox.Show("请输入密码", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -3:
//zzdl.Content = "请输入账号";
//zzdl.Foreground = Brushes.Red;
CustomMessageBox.Show("请输入账号", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -4:
//zzdl.Content = "加载菜单异常,请重试";
//zzdl.Foreground = Brushes.Red;
CustomMessageBox.Show("加载菜单异常,请重试", CustomMessageBoxButton.OK, CustomMessageBoxIcon.Error);
break;
case -5:
// this.Dispatcher.Invoke(
//new Action(() => zzdl.Content = "登录异常,请重试"
//),
//System.Windows.Threading.DispatcherPriority.Render);
CustomMessageBox.Show("登录异常,请重试", CustomMessageBoxButton.OK, 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)
{
userDB usdb = new userDB();
var sresu = usdb.vsion();
string fileName = "vsion.txt"; // 指定要操作的文件名
string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 构建完整的文件路径
if (File.Exists(filePath))
{
string fileContent = File.ReadAllText(filePath);
if (sresu != fileContent)
{
if (MessageBox.Show("检测到有新版本,是否更新?", "温馨提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning) == MessageBoxResult.OK)
{
// 设置要启动的应用程序名称和参数
string appName = "WPFupdate.exe"; // 替换为你的目标控制台应用程序的名称
string appArguments = sresu;// 替换为你的应用程序需要的参数
// 创建进程启动信息
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = appName,
Arguments = appArguments,
RedirectStandardOutput = false, // 可以选择是否重定向标准输出
UseShellExecute = false, // 必须设置为false,以便在控制台中启动应用程序
CreateNoWindow = false // 设置为true,以便在后台启动应用程序
};
// 创建并启动进程
Process process = new Process
{
StartInfo = startInfo
};
process.Start();
Environment.Exit(0);
}
}
else
{
MessageBox.Show("没有检测到新版本", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
else
{
MessageBox.Show("没有找到版本文件", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
Console.WriteLine("文件不存在: " + filePath);
}
}
}
public class UserLogin
{
public string username { get; set; }
public string pwd { get; set; }
public bool? IsChecked { get; set; }
public string teamID { get; set; }
public string shiftID { get; set; }
public string workDate { get; set; }
public string startTime { get; set; }
public string endTime { get; set; }
}
}