LoginFarme.java 11 KB

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