Login.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Data;
  4. using UAS_MES.DataOperate;
  5. using UAS_MES.Entity;
  6. using UAS_MES.PublicMethod;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Net;
  9. using System.Net.Sockets;
  10. using System.Threading;
  11. using System.Drawing.Drawing2D;
  12. using System.Drawing;
  13. using UAS_MES.Properties;
  14. using System.Globalization;
  15. using System.Diagnostics;
  16. using System.Text.RegularExpressions;
  17. namespace UAS_MES
  18. {
  19. public partial class Login : Form
  20. {
  21. DataHelper dh;
  22. //用来保存combox中的数据库信息
  23. DataTable DbComboxData;
  24. //用来保存其他信息
  25. DataTable UserInf;
  26. //注销的时候会重新生成这边变量,所以需要用static
  27. static LogicHandler lgc;
  28. int Close_Size = 24;
  29. Thread LoadMasterInf;
  30. public Login()
  31. {
  32. InitializeComponent();
  33. }
  34. protected override void WndProc(ref Message m)
  35. {
  36. //拦截双击标题栏、移动窗体的系统消息
  37. if (m.Msg != 0xA3)
  38. {
  39. base.WndProc(ref m);
  40. }
  41. }
  42. private void Login_Load(object sender, EventArgs e)
  43. {
  44. BaseUtil.FormStepInOrOut(this, true);
  45. //设置默认数据库
  46. SystemInf.DefaultDB = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  47. SystemInf.ProcessesID = System.Diagnostics.Process.GetCurrentProcess().Id;
  48. CheckForIllegalCrossThreadCalls = false;
  49. LoadMasterInf = new Thread(LoadMaster);
  50. LoadMasterInf.Start();
  51. //显示上次用户登录的用户名,将输入框定位到密码
  52. UserName.Text = BaseUtil.GetCacheData("LastLoginUser").ToString();
  53. Source.Text = BaseUtil.GetCacheData("LastLoginResource").ToString();
  54. if (UserName.Text != "")
  55. PassWord.Select();
  56. else
  57. UserName.Select();
  58. BaseUtil.SetFormCenter(this);
  59. }
  60. private void LoadMaster()
  61. {
  62. try
  63. {
  64. dh = new DataHelper();
  65. //将账套的信息查询出来赋给下拉框
  66. DbComboxData = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_user||'#'||ma_inneraddress db_user from master", "select");
  67. DB.DataSource = DbComboxData;
  68. DB.DisplayMember = "ma_user";
  69. DB.ValueMember = "db_user";
  70. DB.Text = BaseUtil.GetCacheData("LastLoginSob").ToString();
  71. LoginButton.Enabled = true;
  72. //获取数据库时间与系统当前时间比较
  73. //string nowtime = dh.getFieldDataByCondition("dual", "to_char(sysdate,'yyyy-MM-dd HH24:mm:ss')", "1=1").ToString();
  74. //string sysdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  75. ////系统当前时间如果小于数据库时间则更新至数据库时间
  76. //if (nowtime.CompareTo(sysdate) > 0)
  77. //{
  78. // //取得数据库时间
  79. // DateTime dt;
  80. // DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
  81. // dtFormat.ShortDatePattern = "yyyy-MM-dd hh:mm:ss";
  82. // dt = Convert.ToDateTime(nowtime, dtFormat);
  83. // //转换System.DateTime到SYSTEMTIME
  84. // SYSDATE.SYSTEMTIME st = new SYSDATE.SYSTEMTIME();
  85. // st.FromDateTime(dt);
  86. // //调用Win32 API设置系统时间
  87. // SYSDATE.Win32API.SetLocalTime(ref st);
  88. //}
  89. }
  90. catch (Exception ex)
  91. {
  92. MessageBox.Show(ex.Message);
  93. }
  94. }
  95. //点击登陆按钮的时候
  96. private void LoginButton_Click(object sender, EventArgs e)
  97. {
  98. if (DB.SelectedValue == null)
  99. BaseUtil.ShowError("请选择一个数据库!");
  100. //设置选中数据库的链接
  101. string DBUser = DB.SelectedValue.ToString().Split('#')[0];
  102. string Address = DB.SelectedValue.ToString().Split('#')[1];
  103. DataHelper.DBConnectionString = "Password=select!#%*(;User ID=" + DBUser + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST="+Address+")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  104. //DataHelper.DBConnectionString = "Password=select!#%*(;User ID=" + DBUser + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=220.161.94.175)(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  105. SystemInf.CurrentDB = DBUser;
  106. //设置屏幕的宽高信息
  107. SystemInf.ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
  108. SystemInf.ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;
  109. //重新执行一遍构造函数使得重置之后的链接字符串生效
  110. dh = new DataHelper();
  111. SystemInf.dh = dh;
  112. string ErrorMessage = "";
  113. SystemInf.GetRelation = false;
  114. //验证用户名和密码
  115. if (LogicHandler.CheckUserLogin(UserName.Text, PassWord.Text, out ErrorMessage))
  116. {
  117. //验证岗位资源,通过验证开启主窗体
  118. if (LogicHandler.CheckUserAndResourcePassed(UserName.Text, Source.Text, out ErrorMessage))
  119. {
  120. SetInf();//设置User的全局变量
  121. this.Hide();
  122. //记录登陆信息
  123. try
  124. {
  125. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  126. string IPAddress = "";
  127. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  128. {
  129. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  130. IPAddress = IpEntry.AddressList[i].ToString();
  131. }
  132. string machineName = Environment.MachineName;
  133. string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  134. int CSCOUNT = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length;
  135. LogicHandler.RecordLogInfo(User.UserCode, User.UserName, version, "CS", IPAddress, machineName, CSCOUNT.ToString());
  136. }
  137. catch (Exception ex)
  138. {
  139. LogManager.DoLog(ex.Message);
  140. }
  141. LogManager.DoLog("登陆成功" + User.UserName + " " + User.UserAccountType);
  142. Main dialog = new Main();
  143. dialog.ShowDialog();
  144. this.Close();
  145. }
  146. else
  147. MessageBox.Show(ErrorMessage);
  148. }
  149. else
  150. MessageBox.Show(ErrorMessage);
  151. }
  152. /// <summary>
  153. ///设置User类的全局变量,类似于Session
  154. /// </summary>
  155. private void SetInf()
  156. {
  157. //设置全局用户信息
  158. string[] field = { "em_code,em_depart,em_id,em_type,em_name,em_position,em_professname,em_type,ug_code,ug_name" };
  159. UserInf = dh.getFieldsDataByCondition("Employee left join cs$userresource on ur_emcode=em_code left join cs$usergroup on ug_code=ur_groupcode", field, "upper(em_code)='" + UserName.Text.ToUpper() + "'");
  160. DataTable dt = dh.getFieldsDataByCondition("source left join step on sc_stepcode=st_code", new string[] { "st_badgroupcode", "sc_wccode", "sc_code", "sc_stepcode", "sc_stepname", "sc_linecode" }, "upper(sc_code)='" + Source.Text.ToUpper() + "'");
  161. //存在岗位资源则要求添加
  162. if (dt.Rows.Count > 0)
  163. {
  164. User.CurrentStepCode = dt.Rows[0]["sc_stepcode"].ToString();
  165. User.CurrentStepName = dt.Rows[0]["sc_stepname"].ToString();
  166. User.DefaultBadGroup = dt.Rows[0]["st_badgroupcode"].ToString();
  167. User.UserLineCode = dt.Rows[0]["sc_linecode"].ToString();
  168. User.UserSourceCode = dt.Rows[0]["sc_code"].ToString();
  169. User.WorkCenter = dt.Rows[0]["sc_wccode"].ToString();
  170. }
  171. //通过Source表关联岗位资源编号查询出当前的执行工序
  172. User.UserID = UserInf.Rows[0]["em_id"].ToString();
  173. User.UserCode = UserInf.Rows[0]["em_code"].ToString();
  174. User.UserDepartment = UserInf.Rows[0]["em_depart"].ToString();
  175. User.UserName = UserInf.Rows[0]["em_name"].ToString();
  176. User.UserPosition = UserInf.Rows[0]["em_position"].ToString();
  177. User.UserProfessName = UserInf.Rows[0]["em_professname"].ToString();
  178. User.UserAccountType = UserInf.Rows[0]["em_type"].ToString();
  179. User.UserGroupCode = UserInf.Rows[0]["ug_code"].ToString();
  180. User.UserGroup = UserInf.Rows[0]["ug_name"].ToString();
  181. //设置系统基础信息
  182. UserInf = (DataTable)dh.ExecuteSql("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as systime from dual", "select");
  183. SystemInf.LoginTime = UserInf.Rows[0]["systime"].ToString();
  184. //记录在本地用户成功登录的用户名
  185. BaseUtil.SetCacheData("LastLoginUser", UserName.Text);
  186. BaseUtil.SetCacheData("LastLoginSob", DB.Text);
  187. BaseUtil.SetCacheData("LastLoginResource", Source.Text);
  188. }
  189. private void ForGetPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  190. {
  191. MessageBox.Show("请联系管理员修改密码");
  192. }
  193. /// <summary>
  194. /// 导入证书
  195. /// </summary>
  196. private void CertImport()
  197. {
  198. X509Certificate2 cert = new X509Certificate2(Application.StartupPath + @"\Cert\深圳市优软科技有限公司.pfx", "uasmes");
  199. if (cert != null)
  200. {
  201. var store2 = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  202. store2.Open(OpenFlags.ReadWrite);
  203. if (!store2.Certificates.Contains(cert))
  204. {
  205. store2.Add(cert);
  206. }
  207. var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
  208. store.Open(OpenFlags.ReadWrite);
  209. if (!store.Certificates.Contains(cert))
  210. {
  211. store.Add(cert);
  212. }
  213. }
  214. }
  215. private void Login_FormClosing(object sender, FormClosingEventArgs e)
  216. {
  217. BaseUtil.FormStepInOrOut(this, false);
  218. try
  219. {
  220. dh.Dispose();
  221. }
  222. catch (Exception)
  223. {
  224. }
  225. }
  226. private void Login_Paint(object sender, PaintEventArgs e)
  227. {
  228. Bitmap image = Resources.dialog_close_24px_1173781_easyicon_net;
  229. Rectangle rect = this.ClientRectangle;
  230. //画关闭符号
  231. using (Pen objpen = new Pen(Color.Black))
  232. {
  233. //使用图片
  234. Bitmap bt = new Bitmap(image);
  235. Point p5 = new Point(Width - Close_Size - 4, 4);
  236. e.Graphics.DrawImage(bt, p5);
  237. }
  238. }
  239. private Point offset;
  240. private void Login_MouseDown(object sender, MouseEventArgs e)
  241. {
  242. if (MouseButtons.Left != e.Button) return;
  243. Point cur = this.PointToScreen(e.Location);
  244. offset = new Point(cur.X - this.Left, cur.Y - this.Top);
  245. Rectangle rect = this.ClientRectangle;
  246. rect.Offset(Width - Close_Size - 4, 4);
  247. rect.Width = Close_Size;
  248. rect.Height = Close_Size;
  249. int x = e.X, y = e.Y;
  250. bool isClose = x > rect.X && x < rect.Right && y > rect.Y && y < rect.Bottom;
  251. if (isClose)
  252. Close();
  253. }
  254. private void Login_MouseMove(object sender, MouseEventArgs e)
  255. {
  256. if (MouseButtons.Left != e.Button) return;
  257. Point cur = MousePosition;
  258. this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
  259. }
  260. private void Login_KeyDown(object sender, KeyEventArgs e)
  261. {
  262. if (e.KeyCode == Keys.Enter)
  263. {
  264. string pattern = @"\(.+,.+\)";
  265. if (Regex.IsMatch(((TextBox)sender).Text, pattern))
  266. {
  267. try
  268. {
  269. Match match = Regex.Match(((TextBox)sender).Text, pattern);
  270. String message = match.Groups[0].Value;
  271. string[] str = message.Remove(message.Length - 1).Remove(0, 1).Split(',');
  272. UserName.Text = str[0];
  273. PassWord.Text = str[1];
  274. LoginButton.PerformClick();
  275. }
  276. catch {
  277. MessageBox.Show("扫码登陆数据解析错误,请检查条码内容");
  278. }
  279. }
  280. else
  281. {
  282. switch (((TextBox)sender).Name)
  283. {
  284. case "UserName":
  285. PassWord.Focus();
  286. break;
  287. case "PassWord":
  288. Source.Focus();
  289. break;
  290. case "Source":
  291. LoginButton.PerformClick();
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. private void UserName_Enter(object sender, EventArgs e)
  300. {
  301. ((TextBox)sender).SelectAll();
  302. }
  303. private void UserName_MouseClick(object sender, MouseEventArgs e)
  304. {
  305. TextBox tx = sender as TextBox;
  306. tx.Tag = 1;
  307. tx.SelectAll();
  308. }
  309. private void UserName_Leave(object sender, EventArgs e)
  310. {
  311. TextBox tx = sender as TextBox;
  312. tx.Tag = 0;
  313. }
  314. }
  315. }