package com.mes.ui; import com.alibaba.fastjson2.JSONObject; import com.mes.component.MesRadio; import com.mes.component.MesWebView; import com.mes.util.Base64Utils; import com.mes.util.HttpUtils; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoginFarme extends JFrame { //登录模块组件 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 LoginFarme(){ setTitle("MES系统客户端:"+MesClient.mes_gw+" - "+MesClient.mes_gw_des); ImageIcon bg = new ImageIcon(MesClient.class.getResource("/background.png")); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JLabel imgLabel = new JLabel(bg);//将背景图放在标签里。 getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));//注意这里是关键,将背景标签添加到jfram的LayeredPane面板里。 imgLabel.setBounds(0,0,bg.getIconWidth(), bg.getIconHeight());//设置背景标签的位置 Container contentPane=getContentPane(); contentPane.setLayout(null);//布局,很重要 JPanel welcomePanel = new JPanel(); //welcomePanel.setLayout(new GridLayout(3, 1)); welcomePanel.setLayout(null); //welcomeLable.setBounds(10, 5, 700, 100); 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", 1, 16)); userPasswordLabel.setBounds(300, 150, 120, 40); userPasswordLabel.setFont(new java.awt.Font("Dialog", 1, 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", 1, 16)); loginButton.setBounds(300, 200, 255, 40); loginButton.setIcon(new ImageIcon(MesClient.class.getResource("/bg/user.png"))); // scanLoginButton.setVisible(false); scanLoginButton.setFont(new java.awt.Font("Dialog", 1, 16)); scanLoginButton.setBounds(300, 250, 255, 40); scanLoginButton.setIcon(new ImageIcon(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(new ActionListener(){ public void actionPerformed(ActionEvent e) { login(); } }); scanLoginButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { scanLogin(); } }); ((JPanel)contentPane).setOpaque(false); //注意这里,将内容面板设为透明。这样LayeredPane面板中的背景才能显示出来。 //welcomeWin.setSize(902,678); 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().toString(); String password_str = userPasswordTxt.getText().toString(); 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); System.out.println("&username=" + username + "&password=" + password); String url = "http://"+MesClient.mes_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); return; }else { JSONObject retObj = JSONObject.parseObject(loginResult); if(retObj.get("result")!=null&&retObj.get("result").toString().equalsIgnoreCase("true")) { //检查用户权限是否可登录界面 checkUserAuthority(retObj); }else { //ret = "msg save error"; //ret = false; JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录失败,用户名或密码错误","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } } //扫码登录 public static void scanLogin() { //userNameTxt.setText(""); //userPasswordTxt.setText(""); String scanContent = JOptionPane.showInputDialog(null, "请扫码工牌二维码"); System.out.println("scanContent="+scanContent); if(scanContent!=null&&!scanContent.equalsIgnoreCase("")) { String url = "http://"+MesClient.mes_server_ip+":8980/js/a/mes/mesLogin/login?__login=true&__ajax=json&username="+scanContent; String loginResult = HttpUtils.sendRequest(url); System.out.println("loginResult="+loginResult); if(loginResult.equalsIgnoreCase("false")) { JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录异常,请检查网络或联系网络管理员","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; }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); return; } } }else { JOptionPane.showMessageDialog(MesClient.mesClientFrame,"扫码内容错误","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } //检查用户权限是否可登录界面 public static void checkUserAuthority(JSONObject retObj) { //设置登录用户名 JSONObject userObj = JSONObject.parseObject(retObj.get("user").toString()); String user_id = userObj.getString("id").toString(); //获取sessionid,判断权限 MesClient.sessionid = retObj.get("sessionid").toString(); if(MesClient.sessionid!=null&&!MesClient.sessionid.equalsIgnoreCase("")) { //请求权限 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; String authorityResult = HttpUtils.sendRequest(url_authority); System.out.println("authorityResult="+authorityResult); 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").toString()); if(MesClient.mes_auth==0) { //无权限登录 JOptionPane.showMessageDialog(MesClient.mesClientFrame,"您无权登录该工位","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; }else if(MesClient.mes_auth==1||MesClient.mes_auth==2) { //初始化tcp连接,发送同步报文 MesClient.initTcpConnection(); //启动timer心跳包 MesClient.startHeartBeatTimer(); //1操作工人,2管理员 //登录成功 MesClient.user_menu.setText(user_id); MesClient.welcomeWin.setVisible(false); MesClient.mesClientFrame.setVisible(true); if(MesClient.jfxPanel == null){ 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; MesClient.jfxPanel = new MesWebView(url); MesClient.jfxPanel.setSize(990, 550); MesClient.indexPanelB.add(MesClient.jfxPanel); } if(MesClient.jfxPanel2 == null){ 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; MesClient.jfxPanel2 = new MesWebView(url); MesClient.jfxPanel2.setSize(990, 550); MesClient.indexPanelC.add(MesClient.jfxPanel2); } MesClient.initWarehouseData(); } } }else { JOptionPane.showMessageDialog(MesClient.mesClientFrame,"登录失败,用户名或密码错误","提示窗口", JOptionPane.INFORMATION_MESSAGE); return; } } }