Login.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. 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. if (!Directory.Exists(Application.StartupPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd")))
  49. {
  50. Directory.CreateDirectory(Application.StartupPath + @"\" + DateTime.Now.ToString("yyyy-MM-dd"));
  51. }
  52. //MessageBox.Show("HELLO");
  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="+ Address + ")(PORT=1521)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  90. //DataHelper.DBConnectionString = "Password=select!#%*(;User ID=" + DBUser + ";Pooling=false;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=usoft.7766.org)(PORT=11659)))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)));";
  91. SystemInf.CurrentDB = DBUser;
  92. //设置屏幕的宽高信息
  93. SystemInf.ScreenWidth = Screen.PrimaryScreen.WorkingArea.Width;
  94. SystemInf.ScreenHeight = Screen.PrimaryScreen.WorkingArea.Height;
  95. //重新执行一遍构造函数使得重置之后的链接字符串生效
  96. dh = new DataHelper();
  97. SystemInf.dh = dh;
  98. string ErrorMessage = "";
  99. //验证用户名和密码
  100. if (LogicHandler.CheckUserLogin(UserName.Text, PassWord.Text, out ErrorMessage))
  101. {
  102. //验证岗位资源,通过验证开启主窗体
  103. if (LogicHandler.CheckUserAndResourcePassed(UserName.Text, Source.Text, out ErrorMessage))
  104. {
  105. SetInf();//设置User的全局变量
  106. this.Hide();
  107. //记录登陆信息
  108. try
  109. {
  110. IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
  111. string IPAddress = "";
  112. for (int i = 0; i < IpEntry.AddressList.Length; i++)
  113. {
  114. if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
  115. IPAddress = IpEntry.AddressList[i].ToString();
  116. }
  117. string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
  118. LogicHandler.RecordLogInfo(User.UserCode, User.UserName, version, "CS", IPAddress);
  119. }
  120. catch (Exception ex)
  121. {
  122. LogManager.DoLog(ex.Message);
  123. }
  124. LogManager.DoLog("登陆成功" + User.UserName + " " + User.UserAccountType);
  125. Main dialog = new Main();
  126. dialog.ShowDialog();
  127. this.Close();
  128. }
  129. else
  130. MessageBox.Show(ErrorMessage);
  131. }
  132. else
  133. MessageBox.Show(ErrorMessage);
  134. }
  135. /// <summary>
  136. ///设置User类的全局变量,类似于Session
  137. /// </summary>
  138. private void SetInf()
  139. {
  140. //设置全局用户信息
  141. string[] field = { "em_code,em_depart,em_type,em_name,em_position,em_professname,em_type,ug_code,ug_name" };
  142. 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() + "'");
  143. 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() + "'");
  144. //存在岗位资源则要求添加
  145. if (dt.Rows.Count > 0)
  146. {
  147. User.CurrentStepCode = dt.Rows[0]["sc_stepcode"].ToString();
  148. User.CurrentStepName = dt.Rows[0]["sc_stepname"].ToString();
  149. User.DefaultBadGroup = dt.Rows[0]["st_badgroupcode"].ToString();
  150. User.UserLineCode = dt.Rows[0]["sc_linecode"].ToString();
  151. User.UserSourceCode = dt.Rows[0]["sc_code"].ToString();
  152. User.WorkCenter = dt.Rows[0]["sc_wccode"].ToString();
  153. }
  154. //通过Source表关联岗位资源编号查询出当前的执行工序
  155. User.UserCode = UserInf.Rows[0]["em_code"].ToString();
  156. User.UserDepartment = UserInf.Rows[0]["em_depart"].ToString();
  157. User.UserName = UserInf.Rows[0]["em_name"].ToString();
  158. User.UserPosition = UserInf.Rows[0]["em_position"].ToString();
  159. User.UserProfessName = UserInf.Rows[0]["em_professname"].ToString();
  160. User.UserAccountType = UserInf.Rows[0]["em_type"].ToString();
  161. User.UserGroupCode = UserInf.Rows[0]["ug_code"].ToString();
  162. User.UserGroup = UserInf.Rows[0]["ug_name"].ToString();
  163. //设置系统基础信息
  164. UserInf = (DataTable)dh.ExecuteSql("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as systime from dual", "select");
  165. SystemInf.LoginTime = UserInf.Rows[0]["systime"].ToString();
  166. //记录在本地用户成功登录的用户名
  167. BaseUtil.SetCacheData("LastLoginUser", UserName.Text);
  168. BaseUtil.SetCacheData("LastLoginSob", DB.Text);
  169. BaseUtil.SetCacheData("LastLoginResource", Source.Text);
  170. }
  171. private void ForGetPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  172. {
  173. MessageBox.Show("请联系管理员修改密码");
  174. }
  175. //用Enter键调用登陆的点击事件
  176. private void Source_KeyDown(object sender, KeyEventArgs e)
  177. {
  178. if (e.KeyCode == Keys.Enter)
  179. LoginButton.PerformClick();
  180. }
  181. private void PassWord_KeyDown(object sender, KeyEventArgs e)
  182. {
  183. if (e.KeyCode == Keys.Enter)
  184. Source.Focus();
  185. }
  186. /// <summary>
  187. /// 导入证书
  188. /// </summary>
  189. private void CertImport()
  190. {
  191. X509Certificate2 cert = new X509Certificate2(Application.StartupPath + @"\Cert\深圳市优软科技有限公司.pfx", "uasmes");
  192. if (cert != null)
  193. {
  194. var store2 = new X509Store(StoreName.My, StoreLocation.CurrentUser);
  195. store2.Open(OpenFlags.ReadWrite);
  196. if (!store2.Certificates.Contains(cert))
  197. {
  198. store2.Add(cert);
  199. }
  200. var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
  201. store.Open(OpenFlags.ReadWrite);
  202. if (!store.Certificates.Contains(cert))
  203. {
  204. store.Add(cert);
  205. }
  206. }
  207. }
  208. private void Login_FormClosing(object sender, FormClosingEventArgs e)
  209. {
  210. BaseUtil.FormStepInOrOut(this, false);
  211. try
  212. {
  213. dh.Dispose();
  214. }
  215. catch (Exception)
  216. {
  217. }
  218. }
  219. private void UserName_KeyDown(object sender, KeyEventArgs e)
  220. {
  221. if (e.KeyCode == Keys.Enter)
  222. PassWord.Focus();
  223. }
  224. private void Login_Paint(object sender, PaintEventArgs e)
  225. {
  226. Bitmap image = Resources.dialog_close_24px_1173781_easyicon_net;
  227. Rectangle rect = this.ClientRectangle;
  228. //画关闭符号
  229. using (Pen objpen = new Pen(Color.Black))
  230. {
  231. //使用图片
  232. Bitmap bt = new Bitmap(image);
  233. Point p5 = new Point(Width - Close_Size - 4, 4);
  234. e.Graphics.DrawImage(bt, p5);
  235. }
  236. }
  237. private Point offset;
  238. private void Login_MouseDown(object sender, MouseEventArgs e)
  239. {
  240. if (MouseButtons.Left != e.Button) return;
  241. Point cur = this.PointToScreen(e.Location);
  242. offset = new Point(cur.X - this.Left, cur.Y - this.Top);
  243. Rectangle rect = this.ClientRectangle;
  244. rect.Offset(Width - Close_Size - 4, 4);
  245. rect.Width = Close_Size;
  246. rect.Height = Close_Size;
  247. int x = e.X, y = e.Y;
  248. bool isClose = x > rect.X && x < rect.Right && y > rect.Y && y < rect.Bottom;
  249. if (isClose)
  250. Close();
  251. }
  252. private void Login_MouseMove(object sender, MouseEventArgs e)
  253. {
  254. if (MouseButtons.Left != e.Button) return;
  255. Point cur = MousePosition;
  256. this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
  257. }
  258. }
  259. }