using ButcherFactory.BO.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WinFormControl; namespace ButcherFactory.Login { public partial class Login : Form { public Login() { InitializeComponent(); #if DEBUG pwdBox.Text = "123"; #endif try { userNameBox.Text = AppContext.Worker.Name; if (!string.IsNullOrEmpty(AppContext.Worker.Name)) pwdBox.Focus(); } catch { } } private Point mousePoint = new Point(); private void Login_MouseDown(object sender, MouseEventArgs e) { this.mousePoint.X = e.X; this.mousePoint.Y = e.Y; } private void Login_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Top = Control.MousePosition.Y - mousePoint.Y; this.Left = Control.MousePosition.X - mousePoint.X; } } private async void loginBtn_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(AppContext.ConnectInfo.SqlConnection)) throw new Exception("请设置数据库地址!"); var username = userNameBox.Text.Trim(); var pwd = pwdBox.Text; if (string.IsNullOrEmpty(username)) throw new Exception("请输入用户名"); LoginUtil.InitRpcFacade(); if (LoginUtil.TestConnection(1000)) { await Task.Factory.StartNew(() => LoginUtil.Login(username, pwd)); } else { if (AppContext.Worker.Name != username || string.Join("", LoginUtil.EncodePwd(pwd)) != string.Join("", AppContext.Worker.Password)) throw new Exception("离线时只能使用上次登录信息登录"); } var form = FormUtil.CreateFrom(); if (form == null) throw new Exception("权限不符"); form.FormClosing += delegate { SubFormClosing(); }; form.Show(); Hide(); } void SubFormClosing() { foreach (Form form in Application.OpenForms) { if (form is Login) { form.Show(); return; } } } private void exitBtn_Click(object sender, EventArgs e) { Application.Exit(); } private void pictureBox1_Click(object sender, EventArgs e) { if (new SettingForm().ShowDialog() == DialogResult.OK) LoginUtil.ReInitRpcFacade(); } private void userNameBox_Click(object sender, EventArgs e) { LoginUtil.InitRpcFacade(); var keyBoard = new NumberPad(); if (keyBoard.ShowDialog() == true) { if (!LoginUtil.TestConnection(1000)) { if (string.IsNullOrEmpty(AppContext.Worker.Name)) throw new Exception("请检查网络"); throw new Exception("离线时无法切换用户"); } userNameBox.Text = LoginUtil.GetWorkerNameByCode(keyBoard.Result); } } } }