using System;
using System.Windows.Forms;
using System.Data;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
using UAS_MES_NEW.PublicMethod;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Drawing;
using UAS_MES_NEW.Properties;
namespace UAS_MES_NEW
{
public partial class Login : Form
{
DataHelper dh;
//用来保存combox中的数据库信息
DataTable DbComboxData;
//用来保存其他信息
DataTable UserInf;
//注销的时候会重新生成这边变量,所以需要用static
static LogicHandler lgc;
int Close_Size = 24;
Thread LoadMasterInf;
public Login()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
//拦截双击标题栏、移动窗体的系统消息
if (m.Msg != 0xA3)
{
base.WndProc(ref m);
}
}
private void Login_Load(object sender, EventArgs e)
{
BaseUtil.FormStepInOrOut(this, true);
//设置默认数据库
SystemInf.DefaultDB = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
SystemInf.ProcessesID = System.Diagnostics.Process.GetCurrentProcess().Id;
CheckForIllegalCrossThreadCalls = false;
LoadMasterInf = new Thread(LoadMaster);
LoadMasterInf.Start();
//显示上次用户登录的用户名,将输入框定位到密码
UserName.Text = BaseUtil.GetCacheData("LastLoginUser").ToString();
Source.Text = BaseUtil.GetCacheData("LastLoginResource").ToString();
if (UserName.Text != "")
PassWord.Select();
else
UserName.Select();
BaseUtil.SetFormCenter(this);
}
private void LoadMaster()
{
try
{
dh = new DataHelper();
//将账套的信息查询出来赋给下拉框
DbComboxData = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_user||'#'||ma_inneraddress db_user from master", "select");
DB.DataSource = DbComboxData;
DB.DisplayMember = "ma_user";
DB.ValueMember = "db_user";
DB.Text = BaseUtil.GetCacheData("LastLoginSob").ToString();
LoginButton.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//点击登陆按钮的时候
private void LoginButton_Click(object sender, EventArgs e)
{
if (DB.SelectedValue == null)
BaseUtil.ShowError("请选择一个数据库!");
//设置选中数据库的链接
string DBUser = DB.SelectedValue.ToString().Split('#')[0];
string Address = DB.SelectedValue.ToString().Split('#')[1];
DataHelper.DBConnectionString = "Password=select!#%*(;User ID=" + DBUser + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=172.30.80.23)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
SystemInf.CurrentDB = DBUser;
//设置屏幕的宽高信息
SystemInf.ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
SystemInf.ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;
//重新执行一遍构造函数使得重置之后的链接字符串生效
dh = new DataHelper();
SystemInf.dh = dh;
string ErrorMessage = "";
//验证用户名和密码
if (LogicHandler.CheckUserLogin(UserName.Text, PassWord.Text, out ErrorMessage))
{
//验证岗位资源,通过验证开启主窗体
if (LogicHandler.CheckUserAndResourcePassed(UserName.Text, Source.Text, out ErrorMessage))
{
SetInf();//设置User的全局变量
this.Hide();
//记录登陆信息
try
{
IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
string IPAddress = "";
for (int i = 0; i < IpEntry.AddressList.Length; i++)
{
if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
IPAddress = IpEntry.AddressList[i].ToString();
}
string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
LogicHandler.RecordLogInfo(User.UserCode, User.UserName, version, "CS", IPAddress);
}
catch (Exception ex)
{
LogManager.DoLog(ex.Message);
}
LogManager.DoLog("登陆成功" + User.UserName + " " + User.UserAccountType);
Main dialog = new Main();
dialog.ShowDialog();
this.Close();
}
else
MessageBox.Show(ErrorMessage);
}
else
MessageBox.Show(ErrorMessage);
}
///
///设置User类的全局变量,类似于Session
///
private void SetInf()
{
//设置全局用户信息
string[] field = { "em_code,em_depart,em_type,em_name,em_position,em_professname,em_type,ug_code,ug_name" };
UserInf = dh.getFieldsDataByCondition("Employee left join cs$userresource on ur_emcode=em_code left join cs$usergroup on ug_code=ur_groupcode", field, "upper(em_code)='" + UserName.Text.ToUpper() + "'");
DataTable dt = dh.getFieldsDataByCondition("source left join step on sc_stepcode=st_code", new string[] { "st_badgroupcode", "sc_wccode", "sc_code", "sc_stepcode", "sc_stepname", "sc_linecode" }, "upper(sc_code)='" + Source.Text.ToUpper() + "'");
//存在岗位资源则要求添加
if (dt.Rows.Count > 0)
{
User.CurrentStepCode = dt.Rows[0]["sc_stepcode"].ToString();
User.CurrentStepName = dt.Rows[0]["sc_stepname"].ToString();
User.DefaultBadGroup = dt.Rows[0]["st_badgroupcode"].ToString();
User.UserLineCode = dt.Rows[0]["sc_linecode"].ToString();
User.UserSourceCode = dt.Rows[0]["sc_code"].ToString();
User.WorkCenter = dt.Rows[0]["sc_wccode"].ToString();
}
//通过Source表关联岗位资源编号查询出当前的执行工序
User.UserCode = UserInf.Rows[0]["em_code"].ToString();
User.UserDepartment = UserInf.Rows[0]["em_depart"].ToString();
User.UserName = UserInf.Rows[0]["em_name"].ToString();
User.UserPosition = UserInf.Rows[0]["em_position"].ToString();
User.UserProfessName = UserInf.Rows[0]["em_professname"].ToString();
User.UserAccountType = UserInf.Rows[0]["em_type"].ToString();
User.UserGroupCode = UserInf.Rows[0]["ug_code"].ToString();
User.UserGroup = UserInf.Rows[0]["ug_name"].ToString();
//设置系统基础信息
UserInf = (DataTable)dh.ExecuteSql("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as systime from dual", "select");
SystemInf.LoginTime = UserInf.Rows[0]["systime"].ToString();
//记录在本地用户成功登录的用户名
BaseUtil.SetCacheData("LastLoginUser", UserName.Text);
BaseUtil.SetCacheData("LastLoginSob", DB.Text);
BaseUtil.SetCacheData("LastLoginResource", Source.Text);
}
private void ForGetPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MessageBox.Show("请联系管理员修改密码");
}
//用Enter键调用登陆的点击事件
private void Source_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
LoginButton.PerformClick();
}
private void PassWord_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
Source.Focus();
}
///
/// 导入证书
///
private void CertImport()
{
X509Certificate2 cert = new X509Certificate2(Application.StartupPath + @"\Cert\深圳市优软科技有限公司.pfx", "uasmes");
if (cert != null)
{
var store2 = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store2.Open(OpenFlags.ReadWrite);
if (!store2.Certificates.Contains(cert))
{
store2.Add(cert);
}
var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
if (!store.Certificates.Contains(cert))
{
store.Add(cert);
}
}
}
private void Login_FormClosing(object sender, FormClosingEventArgs e)
{
BaseUtil.FormStepInOrOut(this, false);
try
{
dh.Dispose();
}
catch (Exception)
{
}
}
private void UserName_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
PassWord.Focus();
}
private void Login_Paint(object sender, PaintEventArgs e)
{
Bitmap image = Resources.dialog_close_24px_1173781_easyicon_net;
Rectangle rect = this.ClientRectangle;
//画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
//使用图片
Bitmap bt = new Bitmap(image);
Point p5 = new Point(Width - Close_Size - 4, 4);
e.Graphics.DrawImage(bt, p5);
}
}
private Point offset;
private void Login_MouseDown(object sender, MouseEventArgs e)
{
if (MouseButtons.Left != e.Button) return;
Point cur = this.PointToScreen(e.Location);
offset = new Point(cur.X - this.Left, cur.Y - this.Top);
Rectangle rect = this.ClientRectangle;
rect.Offset(Width - Close_Size - 4, 4);
rect.Width = Close_Size;
rect.Height = Close_Size;
int x = e.X, y = e.Y;
bool isClose = x > rect.X && x < rect.Right && y > rect.Y && y < rect.Bottom;
if (isClose)
Close();
}
private void Login_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Left != e.Button) return;
Point cur = MousePosition;
this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
}
}
}