Login.cs 11 KB

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