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 QMAPP.ServicesAgent.LoginService; using System.Configuration; using System.ServiceModel.Configuration; using QMAPP.WinForm.Common; namespace QMAPP.WinForm { /// /// 登录窗体 /// public partial class LoginForm : Form { QMAPP.ServicesAgent.LoginService.LoginServiceClient _loginClient = new ServicesAgent.LoginService.LoginServiceClient(); public static LoginInfo loginUser; Dictionary users = new Dictionary(); public LoginForm() { try { InitializeComponent(); //checkBox1.Visible = false; //checkBox1.Checked = true; //加载用户名 string userInfofile = ConfigurationManager.AppSettings["UserInfo"].ToString(); System.IO.FileStream fs = new System.IO.FileStream(userInfofile, System.IO.FileMode.OpenOrCreate); if (fs.Length > 0) { System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); users = bf.Deserialize(fs) as Dictionary; foreach (QMAPP.ServicesAgent.LoginService.LoginInfo user in users.Values) { comboBox1.Items.Add(user.LoginUserID); } for (int i = 0; i < users.Count; i++) { if (comboBox1.Text != "") { if (users.ContainsKey(comboBox1.Text)) { this.txtPassWord.Text = users[comboBox1.Text].PassWord; //checkBox1.Checked = true; } } } if (comboBox1.Items.Count > 0) { comboBox1.SelectedIndex = 0; //checkBox1.Checked = true; } } fs.Close(); //if (!users.Keys.Contains("JG1")) //{ // users.Add("JG1", new LoginInfo { LoginUserID="JG1",PassWord="1" }); // comboBox1.Items.Add("JG1"); //} } catch (Exception ex) { throw ex; } } #region 登录 private void btnLogin_Click(object sender, EventArgs e) { try { if (this.comboBox1.Text == "") { MessageBox.Show("用户名不能为空!"); return; } if (this.txtPassWord.Text == "") { MessageBox.Show("密码不能为空!"); return; } //登录验证 loginUser = _loginClient.IsLogin(new QMAPP.ServicesAgent.LoginService.LoginInfo { LoginUserID = this.comboBox1.Text, PassWord = this.txtPassWord.Text, UserDes= this.txtPassWord.Text } , ClientContext.GetCredentialInfo()); if (loginUser == null) { MessageBox.Show("用户名或密码输入不正确!"); return; } //存储用户名以及密码 if (this.checkBox1.Checked) { users.Clear(); string userInfofile = ConfigurationManager.AppSettings["UserInfo"].ToString(); System.IO.FileStream fs = new System.IO.FileStream(userInfofile, System.IO.FileMode.Create); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); loginUser.PassWord = this.txtPassWord.Text; if (users.Keys.Contains(loginUser.LoginUserID)) { users[loginUser.LoginUserID] = loginUser; } else { users.Add(loginUser.LoginUserID, loginUser); } bf.Serialize(fs, users); fs.Close(); } ClientContext.LoginInfo = loginUser; //打开主窗体 MainForm form = new MainForm(loginUser); form.Show(); this.Hide(); } catch (Exception ex) { throw ex; } } private void Login(string userid, string pwd) { try { pwd = QMFrameWork.Common.Encrypt.DESEncrypt.Decrypt(pwd); //登录验证 loginUser = _loginClient.IsLogin(new QMAPP.ServicesAgent.LoginService.LoginInfo { LoginUserID = userid, PassWord = pwd, UserDes = "" } , ClientContext.GetCredentialInfo()); if (loginUser == null) { MessageBox.Show("登录码不正确或已过期,请联系管理员更换!"); return; } ClientContext.LoginInfo = loginUser; //打开主窗体 MainForm form = new MainForm(loginUser); form.Show(); this.Hide(); } catch (System.Security.Cryptography.CryptographicException) { MessageBox.Show("登录码格式不正确!"); } catch (System.FormatException) { MessageBox.Show("登录码格式不正确!"); } catch (Exception ex) { throw ex; } } #endregion #region 取消 private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } #endregion private void LoginForm_Load(object sender, EventArgs e) { LoadServerHost(); comboBox1.Focus(); comboBox1.SelectAll(); txtPassWord.SelectAll(); } private void LoadServerHost() { Configuration cfg = ConfigurationManager.OpenExeConfiguration(""); var clientSecion = cfg.GetSection("system.serviceModel/client") as ClientSection; lblServerHost.Text = GetEndointByName(clientSecion, "BasicHttpBinding_IGeneralService").Address.Host; } private ChannelEndpointElement GetEndointByName(ClientSection section, string endpointname) { foreach (ChannelEndpointElement endpoint in section.Endpoints) { if (string.Equals(endpoint.Name, endpointname)) { return endpoint; } } return null; } #region 输入控制 private void txtUserName_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { if (comboBox1.Text.StartsWith("<") && comboBox1.Text.EndsWith(">")) { var passport = comboBox1.Text.Trim('<', '>').Split(new string[]{"::"},StringSplitOptions.RemoveEmptyEntries); if (passport.Length == 2) { string userid = passport[0]; string pwd = passport[1]; comboBox1.Text = ""; txtPassWord.Text = ""; Login(userid, pwd); return; } } txtPassWord.SelectAll(); txtPassWord.Focus(); e.Handled = true; } } private void txtPassWord_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { this.btnLogin_Click(this.btnLogin, new EventArgs()); } } #endregion private void comboBox1_SelectedValueChanged(object sender, EventArgs e) { if (users.ContainsKey(comboBox1.Text) && users[comboBox1.Text].PassWord != "") { this.txtPassWord.Text = users[comboBox1.Text].PassWord; //checkBox1.Checked = true; } } private void comboBox1_MouseDoubleClick(object sender, MouseEventArgs e) { MessageBox.Show("1"); } DateTime lastDownTime = DateTime.Now; private void comboBox1_MouseClick(object sender, MouseEventArgs e) { //TimeSpan sp = DateTime.Now - lastDownTime; //if (sp.Milliseconds <= 500) //{ // lastDownTime = DateTime.Now; // QMAPP.WinForm.Forms.Common.TouchKey key = new Forms.Common.TouchKey((ComboBox)sender); // key.ShowDialog(); //} } int clickcounter = 0; private void lblServerHost_DoubleClick(object sender, EventArgs e) { clickcounter += 1; if (clickcounter >= 3) { if (MessageBox.Show("您希望切换WCF服务器吗?\r\n点击“确定”,程序重启后将进入服务器切换界面;\r\n点击“取消”,不执行任何操作。", "服务器切换" , MessageBoxButtons.OKCancel , MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK) { LocalSetting.Settings["SwitchServer"] = "1"; } clickcounter = 0; } } private void LoginForm_DoubleClick(object sender, EventArgs e) { comboBox1.SelectAll(); comboBox1.Focus(); } } }