LoginFarme.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.mes.ui;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.mes.component.MesRadio;
  4. import com.mes.component.MesWebView;
  5. import com.mes.component.MyDialog;
  6. import com.mes.util.Base64Utils;
  7. import com.mes.util.HttpUtils;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.time.LocalDateTime;
  13. public class LoginFarme extends JFrame {
  14. //登录模块组件
  15. static JLabel userNameLabel= new JLabel("<html><body>用户名:</body></html>",JLabel.LEFT);//用户名
  16. static JLabel userPasswordLabel= new JLabel("<html><body>密码:</body></html>",JLabel.LEFT);//用户名
  17. public static JTextField userNameTxt;
  18. public static JPasswordField userPasswordTxt;
  19. static JButton loginButton = new JButton("用户密码登录");
  20. static JButton scanLoginButton = new JButton("扫 码 登 录");
  21. public LoginFarme(){
  22. setTitle("MES系统客户端:"+MesClient.mes_gw+" - "+MesClient.mes_gw_des);
  23. ImageIcon bg = new ImageIcon(MesClient.class.getResource("/background.png"));
  24. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25. JLabel imgLabel = new JLabel(bg);//将背景图放在标签里。
  26. getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。
  27. imgLabel.setBounds(0,0,bg.getIconWidth(), bg.getIconHeight());//设置背景标签的位置
  28. Container contentPane=getContentPane();
  29. contentPane.setLayout(null);//布局,很重要
  30. JPanel welcomePanel = new JPanel();
  31. //welcomePanel.setLayout(new GridLayout(3, 1));
  32. welcomePanel.setLayout(null);
  33. //welcomeLable.setBounds(10, 5, 700, 100);
  34. welcomePanel.setBounds(30, 330, 890, 300);
  35. welcomePanel.setOpaque(false);//背景透明
  36. contentPane.add(welcomePanel);
  37. //登录页面
  38. userNameLabel.setBounds(300, 100, 120, 40);
  39. userNameLabel.setFont(new java.awt.Font("Dialog", 1, 16));
  40. userPasswordLabel.setBounds(300, 150, 120, 40);
  41. userPasswordLabel.setFont(new java.awt.Font("Dialog", 1, 16));
  42. userNameTxt = new JTextField(20);
  43. userNameTxt.setText("system");
  44. userNameTxt.setBounds(400, 105, 150, 30);
  45. userPasswordTxt = new JPasswordField(20);
  46. userPasswordTxt.setBounds(400, 155, 150, 30);
  47. userPasswordTxt.setText("Aa111111");
  48. loginButton.setFont(new java.awt.Font("Dialog", 1, 16));
  49. loginButton.setBounds(300, 200, 255, 40);
  50. loginButton.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png")));
  51. // scanLoginButton.setVisible(false);
  52. scanLoginButton.setFont(new java.awt.Font("Dialog", 1, 16));
  53. scanLoginButton.setBounds(300, 250, 255, 40);
  54. scanLoginButton.setIcon(new ImageIcon(MesClient.class.getResource("/bg/scan_barcode.png")));
  55. welcomePanel.add(userNameLabel);
  56. welcomePanel.add(userPasswordLabel);
  57. welcomePanel.add(userNameTxt);
  58. welcomePanel.add(userPasswordTxt);
  59. welcomePanel.add(loginButton);
  60. welcomePanel.add(scanLoginButton);
  61. loginButton.addActionListener(new ActionListener(){
  62. public void actionPerformed(ActionEvent e) {
  63. login();
  64. }
  65. });
  66. scanLoginButton.addActionListener(new ActionListener(){
  67. public void actionPerformed(ActionEvent e) {
  68. scanLogin();
  69. }
  70. });
  71. ((JPanel)contentPane).setOpaque(false); //注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示出来。
  72. //welcomeWin.setSize(902,678);
  73. setResizable(false);//禁止最大化
  74. setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png")));
  75. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  76. int width = 902;
  77. int height = 678;
  78. setBounds((d.width-width)/2, (d.height-height)/2-(d.height-height)/4, width, height);
  79. /*****正常模式******/
  80. setVisible(true);
  81. }
  82. //登录
  83. public static void login() {
  84. String user_str = userNameTxt.getText().toString();
  85. String password_str = userPasswordTxt.getText().toString();
  86. if(user_str.equalsIgnoreCase("")||password_str.equalsIgnoreCase("")) {
  87. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"用户名或密码不能为空","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  88. return;
  89. }
  90. String username = Base64Utils.getBase64(user_str);
  91. String password = Base64Utils.getBase64(password_str);
  92. System.out.println("&username=" + username + "&password=" + password);
  93. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/login?__ajax=json&username="+username+"&password="+password+"&validCode=&__sid=";
  94. String loginResult = HttpUtils.sendRequest(url);
  95. if(loginResult.equalsIgnoreCase("false")) {
  96. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录异常,请检查网络或联系网络管理员","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  97. return;
  98. }else {
  99. JSONObject retObj = JSONObject.parseObject(loginResult);
  100. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  101. //检查用户权限是否可登录界面
  102. checkUserAuthority(retObj);
  103. }else {
  104. //ret = "msg save error";
  105. //ret = false;
  106. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录失败,用户名或密码错误","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  107. return;
  108. }
  109. }
  110. }
  111. //扫码登录
  112. public static void scanLogin() {
  113. //userNameTxt.setText("");
  114. //userPasswordTxt.setText("");
  115. String scanContent = JOptionPane.showInputDialog(null, "请扫码工牌二维码");
  116. System.out.println("scanContent="+scanContent);
  117. if(scanContent!=null&&!scanContent.equalsIgnoreCase("")) {
  118. String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesLogin/login?__login=true&__ajax=json&username="+scanContent;
  119. String loginResult = HttpUtils.sendRequest(url);
  120. System.out.println("loginResult="+loginResult);
  121. if(loginResult.equalsIgnoreCase("false")) {
  122. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录异常,请检查网络或联系网络管理员","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  123. return;
  124. }else {
  125. JSONObject retObj = JSONObject.parseObject(loginResult);
  126. if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) {
  127. //检查用户权限是否可登录界面
  128. checkUserAuthority(retObj);
  129. }else {
  130. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录失败,用户名或密码错误","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  131. return;
  132. }
  133. }
  134. }else {
  135. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"扫码内容错误","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  136. return;
  137. }
  138. }
  139. //检查用户权限是否可登录界面
  140. public static void checkUserAuthority(JSONObject retObj) {
  141. //设置登录用户名
  142. JSONObject userObj = JSONObject.parseObject(retObj.get("user").toString());
  143. String user_id = userObj.getString("id").toString();
  144. //获取sessionid,判断权限
  145. MesClient.sessionid = retObj.get("sessionid").toString();
  146. if(MesClient.sessionid!=null&&!MesClient.sessionid.equalsIgnoreCase("")) {
  147. //请求权限
  148. String url_authority = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesLineProcess/userAuth?__ajax=json&type=0&__sid="+MesClient.sessionid+"&oprno="+MesClient.mes_gw+"&lineSn="+MesClient.mes_line_sn;
  149. String authorityResult = HttpUtils.sendRequest(url_authority);
  150. System.out.println("authorityResult="+authorityResult);
  151. JSONObject authorityObj = JSONObject.parseObject(authorityResult);
  152. if(authorityObj.get("result")!=null&&authorityObj.get("result").toString().equalsIgnoreCase("true")) {
  153. JSONObject authObjTmp = JSONObject.parseObject(authorityObj.get("data").toString());
  154. MesClient.mes_auth = Integer.parseInt(authObjTmp.getString("auth").toString());
  155. if(MesClient.mes_auth==0) {
  156. //无权限登录
  157. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"您无权登录该工位","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  158. return;
  159. }else if(MesClient.mes_auth==1||MesClient.mes_auth==2) {
  160. // 获取等于所处时间-当前小时
  161. LocalDateTime now = LocalDateTime.now();
  162. MesClient.userLoginHours = now.getHour();
  163. //初始化tcp连接,发送同步报文
  164. MesClient.initTcpConnection();
  165. //启动timer心跳包
  166. MesClient.startHeartBeatTimer();
  167. //1操作工人,2管理员
  168. //登录成功
  169. MesClient.user_menu.setText(user_id);
  170. MesClient.welcomeWin.setVisible(false);
  171. MesClient.mesClientFrame.setVisible(true);
  172. if(MesClient.jfxPanel == null){
  173. String url = "http://"+ MesClient.mes_server_ip+":8980/js/a/mes/mesProductRecord/work?__sid="+MesClient.sessionid+"&oprno="+MesClient.mes_gw+"&lineSn="+MesClient.mes_line_sn;
  174. MesClient.jfxPanel = new MesWebView(url);
  175. MesClient.jfxPanel.setSize(990, 550);
  176. MesClient.indexPanelB.add(MesClient.jfxPanel);
  177. }
  178. if(MesClient.jfxPanel2 == null){
  179. String url = "http://"+ MesClient.mes_server_ip+":8980/js/a/mes/mesProcessCheckRecord/ulist?__sid="+MesClient.sessionid+"&oprno="+MesClient.mes_gw+"&lineSn="+MesClient.mes_line_sn;
  180. MesClient.jfxPanel2 = new MesWebView(url);
  181. MesClient.jfxPanel2.setSize(990, 550);
  182. MesClient.indexPanelC.add(MesClient.jfxPanel2);
  183. }
  184. MesClient.initWarehouseData();
  185. /*MesClient.product_sn.setText("5024026600851241241999000091");
  186. String lmsg = "该工件OP040加工NG";
  187. MesClient.status_menu.setText(lmsg);
  188. new MyDialog(MesClient.mesClientFrame,"提示",lmsg);*/
  189. }
  190. }
  191. }else {
  192. JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录失败,用户名或密码错误","提示窗口", JOptionPane.INFORMATION_MESSAGE);
  193. return;
  194. }
  195. }
  196. }