Login.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Windows.Forms;
  3. using System.Data;
  4. using UAS_MES_NEW.DataOperate;
  5. using UAS_MES_NEW.Entity;
  6. using UAS_MES_NEW.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_NEW.Properties;
  14. namespace UAS_MES_NEW
  15. {
  16. public partial class Login : Form
  17. {
  18. DataHelper dh;
  19. //用来保存combox中的数据库信息
  20. DataTable DbComboxData;
  21. //用来保存其他信息
  22. DataTable UserInf;
  23. //注销的时候会重新生成这边变量,所以需要用static
  24. static LogicHandler lgc;
  25. int Close_Size = 24;
  26. Thread LoadMasterInf;
  27. public Login()
  28. {
  29. InitializeComponent();
  30. }
  31. protected override void WndProc(ref Message m)
  32. {
  33. //拦截双击标题栏、移动窗体的系统消息
  34. if (m.Msg != 0xA3)
  35. {
  36. base.WndProc(ref m);
  37. }
  38. }
  39. private void Login_Load(object sender, EventArgs e)
  40. {
  41. BaseUtil.FormStepInOrOut(this, true);
  42. //设置默认数据库
  43. SystemInf.DefaultDB = Properties.Settings.Default.Properties["MES"].DefaultValue.ToString();
  44. SystemInf.ProcessesID = System.Diagnostics.Process.GetCurrentProcess().Id;
  45. CheckForIllegalCrossThreadCalls = false;
  46. LoadMasterInf = new Thread(LoadMaster);
  47. LoadMasterInf.Start();
  48. //显示上次用户登录的用户名,将输入框定位到密码
  49. UserName.Text = BaseUtil.GetCacheData("LastLoginUser").ToString();
  50. Source.Text = BaseUtil.GetCacheData("LastLoginResource").ToString();
  51. if (UserName.Text != "")
  52. PassWord.Select();
  53. else
  54. UserName.Select();
  55. BaseUtil.SetFormCenter(this);
  56. }
  57. private void LoadMaster()
  58. {
  59. try
  60. {
  61. dh = new DataHelper();
  62. //将账套的信息查询出来赋给下拉框
  63. DbComboxData = (DataTable)dh.ExecuteSql("select ms_pwd,ma_user,ma_user||'#'||ma_inneraddress db_user from master", "select");
  64. DB.DataSource = DbComboxData;
  65. DB.DisplayMember = "ma_user";
  66. DB.ValueMember = "db_user";
  67. DB.Text = BaseUtil.GetCacheData("LastLoginSob").ToString();
  68. LoginButton.Enabled = true;
  69. }
  70. catch (Exception ex)
  71. {
  72. MessageBox.Show(ex.Message);
  73. }
  74. }
  75. //点击登陆按钮的时候
  76. private void LoginButton_Click(object sender, EventArgs e)
  77. {
  78. if (DB.SelectedValue == null)
  79. BaseUtil.ShowError("请选择一个数据库!");
  80. //设置选中数据库的链接
  81. string DBUser = DB.SelectedValue.ToString().Split('#')[0];
  82. string Address = DB.SelectedValue.ToString().Split('#')[1];
  83. //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)));";
  84. DataHelper.DBConnectionString = "Password=select!#%*(;User ID=" + DBUser + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=113.108.109.210)(PORT=1522)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  85. SystemInf.CurrentDB = DBUser;
  86. //设置屏幕的宽高信息
  87. SystemInf.ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
  88. SystemInf.ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;
  89. //重新执行一遍构造函数使得重置之后的链接字符串生效
  90. dh = new DataHelper();
  91. SystemInf.dh = dh;
  92. string ErrorMessage = "";
  93. //验证用户名和密码
  94. if (LogicHandler.CheckUserLogin(UserName.Text, PassWord.Text, out ErrorMessage))
  95. {
  96. //验证岗位资源,通过验证开启主窗体
  97. if (LogicHandler.CheckUserAndResourcePassed(UserName.Text, Source.Text, out ErrorMessage))
  98. {
  99. SetInf();//设置User的全局变量
  100. this.Hide();
  101. //记录登陆信息
  102. try
  103. {
  104. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  105. string IPAddress = "";
  106. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  107. {
  108. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  109. IPAddress = IpEntry.AddressList[i].ToString();
  110. }
  111. string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  112. LogicHandler.RecordLogInfo(User.UserCode, User.UserName, version, "CS", IPAddress);
  113. }
  114. catch (Exception ex)
  115. {
  116. LogManager.DoLog(ex.Message);
  117. }
  118. LogManager.DoLog("登陆成功" + User.UserName + " " + User.UserAccountType);
  119. Main dialog = new Main();
  120. dialog.ShowDialog();
  121. this.Close();
  122. }
  123. else
  124. MessageBox.Show(ErrorMessage);
  125. }
  126. else
  127. MessageBox.Show(ErrorMessage);
  128. }
  129. /// <summary>
  130. ///设置User类的全局变量,类似于Session
  131. /// </summary>
  132. private void SetInf()
  133. {
  134. //设置全局用户信息
  135. string[] field = { "em_code,em_depart,em_type,em_name,em_position,em_professname,em_type,ug_code,ug_name" };
  136. 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() + "'");
  137. 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() + "'");
  138. //存在岗位资源则要求添加
  139. if (dt.Rows.Count > 0)
  140. {
  141. User.CurrentStepCode = dt.Rows[0]["sc_stepcode"].ToString();
  142. User.CurrentStepName = dt.Rows[0]["sc_stepname"].ToString();
  143. User.DefaultBadGroup = dt.Rows[0]["st_badgroupcode"].ToString();
  144. User.UserLineCode = dt.Rows[0]["sc_linecode"].ToString();
  145. User.UserSourceCode = dt.Rows[0]["sc_code"].ToString();
  146. User.WorkCenter = dt.Rows[0]["sc_wccode"].ToString();
  147. }
  148. //通过Source表关联岗位资源编号查询出当前的执行工序
  149. User.UserCode = UserInf.Rows[0]["em_code"].ToString();
  150. User.UserDepartment = UserInf.Rows[0]["em_depart"].ToString();
  151. User.UserName = UserInf.Rows[0]["em_name"].ToString();
  152. User.UserPosition = UserInf.Rows[0]["em_position"].ToString();
  153. User.UserProfessName = UserInf.Rows[0]["em_professname"].ToString();
  154. //User.UserAccountType = UserInf.Rows[0]["em_type"].ToString();
  155. User.UserAccountType = "admin";
  156. User.UserGroupCode = UserInf.Rows[0]["ug_code"].ToString();
  157. User.UserGroup = UserInf.Rows[0]["ug_name"].ToString();
  158. //设置系统基础信息
  159. UserInf = (DataTable)dh.ExecuteSql("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as systime from dual", "select");
  160. SystemInf.LoginTime = UserInf.Rows[0]["systime"].ToString();
  161. //记录在本地用户成功登录的用户名
  162. BaseUtil.SetCacheData("LastLoginUser", UserName.Text);
  163. BaseUtil.SetCacheData("LastLoginSob", DB.Text);
  164. BaseUtil.SetCacheData("LastLoginResource", Source.Text);
  165. }
  166. private void ForGetPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  167. {
  168. MessageBox.Show("请联系管理员修改密码");
  169. }
  170. //用Enter键调用登陆的点击事件
  171. private void Source_KeyDown(object sender, KeyEventArgs e)
  172. {
  173. if (e.KeyCode == Keys.Enter)
  174. LoginButton.PerformClick();
  175. }
  176. private void PassWord_KeyDown(object sender, KeyEventArgs e)
  177. {
  178. if (e.KeyCode == Keys.Enter)
  179. Source.Focus();
  180. }
  181. /// <summary>
  182. /// 导入证书
  183. /// </summary>
  184. private void CertImport()
  185. {
  186. X509Certificate2 cert = new X509Certificate2(Application.StartupPath + @"\Cert\深圳市优软科技有限公司.pfx", "uasmes");
  187. if (cert != null)
  188. {
  189. var store2 = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  190. store2.Open(OpenFlags.ReadWrite);
  191. if (!store2.Certificates.Contains(cert))
  192. {
  193. store2.Add(cert);
  194. }
  195. var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
  196. store.Open(OpenFlags.ReadWrite);
  197. if (!store.Certificates.Contains(cert))
  198. {
  199. store.Add(cert);
  200. }
  201. }
  202. }
  203. private void Login_FormClosing(object sender, FormClosingEventArgs e)
  204. {
  205. BaseUtil.FormStepInOrOut(this, false);
  206. try
  207. {
  208. dh.Dispose();
  209. }
  210. catch (Exception)
  211. {
  212. }
  213. }
  214. private void UserName_KeyDown(object sender, KeyEventArgs e)
  215. {
  216. if (e.KeyCode == Keys.Enter)
  217. PassWord.Focus();
  218. }
  219. private void Login_Paint(object sender, PaintEventArgs e)
  220. {
  221. Bitmap image = Resources.dialog_close_24px_1173781_easyicon_net;
  222. Rectangle rect = this.ClientRectangle;
  223. //画关闭符号
  224. using (Pen objpen = new Pen(Color.Black))
  225. {
  226. //使用图片
  227. Bitmap bt = new Bitmap(image);
  228. Point p5 = new Point(Width - Close_Size - 4, 4);
  229. e.Graphics.DrawImage(bt, p5);
  230. }
  231. }
  232. private Point offset;
  233. private void Login_MouseDown(object sender, MouseEventArgs e)
  234. {
  235. if (MouseButtons.Left != e.Button) return;
  236. Point cur = this.PointToScreen(e.Location);
  237. offset = new Point(cur.X - this.Left, cur.Y - this.Top);
  238. Rectangle rect = this.ClientRectangle;
  239. rect.Offset(Width - Close_Size - 4, 4);
  240. rect.Width = Close_Size;
  241. rect.Height = Close_Size;
  242. int x = e.X, y = e.Y;
  243. bool isClose = x > rect.X && x < rect.Right && y > rect.Y && y < rect.Bottom;
  244. if (isClose)
  245. Close();
  246. }
  247. private void Login_MouseMove(object sender, MouseEventArgs e)
  248. {
  249. if (MouseButtons.Left != e.Button) return;
  250. Point cur = MousePosition;
  251. this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
  252. }
  253. }
  254. }