package com.mes.ui; import com.alibaba.fastjson2.JSONObject; import com.mes.component.MesWebView; import com.mes.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.*; import java.time.LocalDateTime; import java.util.Objects; public class LoginFrame extends JFrame { public static final Logger log = LoggerFactory.getLogger(LoginFrame.class); //登录模块组件 static JLabel userNameLabel = new JLabel("用户名:", JLabel.LEFT);//用户名 static JLabel userPasswordLabel = new JLabel("密码:", JLabel.LEFT);//用户名 public static JTextField userNameTxt; public static JPasswordField userPasswordTxt; static JButton loginButton = new JButton("用户密码登录"); static JButton scanLoginButton = new JButton("扫 码 登 录"); public LoginFrame() { setTitle("MES系统客户端:" + Config.gw_1 + " - 绝缘检测"); ImageIcon bg = new ImageIcon(Objects.requireNonNull(MesClient.class.getResource("/background.png"))); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel imgLabel = new JLabel(bg);//将背景图放在标签里。 getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签添加到frame的LayeredPane面板里。 imgLabel.setBounds(0, 0, bg.getIconWidth(), bg.getIconHeight());//设置背景标签的位置 Container contentPane = getContentPane(); contentPane.setLayout(null);//布局,很重要 JPanel welcomePanel = new JPanel(); welcomePanel.setLayout(null); welcomePanel.setBounds(30, 330, 890, 300); welcomePanel.setOpaque(false);//背景透明 contentPane.add(welcomePanel); //登录页面 userNameLabel.setBounds(300, 100, 120, 40); userNameLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 16)); userPasswordLabel.setBounds(300, 150, 120, 40); userPasswordLabel.setFont(new java.awt.Font("Dialog", Font.BOLD, 16)); userNameTxt = new JTextField(20); userNameTxt.setText("system"); userNameTxt.setBounds(400, 105, 150, 30); userPasswordTxt = new JPasswordField(20); userPasswordTxt.setBounds(400, 155, 150, 30); userPasswordTxt.setText("Aa111111"); loginButton.setFont(new java.awt.Font("Dialog", Font.BOLD, 16)); loginButton.setBounds(300, 200, 255, 40); loginButton.setIcon(new ImageIcon(Objects.requireNonNull(MesClient.class.getResource("/bg/user.png")))); scanLoginButton.setFont(new java.awt.Font("Dialog", Font.BOLD, 16)); scanLoginButton.setBounds(300, 250, 255, 40); scanLoginButton.setIcon(new ImageIcon(Objects.requireNonNull(MesClient.class.getResource("/bg/scan_barcode.png")))); welcomePanel.add(userNameLabel); welcomePanel.add(userPasswordLabel); welcomePanel.add(userNameTxt); welcomePanel.add(userPasswordTxt); welcomePanel.add(loginButton); welcomePanel.add(scanLoginButton); loginButton.addActionListener(e -> login()); scanLoginButton.addActionListener(e -> scanLogin()); ((JPanel) contentPane).setOpaque(false); //注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示出来。 setResizable(false);//禁止最大化 setIconImage(Toolkit.getDefaultToolkit().getImage(MesClient.class.getResource("/bg/logo.png"))); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); int width = 902; int height = 678; setBounds((d.width - width) / 2, (d.height - height) / 2 - (d.height - height) / 4, width, height); // 正常模式 setVisible(true); } //登录 public static void login() { String user_str = userNameTxt.getText(); String password_str = userPasswordTxt.getText(); if (user_str.equalsIgnoreCase("") || password_str.equalsIgnoreCase("")) { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "用户名或密码不能为空", "提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } String username = Base64Utils.getBase64(user_str); String password = Base64Utils.getBase64(password_str); log.info("登录 --> 用户名: {}, 密码: {}", username, password); String url = "http://" + Config.server_ip + ":8980/js/a/login?__ajax=json&username=" + username + "&password=" + password + "&validCode=&__sid="; String loginResult = HttpUtils.sendRequest(url); if (loginResult.equalsIgnoreCase("false")) { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "登录异常,请检查网络或联系网络管理员", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } else { JSONObject retObj = JSONObject.parseObject(loginResult); if (retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) { //检查用户权限是否可登录界面 checkUserAuthority(retObj); } else { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "登录失败,用户名或密码错误", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } } } //扫码登录 public static void scanLogin() { String scanContent = JOptionPane.showInputDialog(null, "请扫码工牌二维码"); log.info("scanContent={}", scanContent); if (scanContent != null && !scanContent.equalsIgnoreCase("")) { String url = "http://" + Config.server_ip + ":8980/js/a/mes/mesLogin/login?__login=true&__ajax=json&username=" + scanContent; String loginResult = HttpUtils.sendRequest(url); log.info("loginResult={}", loginResult); if (loginResult.equalsIgnoreCase("false")) { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "登录异常,请检查网络或联系网络管理员", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } else { JSONObject retObj = JSONObject.parseObject(loginResult); if (retObj.get("result") != null && retObj.get("result").toString().equalsIgnoreCase("true")) { //检查用户权限是否可登录界面 checkUserAuthority(retObj); } else { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "登录失败,用户名或密码错误", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } } } else { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "扫码内容错误", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } } //检查用户权限是否可登录界面 public static void checkUserAuthority(JSONObject retObj) { //设置登录用户名 JSONObject userObj = JSONObject.parseObject(retObj.get("user").toString()); String user_id = userObj.getString("id"); //获取session id,判断权限 MesClient.sessionid = retObj.get("sessionid").toString(); if (MesClient.sessionid != null && !MesClient.sessionid.equalsIgnoreCase("")) { //请求权限 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; String authorityResult = HttpUtils.sendRequest(url_authority); JSONObject authorityObj = JSONObject.parseObject(authorityResult); if (authorityObj.get("result") != null && authorityObj.get("result").toString().equalsIgnoreCase("true")) { JSONObject authObjTmp = JSONObject.parseObject(authorityObj.get("data").toString()); MesClient.mes_auth = Integer.parseInt(authObjTmp.getString("auth")); if (MesClient.mes_auth == 0) { //无权限登录 JOptionPane.showMessageDialog(MesClient.mesClientFrame, "您无权登录该工位", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } else if (MesClient.mes_auth == 1 || MesClient.mes_auth == 2) { // 获取等于所处时间-当前小时 MesClient.userLoginHours = LocalDateTime.now().getHour(); //初始化tcp连接,发送同步报文 MesClient.initTcpConnection(); //启动timer心跳包 MesClient.startHeartBeatTimer(); //1操作工人,2管理员 //登录成功 MesClient.user_menu.setText(user_id); MesClient.welcomeWin.setVisible(false); MesClient.mesClientFrame.setVisible(true); MesClient.getUser(); String url1 = "http://" + Config.server_ip + ":8980/js/a/mes/mesProductRecord/work" + "?oprno=" + Config.gw_1 + "&lineSn=" + Config.line_sn; MesWebView jfxPanel = new MesWebView(url1); jfxPanel.setSize(990, 550); MesClient.indexPanelB.add(jfxPanel); String url2 = "http://" + Config.server_ip + ":8980/js/a/mes/mesProcessCheckRecord/ulist?ucode=" + user_id + "&oprno=OP380" + "&lineSn=" + Config.line_sn; MesWebView jfxPanel2 = new MesWebView(url2); jfxPanel2.setSize(990, 550); MesClient.indexPanelC.add(jfxPanel2); String url4 = "http://" + Config.server_ip + ":8980/js/a/mes/mesProcessUserRecord/submitView?oprno=" + Config.gw_1; MesWebView webView4 = new MesWebView(url4); webView4.setSize(990, 550); MesClient.panel4.removeAll(); MesClient.panel4.add(webView4); MesClient.initWarehouseData(); UpdateQualityTimer.start(); // 开启更新质量的定时任务 WorkTimer.start(); } } } else { JOptionPane.showMessageDialog(MesClient.mesClientFrame, "登录失败,用户名或密码错误", "提示窗口", JOptionPane.INFORMATION_MESSAGE); } } }