using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Lskj.Control; using Lskj.Business.Impl; using Lskj.Core; using Lskj.Business; using Lskj.Data; using Lskj.Model; using Lskj.Control.Model; using DevExpress.LookAndFeel; using Lskj.Util; using Lskj.AutoUpdate; using System.Net; using System.Xml; using System.Data.SqlClient; using System.IO; namespace Lskj.ProductPlatform2 { public partial class FrmLogin : BaseForm { private string MesLoginConfig = "MesLoginConfig.ini"; private string MesUserInputBefore = "P_MesLoginBefore "; private DataTable mUserTable, mLedgerTable, mWorkTable; private static string Logobg = @"Mes\logo.png"; //用户下拉框数据源 private DataTable _userTable; public FrmLogin() { InitializeComponent(); this.txtUserName.TextChanged += new EventHandler(OnTxtUserNameLostFocus); //this.txtUserName.LostFocus += new EventHandler(OnTxtUserNameLostFocus); } /// /// 说明:设置帐套信息 /// 创建人:龚宇超 /// 创建日期:2017-08-09 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetLedgers() { // 获取帐套信息 this.mUserTable = MainImpl.GetLedgerList(); cbLedger.Properties.Items.Clear(); if (this.mUserTable.Rows.Count > 0) { foreach (DataRow item in this.mUserTable.Rows) { cbLedger.Properties.Items.Add(item["ShowName"]); } if (string.IsNullOrEmpty(DBConfig.Instance.DataBook)) { } cbLedger.Text = string.IsNullOrEmpty(DBConfig.Instance.DataBook) || this.mUserTable.Rows.Cast().FirstOrDefault(x => DBConfig.Instance.DataBook.Equals(x["ShowName"] + "")) == null ? this.mUserTable.Rows[0]["ShowName"] + "" : DBConfig.Instance.DataBook; } else { cbLedger.Enabled = false; } } /// /// 说明:设置工作车间 /// 创建人:龚宇超 /// 创建日期:2019-07-29 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetCJ() { try { this.mWorkTable = MainImpl.MesGetCJ(); foreach (DataRow item in this.mWorkTable.Rows) { this.cbCJ.Items.Add(item["mc"] + ""); } } catch (Exception) { } } /// /// 说明:设置机台数据 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetJT() { try { DataRow[] rows = this.mWorkTable.Select(string.Format("mc = '{0}'", this.cbCJ.SelectedItem + "")); this.cbJT.Items.Clear(); if (rows != null && rows.Length > 0) { DataTable table = MainImpl.MesGetJT(rows[0]["dm"] + ""); foreach (DataRow item in table.Rows) { this.cbJT.Items.Add(item["mc"] + ""); } } } catch (Exception) { } } /// /// 说明:设置班组数据 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetBZ() { this.cbBZ.Items.Clear(); DataTable tables = MainImpl.MesGetBZ(); this.cbBZ.Items.Add(""); foreach (DataRow item in tables.Rows) { this.cbBZ.Items.Add(item["mc"] + ""); } } /// /// 说明:加载本地信息 /// 创建人:龚宇超 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetLocalCongfig() { this.cbCJ.SelectedItem = IniHelper.Read(MesLoginConfig, "CJ"); this.cbJT.SelectedItem = IniHelper.Read(MesLoginConfig, "JT"); this.cbBZ.SelectedItem = IniHelper.Read(MesLoginConfig, "BZ"); } /// /// 说明:启动自动更新 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void StartAutoUpdate() { if (string.IsNullOrEmpty(SystemInfo.Instance.MesUpdateAddress)) { //MessageUtil.Show(ResourceKeys.UpdateAddressIsNull); return; } bool isUpdateSucess = false; IAutoUpdater updater = new AutoUpdater(SystemInfo.Instance.MesUpdateAddress); try { updater.Update(); isUpdateSucess = true; } catch (WebException ex) { MessageUtil.Show(ResourceKeys.ServerConnectionFault + "\r\n" + ex); } catch (XmlException ex) { MessageUtil.Show(ResourceKeys.DownLoadUpdateXmlFault + "\r\n" + ex); } catch (NotSupportedException ex) { MessageUtil.Show(ResourceKeys.UpdateXmlSettingFault + "\r\n" + ex); } catch (ArgumentException ex) { MessageUtil.Show(ResourceKeys.DownLoadUpdateFileFault + "\r\n" + ex); } catch (Exception ex) { MessageUtil.Show(ResourceKeys.UpdateError + "\r\n" + ex); LogHelper.Instance.WriteError(ex); } finally { if (!isUpdateSucess) { try { updater.RollBack(); } catch (Exception) { } } } } /// /// 说明:设置delphi参数 /// 创建人:龚宇超 /// 创建日期:2018-03-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetDelphiDllParams() { try { StringBuilder sb1 = new StringBuilder(); sb1.Append(ERPInfo.Instance.UserName); StringBuilder sb2 = new StringBuilder(); sb2.Append(ERPInfo.Instance.WindowName); StringBuilder sb3 = new StringBuilder(); sb3.Append(ERPInfo.Instance.SkinName); //给delphi库设置参数(用户id、子系统id、操作员名称、计算机名称) DelphiHelper.Delphi_SetParams(Convert.ToInt32(ERPInfo.Instance.UserId), Convert.ToInt32(ERPInfo.Instance.SubSysId), sb1, sb2, sb3); } catch (Exception ex) { //MessageUtil.Show(ex); LogHelper.Instance.WriteError(ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:登陆 /// 创建人:龚宇超 /// 创建日期:2018-11-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void StartLogin() { try { string userCode = this.txtUserName.EditValue + ""; string userName = this.txtUserName.Text.Trim(); string password = this.txtPassword.Text.Trim(); DataTable table = MainImpl.GetEmployeeList(); DataRow[] drRows = table.Select("UserName='" + userName + "' or UserCode='" + userName + "'"); if ((drRows == null) || (drRows.Count() < 1)) { MessageUtil.Show(ResourceKeys.NameOrPasswordError); return; } //找到多个操作员 if (drRows.Count() > 1) { MessageUtil.Show(ResourceKeys.DuplicatedNameError); return; } DataRow drRow = drRows[0]; //DataRow drRow = table.Rows.Cast().FirstOrDefault(x => userName.Equals(x["UserName"] + "")); if (drRow != null) { userCode = drRow["userCode"] + ""; userName = drRow["UserName"] + ""; } else { userCode = ""; } if (string.IsNullOrWhiteSpace(userCode)) { MessageUtil.Show(ResourceKeys.UserNotFound); return; } if (string.IsNullOrEmpty(cbBZ.SelectedItem + "")) { MessageUtil.Show("请选择班次."); return; } if (string.IsNullOrEmpty(userName)) { this.txtUserName.Focus(); MessageUtil.Show(ResourceKeys.UserNameNotNull); return; } if (string.IsNullOrEmpty(this.cbCJ.SelectedItem + "")) { MessageUtil.Show("当前用户未设置车间,请先设置."); return; } // 检查Mac地址 if (SystemInfo.Instance.MacEnabled) { int result = MainImpl.CheckMacAddress(ERPInfo.Instance.MacAddress, userCode); if (result == 0) { MessageUtil.Show(ResourceKeys.LoginFault + ResourceKeys.UnKownMacAddress); return; } if (result == -1) { MessageUtil.Show(ResourceKeys.LoginFault + ResourceKeys.UnKownMacAddressTable); return; } } WaitForm.ShowForm(ResourceKeys.Logining); int loginResult = MainImpl.MesLogin(userCode, password, true); switch (loginResult) { case 0: // 登录成功 case 2: DataRow rowItem = BaseImpl.GetDataRowResult(string.Format("select * from P_employeeTab where loginaccount='{0}'", userCode)); if (rowItem == null) { MessageUtil.Show("工号错误!"); return; } string userId = rowItem["employeeid"] + ""; // 登录成功后调用 string msg = string.Empty; int result = LoginAfter(userId, out msg); if (result == -1) { MessageUtil.Show(msg); return; } string filepath = string.Empty; string paramstr = string.Empty; // 记录本地数据 IniHelper.Write(MesLoginConfig, "CJ", this.cbCJ.SelectedItem + ""); IniHelper.Write(MesLoginConfig, "JT", this.cbJT.SelectedItem + ""); IniHelper.Write(MesLoginConfig, "BZ", this.cbBZ.SelectedItem + ""); IniHelper.Write(MesLoginConfig, "UserName", userCode); // 设置皮肤 DBConfig.Instance.BCName = cbBZ.SelectedItem + ""; DBConfig.Instance.NoticeUserID = userId; DBConfig.Instance.NoticeUserName = userCode; DBConfig.Instance.UpdateSet = true; DBConfig.Instance.SmallClient = false; DBConfig.Instance.DataBook = cbLedger.Text; DBConfig.Instance.WriteConfig(); userName = rowItem["EmployeeName"] + ""; string skinName = MainImpl.GetUserSkin(userId); skinName = string.IsNullOrEmpty(skinName) ? ERPInfo.Instance.SkinName : skinName; UserLookAndFeel.Default.SetSkinStyle(skinName); ERPInfo.Instance.UserId = userId; ERPInfo.Instance.UserName = userName; ERPInfo.Instance.Password = SHAHelper.EncryptPassword(password); ERPInfo.Instance.SkinName = skinName; ERPInfo.Instance.AccountBook = cbLedger.Text; DataRow workItem = this.mWorkTable.Rows.Cast().FirstOrDefault(x => (this.cbCJ.SelectedItem + "").Equals(x["mc"] + "")); if (workItem != null) { ERPInfo.Instance.WorkModid = workItem["dllcoid"] + ""; ERPInfo.Instance.WorkNo = workItem["no"] + ""; } ERPInfo.Instance.WorkCJ = this.cbCJ.SelectedItem + ""; ERPInfo.Instance.WorkJT = this.cbJT.SelectedItem + ""; ERPInfo.Instance.WorkBZ = this.cbBZ.SelectedItem + ""; this.SetDelphiDllParams(); // 设置调用delphi参数 if (!string.IsNullOrEmpty(msg)) { // 返回Msg值:tipmsg=我是提示信息;filepath=ccc.lsp;params=daskd string[] msgs = msg.Split(';'); if (msgs != null && msgs.Length > 2) { msg = msgs[0].Replace("tipmsg=", ""); filepath = msgs[1].Replace("filepath=", ""); paramstr = msgs[2].Replace("params=", ""); } if (!string.IsNullOrEmpty(msg)) MessageUtil.Show(msg); if (filepath.Contains(".dll")) { GetMsgParamters(paramstr); } if (filepath.Contains(".lsp")) { DelphiHelper.LoadDelphiDll(PubUtil.AbsolutelyLibPath + filepath, "", "", paramstr, "", 0, ""); } } bool isStartMain = true; if (isStartMain) { // 检查自动升级 WaitForm.ShowForm(ResourceKeys.AutoUpdating); //this.StartAutoUpdate(); //// 启动小平台 //if (chkClient.Checked) //{ // this.StartSmallClient(); //} this.DialogResult = DialogResult.OK; this.Close(); } break; case 1: // 密码不正确 MessageUtil.Show(ResourceKeys.NameOrPasswordError); break; //case 2: // 已有此用户登录 // MessageUtil.Show(ResourceKeys.AlreadyLogin); // break; case -1: // 登录失败 default: MessageUtil.Show(ResourceKeys.LoginFault); break; } } catch (Exception ex) { LogUtil.WriteError("登录出错!", ex); //MessageUtil.Show("登录出错!" + ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:调用弹出dll /// 创建人:王一帆 /// 创建日期:2020-04-14 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void GetMsgParamters(string msgs) { if (msgs.Contains("module=")) { string[] paramters = msgs.Split('='); if (paramters.Length != 2) { MessageUtil.Show(ResourceKeys.ProcNameSettingFault); return; } string[] defaultParams = paramters[1].Split('^'); if (defaultParams.Length < 4) { MessageUtil.Show(ResourceKeys.ExcuteMoudleFail); return; } string dllName = defaultParams[1]; string moduleTitle = defaultParams[2]; string saveState = defaultParams[3]; if ("lskj.pubadd3.dll".Equals(dllName) || "lskj.pubadd2.dll".Equals(dllName) || "lskj.pubadd4.dll".Equals(dllName)) dllName = "Lskj.PubAdd.dll"; List paramList = new List(); for (int i = 4; i < 14; i++) { paramList.Add(defaultParams.Length > i ? defaultParams[i] : ""); } //执行dll string[] str = { moduleTitle, ERPInfo.Instance.UserId, ERPInfo.Instance.UserName, "1", ERPInfo.Instance.WorkModid, paramList[0], paramList[1], paramList[2], paramList[3], paramList[4], paramList[5], paramList[6], paramList[7], paramList[8], paramList[9], "", "", saveState }; IForm form = FormHelper.LoadDllForm(dllName, str); if (form == null) { MessageUtil.Show(ResourceKeys.DyncmicDllNotFount); return; } if (form.SubForm == null) { MessageUtil.Show(ResourceKeys.DyncmicDllInitFault); return; } form.SubForm.WindowState = FormWindowState.Normal; form.SubForm.StartPosition = FormStartPosition.CenterScreen; form.SubForm.Text = moduleTitle; DialogResult dr = form.SubForm.ShowDialog(); } } /// /// 说明:窗口加载 /// 创建人:龚宇超 /// 创建日期:2018-11-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnLoad(object sender, EventArgs e) { try { string iniPath = Path.Combine(new string[] { PubUtil.AbsolutelyPath, MesLoginConfig }); if (!File.Exists(iniPath)) { File.Create(iniPath); } this.BackgroundImage = getBitmap(); this.cbBZ.SelectedIndex = 0; this.SetUsers(); this.SetLedgers(); this.mUserTable = MainImpl.GetEmployeeList(); this.SetCJ(); this.SetJT(); this.SetBZ(); this.SetLocalCongfig(); if (string.IsNullOrEmpty(this.txtUserName.Text)) { this.txtUserName.Focus(); } else { this.txtPassword.Focus(); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 获取本地资源文件 /// /// /// public static Bitmap getBitmap() { Bitmap bitmap = null; try { FileStream fs = File.OpenRead(PubUtil.BitMapPath + Logobg); //OpenRead int filelength = (int)fs.Length; //获得文件长度 Byte[] image = new Byte[filelength]; //建立一个字节数组 fs.Read(image, 0, filelength); //按字节流读取 Image result = Image.FromStream(fs); fs.Close(); bitmap = new Bitmap(result); } catch (Exception ex) { bitmap = Lskj.ProductPlatform2.Properties.Resources.mesbj; } return bitmap; } /// /// 说明:设置系统用户 /// 创建人:王一帆 /// 创建日期:2020-04-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void SetUsers() { BaseImpl.HasExistsColumn("P_SystemTab", "LoginPopupFlag", "int"); // 设置登录用户信息 _userTable = MainImpl.GetEmployeeList(); txtUserName.ValueField = txtUserName.ValueMember = "UserCode"; txtUserName.TextMember = "UserName"; txtUserName.SourceSQL = "select EmployeeId UserId,LoginAccount UserCode,EmployeeName UserName from P_EmployeeTab WHERE sign=0 and UseFlag=1"; txtUserName.DataSource = _userTable; txtUserName.AutoShowPopup = "1".Equals(BaseImpl.GetResult("SELECT LoginPopupFlag FROM P_SystemTab") + ""); DataRow[] rowItems = _userTable.Select(" UserName = '" + DBConfig.Instance.LoginName + "'"); if (rowItems != null && rowItems.Length > 0) { txtUserName.EditValue = rowItems[0][txtUserName.ValueField] + ""; } } /// /// 说明:服务器设置 /// 创建人:龚宇超 /// 创建日期:2018-11-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnSettingClick(object sender, EventArgs e) { try { FrmConfig config = new FrmConfig(); DialogResult result = config.ShowDialog(); if (result == DialogResult.OK) { SystemInfo.RefreshSystemParam(); } } catch (Exception ex) { //MessageUtil.Show(ex); string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:登陆 /// 创建人:龚宇超 /// 创建日期:2018-11-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnLoginClick(object sender, EventArgs e) { try { this.StartLogin(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:键盘按下 /// 创建人:龚宇超 /// 创建日期:2018-11-12 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. void OnPasswordKeyDown(object sender, KeyEventArgs e) { try { if (e.KeyCode == Keys.Enter) { this.StartLogin(); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 说明:失去焦点获取车间 /// 创建人:龚宇超 /// 创建日期:2019-07-29 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. void OnTxtUserNameLostFocus(object sender, EventArgs e) { try { string userCode = this.txtUserName.EditValue + ""; string userName = this.txtUserName.Text.Trim(); if (!string.IsNullOrEmpty(userCode)) { DataRow rowItem = BaseImpl.GetDataRowResult(string.Format("select * from P_employeeTab where loginaccount='{0}'", userCode)); string userId = rowItem["employeeid"] + ""; DataTable Logindt = SqlHelper.ExecuteDataTable(string.Format(" exec {0} {1},'{2}' ", MesUserInputBefore, userId, userName)); DataRow LoginRow = Logindt.Rows.Count > 0 ? Logindt.Rows[0] : null; ERPInfo.Instance.ClassName = string.Empty; if (LoginRow == null) return; this.cbCJ.SelectedItem = LoginRow["mes_ws_Name"] + ""; this.cbJT.SelectedItem = LoginRow["JtName"] + ""; this.cbBZ.SelectedItem = LoginRow["ClassBc"] + ""; ERPInfo.Instance.ClassName = LoginRow["ClassName"] + ""; } } catch (Exception) { } } private void btnClose_Click(object sender, EventArgs e) { this.Close(); } public int LoginAfter(string opertaorid, out string tipMsg) { try { SqlParameter pMsg = new SqlParameter("@msg", SqlDbType.VarChar, 2000); pMsg.Direction = ParameterDirection.Output; SqlParameter returnValue = new SqlParameter("@return", SqlDbType.Int, 4); returnValue.Direction = ParameterDirection.ReturnValue; SqlParameter[] param = { new SqlParameter("@operatorid",SqlDbType.VarChar), new SqlParameter("@TypeID ",SqlDbType.Int), pMsg, returnValue }; param[0].Value = opertaorid; param[1].Value = cbBZ.SelectedIndex; SqlHelper.ExecuteDataSet(CommandType.StoredProcedure, "P_Login_AfterPr", "P_Login_AfterPr", param); tipMsg = pMsg.Value + ""; return Convert.ToInt32(returnValue.Value + ""); } catch (Exception) { tipMsg = ""; return 0; } } private void cbCJ_SelectedIndexChanged(object sender, EventArgs e) { try { SetJT(); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message,ex.Message); } } /// /// 机台值改变时改变操作人 /// /// /// private void JTSelectedIndexChanged(object sender, EventArgs e) { try { ComboBox comboBox = sender as ComboBox; DataRow comRow = MainImpl.JTGetStaff(comboBox.SelectedItem+""); if (comRow != null) { DataRow[] rowItems = _userTable.Select(" UserName = '" + comRow["JzName"] + "'"); if (rowItems != null && rowItems.Length > 0) { txtUserName.EditValue = rowItems[0][txtUserName.ValueField] + ""; } } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } } }