LoginFarme.java 11 KB

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