Login.cs 6.4 KB

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