Login.cs 12 KB

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