Login.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. using UAS_MES_NEW.PublicMethod;
  14. using UAS_Tools_HY.PublicMethods;
  15. namespace UAS_MES_Tools
  16. {
  17. public partial class Login : Form
  18. {
  19. public Login()
  20. {
  21. InitializeComponent();
  22. }
  23. public Login(bool isFirst)
  24. {
  25. InitializeComponent();
  26. isLogin = isFirst;
  27. }
  28. public string _Account, _Password;
  29. public bool isLogin = true;
  30. DataTable dt;
  31. private void Login_Load(object sender, EventArgs e)
  32. {
  33. showMsg.Text = "";
  34. Thread LoadDBThread = new Thread(() =>
  35. {
  36. string result = LoadDB();
  37. if (showMsg.InvokeRequired)
  38. {
  39. showMsg.Invoke(new Action(() =>
  40. {
  41. showMsg.Text = result;
  42. LoginIN.Enabled = true;
  43. }));
  44. }
  45. });
  46. LoadDBThread.Start();
  47. LoginIN.Enabled = false;
  48. if (isLogin)
  49. {
  50. Account.Text = BaseUtil.GetCacheData("Account").ToString();
  51. Password.Text = BaseUtil.GetCacheData("Password").ToString();
  52. }
  53. }
  54. private void Account_KeyDown(object sender, KeyEventArgs e)
  55. {
  56. if (e.KeyCode != Keys.Enter) return;
  57. Password.Focus();
  58. Password.SelectAll();
  59. }
  60. private void Password_KeyDown(object sender, KeyEventArgs e)
  61. {
  62. if (e.KeyCode != Keys.Enter) return;
  63. LoginIN.Focus();
  64. }
  65. private void LoginIN_Click(object sender, EventArgs e)
  66. {
  67. _Account = Account.Text;
  68. _Password = Password.Text;
  69. DateTime now = DateTime.Now;
  70. string formattedTime = now.ToString("HH:mm:ss");
  71. string ErrorMessage;
  72. if (CheckUserLogin(Account.Text, Password.Text, "N_MES_HY", out ErrorMessage))
  73. {
  74. if (isLogin)
  75. {
  76. BaseUtil.SetCacheData("Account", Account.Text);
  77. BaseUtil.SetCacheData("Password", Password.Text);
  78. this.DialogResult = DialogResult.OK;
  79. }
  80. else
  81. {
  82. dt = ConnectDB.ExecuteSelect($"select * from employee where em_code = :em_code and em_type = 'admin'",
  83. new Dictionary<string, object> {
  84. { "em_code", Account.Text }
  85. });
  86. if (dt.Rows.Count > 0)
  87. {
  88. this.DialogResult = DialogResult.Yes;
  89. }
  90. else
  91. {
  92. this.DialogResult = DialogResult.No;
  93. }
  94. }
  95. showMsg.Text = $"{formattedTime} 登录成功";
  96. LogManager.DoLog("登陆成功" + Account.Text + " " + Password.Text);
  97. this.Close();
  98. this.Dispose();
  99. }
  100. else
  101. {
  102. showMsg.Text = $"{formattedTime} {ErrorMessage}";
  103. }
  104. }
  105. public static bool CheckUserLogin(string iUserCode, string iPassWord, string Master, out string oErrorMessage)
  106. {
  107. oErrorMessage = "";
  108. try
  109. {
  110. string url = "http://erp.ubtob.net:11764/mes/" + "mobile/login.action";//html调用的地址
  111. HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
  112. if (webrequest == null)
  113. {
  114. return false;
  115. }
  116. webrequest.Method = "POST";
  117. webrequest.Timeout = 1000;
  118. webrequest.ContentType = "application/x-www-form-urlencoded";
  119. System.Collections.Hashtable pars = new System.Collections.Hashtable();
  120. pars.Add("username", iUserCode);
  121. pars.Add("password", iPassWord);
  122. pars.Add("master", Master);
  123. string buffer = "";
  124. //发送POST数据
  125. if (!(pars == null || pars.Count == 0))
  126. {
  127. foreach (string key in pars.Keys)
  128. {
  129. buffer = buffer + "&" + key + "=" + pars[key].ToString();
  130. }
  131. byte[] data = Encoding.UTF8.GetBytes(buffer);
  132. using (Stream stream = webrequest.GetRequestStream())
  133. {
  134. stream.Write(data, 0, data.Length);
  135. }
  136. }
  137. string[] values = webrequest.Headers.GetValues("Content-Type");
  138. WebResponse myResponse = webrequest.GetResponse();
  139. using (Stream resStream = myResponse.GetResponseStream())
  140. {
  141. StreamReader newReader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
  142. string Content = newReader.ReadToEnd();
  143. Dictionary<string, object> dic = new Dictionary<string, object>();
  144. dic = BaseUtil.ToDictionary(Content);
  145. if (!dic.ContainsKey("erpaccount"))
  146. {
  147. oErrorMessage = dic["reason"].ToString();
  148. return false;
  149. }
  150. newReader.Close();
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. LogManager.DoLog(ex.Message.ToString());
  156. }
  157. return true;
  158. }
  159. private string LoadDB()
  160. {
  161. try
  162. {
  163. if (ConnectDB.TestConnection())
  164. {
  165. return $"数据库连接成功";
  166. }
  167. else
  168. {
  169. return $"数据库连接失败";
  170. }
  171. }
  172. catch (Exception ex)
  173. {
  174. return $"{ex.Message}";
  175. }
  176. }
  177. }
  178. }