LoginFrame.java 10 KB

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