wangxichen 3 дней назад
Родитель
Сommit
35feb657f5
2 измененных файлов с 136 добавлено и 0 удалено
  1. 89 0
      src/com/mes/ui/MesClient.java
  2. 47 0
      src/com/mes/ui/StationConfig.java

+ 89 - 0
src/com/mes/ui/MesClient.java

@@ -73,6 +73,7 @@ public class MesClient extends JFrame {
     public static int mes_heart_beat_cycle = 10; // 心跳周期
     public static int mes_heart_icon_cycle = 1;
     public static String mes_line_sn = ""; // 产线编号
+    public static java.util.List<String> mes_barcode_prefixes = new java.util.ArrayList<String>(); // 工件码前缀白名单(空=不校验)
 
     //TCP连接
     public static NettyClient nettyClient;
@@ -175,6 +176,7 @@ public class MesClient extends JFrame {
         mes_line_sn = pro.getProperty("mes.line_sn");
 
         mes_gw_des = OprnoUtil.getGwDes(mes_line_sn,mes_gw);
+        mes_barcode_prefixes = StationConfig.loadBarcodePrefixes();
 
         log.info(mes_gw + ";" + mes_gw_des + ";" + mes_server_ip + ";" + mes_tcp_port + ";" + mes_heart_beat_cycle);
     }
@@ -360,6 +362,20 @@ public class MesClient extends JFrame {
         //弹窗扫工件码
         String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
         if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
+            // 前缀校验(多前缀,匹配任一即通过)
+            if (mes_barcode_prefixes != null && !mes_barcode_prefixes.isEmpty()) {
+                boolean matched = false;
+                for (String prefix : mes_barcode_prefixes) {
+                    if (scanBarcode.startsWith(prefix)) {
+                        matched = true;
+                        break;
+                    }
+                }
+                if (!matched) {
+                    MesClient.setMenuStatus("工件码前缀不匹配,允许的前缀:" + mes_barcode_prefixes, -1);
+                    return;
+                }
+            }
             //获取用户
             getUser();
             //获取扫码内容36位
@@ -480,6 +496,17 @@ public class MesClient extends JFrame {
             }
         });
         settingMenu.add(switchStationMenu);
+
+        JMenuItem prefixConfigMenu = new JMenuItem("前缀校验配置");
+        prefixConfigMenu.setIcon(new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")));
+        prefixConfigMenu.setFont(new Font("微软雅黑", Font.PLAIN, 20));
+        prefixConfigMenu.addActionListener(new ActionListener() {
+            public void actionPerformed(ActionEvent e) {
+                showPrefixConfigDialog();
+            }
+        });
+        settingMenu.add(prefixConfigMenu);
+
         contentPane = new JPanel();
         contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
         setContentPane(contentPane);
@@ -709,6 +736,68 @@ public class MesClient extends JFrame {
     }
 
 
+    /** 前缀校验配置(密码保护) */
+    private static void showPrefixConfigDialog() {
+        // 先验证密码 503833
+        JPasswordField pwdField = new JPasswordField(15);
+        pwdField.setFont(new Font("微软雅黑", Font.PLAIN, 18));
+        int pwdOk = JOptionPane.showConfirmDialog(mesClientFrame, pwdField, "请输入设置密码", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
+        if (pwdOk != JOptionPane.OK_OPTION) return;
+        String pwd = new String(pwdField.getPassword());
+        if (!"503833".equals(pwd)) {
+            JOptionPane.showMessageDialog(mesClientFrame, "密码错误", "提示", JOptionPane.WARNING_MESSAGE);
+            return;
+        }
+
+        // 密码正确,显示前缀列表配置界面
+        DefaultListModel<String> listModel = new DefaultListModel<String>();
+        for (String p : mes_barcode_prefixes) listModel.addElement(p);
+        JList<String> prefixList = new JList<String>(listModel);
+        prefixList.setFont(new Font("微软雅黑", Font.PLAIN, 16));
+        JScrollPane scrollPane = new JScrollPane(prefixList);
+        scrollPane.setPreferredSize(new Dimension(300, 150));
+
+        JButton addBtn = new JButton("添加");
+        addBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
+        addBtn.addActionListener(e -> {
+            String newPrefix = JOptionPane.showInputDialog(mesClientFrame, "输入新前缀:");
+            if (newPrefix != null && !newPrefix.trim().isEmpty()) {
+                String t = newPrefix.trim();
+                if (!listModel.contains(t)) listModel.addElement(t);
+            }
+        });
+
+        JButton delBtn = new JButton("删除");
+        delBtn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
+        delBtn.addActionListener(e -> {
+            int idx = prefixList.getSelectedIndex();
+            if (idx >= 0) listModel.remove(idx);
+        });
+
+        JPanel btnPanel = new JPanel();
+        btnPanel.add(addBtn);
+        btnPanel.add(delBtn);
+
+        JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
+        mainPanel.add(new JLabel("工件码前缀白名单(为空则不校验):"), BorderLayout.NORTH);
+        mainPanel.add(scrollPane, BorderLayout.CENTER);
+        mainPanel.add(btnPanel, BorderLayout.SOUTH);
+
+        int ok = JOptionPane.showConfirmDialog(mesClientFrame, mainPanel, "前缀校验配置", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
+        if (ok == JOptionPane.OK_OPTION) {
+            java.util.List<String> newList = new java.util.ArrayList<String>();
+            for (int i = 0; i < listModel.size(); i++) newList.add(listModel.getElementAt(i));
+            try {
+                StationConfig.saveBarcodePrefixes(newList);
+                mes_barcode_prefixes = newList;
+                String msg = newList.isEmpty() ? "已清空前缀,不再校验" : "前缀已更新:" + newList;
+                JOptionPane.showMessageDialog(mesClientFrame, msg, "提示", JOptionPane.INFORMATION_MESSAGE);
+            } catch (IOException ex) {
+                JOptionPane.showMessageDialog(mesClientFrame, "保存失败:" + ex.getMessage(), "提示", JOptionPane.ERROR_MESSAGE);
+            }
+        }
+    }
+
     private static void showStationSwitchDialog() {
         String[] stations = new String[OprnoUtil.t9oprnos.length];
         int selectedStationIndex = 0;

+ 47 - 0
src/com/mes/ui/StationConfig.java

@@ -42,6 +42,53 @@ public class StationConfig {
         }
     }
 
+    // ---- 工件码前缀校验(多前缀,逗号分隔;列表为空=不校验)----
+    private static final String BARCODE_PREFIX_KEY = "mes.barcode_prefix";
+
+    /** 读前缀列表,空列表表示不校验 */
+    public static java.util.List<String> loadBarcodePrefixes() {
+        java.util.List<String> list = new java.util.ArrayList<String>();
+        File file = new File(RUNTIME_CONFIG_PATH);
+        if (!file.exists()) return list;
+        Properties pro = new Properties();
+        try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
+            pro.load(br);
+            String v = pro.getProperty(BARCODE_PREFIX_KEY);
+            if (v == null) return list;
+            for (String s : v.split(",")) {
+                String t = s.trim();
+                if (!t.isEmpty() && !list.contains(t)) list.add(t);
+            }
+        } catch (Exception ignored) {
+        }
+        return list;
+    }
+
+    /** 保存前缀列表,保留文件其他 key */
+    public static void saveBarcodePrefixes(java.util.List<String> prefixes) throws IOException {
+        StringBuilder sb = new StringBuilder();
+        if (prefixes != null) {
+            for (String p : prefixes) {
+                if (p == null || p.trim().isEmpty()) continue;
+                if (sb.length() > 0) sb.append(",");
+                sb.append(p.trim());
+            }
+        }
+        File file = new File(RUNTIME_CONFIG_PATH);
+        Properties pro = new Properties();
+        if (file.exists()) {
+            try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"))) {
+                pro.load(br);
+            }
+        }
+        pro.setProperty(BARCODE_PREFIX_KEY, sb.toString());
+        File parent = file.getParentFile();
+        if (parent != null && !parent.exists()) parent.mkdirs();
+        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"))) {
+            pro.store(bw, "runtime station config");
+        }
+    }
+
     public static void saveRuntimeGw(String gw) throws IOException {
         File file = new File(RUNTIME_CONFIG_PATH);
         File parent = file.getParentFile();