Login.cs 12 KB

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