| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using UAS_MES_NEW.PublicMethod;
- using UAS_Tools_HY.PublicMethods;
- namespace UAS_MES_Tools
- {
- public partial class Login : Form
- {
- public Login()
- {
- InitializeComponent();
- }
- public Login(bool isFirst)
- {
- InitializeComponent();
- isLogin = isFirst;
- }
- public string _Account, _Password;
- public bool isLogin = true;
- DataTable dt;
- private void Login_Load(object sender, EventArgs e)
- {
- showMsg.Text = "";
- Thread LoadDBThread = new Thread(() =>
- {
- string result = LoadDB();
- if (showMsg.InvokeRequired)
- {
- showMsg.Invoke(new Action(() =>
- {
- showMsg.Text = result;
- LoginIN.Enabled = true;
- }));
- }
- });
- LoadDBThread.Start();
- LoginIN.Enabled = false;
- if (isLogin)
- {
- Account.Text = BaseUtil.GetCacheData("Account").ToString();
- Password.Text = BaseUtil.GetCacheData("Password").ToString();
- }
- }
- private void Account_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode != Keys.Enter) return;
- Password.Focus();
- Password.SelectAll();
- }
- private void Password_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode != Keys.Enter) return;
- LoginIN.Focus();
- }
- private void LoginIN_Click(object sender, EventArgs e)
- {
- _Account = Account.Text;
- _Password = Password.Text;
- DateTime now = DateTime.Now;
- string formattedTime = now.ToString("HH:mm:ss");
- string ErrorMessage;
- if (CheckUserLogin(Account.Text, Password.Text, "N_MES_HY", out ErrorMessage))
- {
- if (isLogin)
- {
- BaseUtil.SetCacheData("Account", Account.Text);
- BaseUtil.SetCacheData("Password", Password.Text);
- this.DialogResult = DialogResult.OK;
- }
- else
- {
- dt = ConnectDB.ExecuteSelect($"select * from employee where em_code = :em_code and em_type = 'admin'",
- new Dictionary<string, object> {
- { "em_code", Account.Text }
- });
- if (dt.Rows.Count > 0)
- {
- this.DialogResult = DialogResult.Yes;
- }
- else
- {
- this.DialogResult = DialogResult.No;
- }
- }
- showMsg.Text = $"{formattedTime} 登录成功";
- LogManager.DoLog("登陆成功" + Account.Text + " " + Password.Text);
- this.Close();
- this.Dispose();
- }
- else
- {
- showMsg.Text = $"{formattedTime} {ErrorMessage}";
- }
- }
- public static bool CheckUserLogin(string iUserCode, string iPassWord, string Master, out string oErrorMessage)
- {
- oErrorMessage = "";
- try
- {
- string url = "http://erp.ubtob.net:11764/mes/" + "mobile/login.action";//html调用的地址
- HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
- if (webrequest == null)
- {
- return false;
- }
- webrequest.Method = "POST";
- webrequest.Timeout = 1000;
- webrequest.ContentType = "application/x-www-form-urlencoded";
- System.Collections.Hashtable pars = new System.Collections.Hashtable();
- pars.Add("username", iUserCode);
- pars.Add("password", iPassWord);
- pars.Add("master", Master);
- string buffer = "";
- //发送POST数据
- if (!(pars == null || pars.Count == 0))
- {
- foreach (string key in pars.Keys)
- {
- buffer = buffer + "&" + key + "=" + pars[key].ToString();
- }
- byte[] data = Encoding.UTF8.GetBytes(buffer);
- using (Stream stream = webrequest.GetRequestStream())
- {
- stream.Write(data, 0, data.Length);
- }
- }
- string[] values = webrequest.Headers.GetValues("Content-Type");
- WebResponse myResponse = webrequest.GetResponse();
- using (Stream resStream = myResponse.GetResponseStream())
- {
- StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
- string Content = newReader.ReadToEnd();
- Dictionary<string, object> dic = new Dictionary<string, object>();
- dic = BaseUtil.ToDictionary(Content);
- if (!dic.ContainsKey("erpaccount"))
- {
- oErrorMessage = dic["reason"].ToString();
- return false;
- }
- newReader.Close();
- }
- }
- catch (Exception ex)
- {
- LogManager.DoLog(ex.Message.ToString());
- }
- return true;
- }
- private string LoadDB()
- {
- try
- {
- if (ConnectDB.TestConnection())
- {
- return $"数据库连接成功";
- }
- else
- {
- return $"数据库连接失败";
- }
- }
- catch (Exception ex)
- {
- return $"{ex.Message}";
- }
- }
- }
- }
|