LoginFarme.java 10 KB

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