Browse Source

新增客户端版本更新功能

hou 6 days ago
parent
commit
04a088efe2

+ 42 - 11
src/com/mes/ui/DataUtil.java

@@ -140,6 +140,7 @@ public class DataUtil {
     }
 
     public static String doPost(String httpUrl, String param) {
+        httpUrl = appendSid(httpUrl);
         HttpURLConnection connection = null;
         InputStream is = null;
         OutputStream os = null;
@@ -210,9 +211,9 @@ public class DataUtil {
     }
 
     /**
-     * 查询工记录
+     * 查询单部件加工记录
      * @param oprno 工位号(空则查所有工位)
-     * @param sn 工件码(可选,用于搜索)
+     * @param sn 精追码(可选,用于搜索)
      * @param pageNo 页码
      * @param pageSize 每页条数
      * @return 工作记录响应
@@ -223,25 +224,27 @@ public class DataUtil {
             String mes_server_ip = MesClient.mes_server_ip;
             String lineSn = MesClient.mes_line_sn == null ? "" : MesClient.mes_line_sn.trim();
             
-            String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductRecord/workData";
+            String url = "http://" + mes_server_ip + ":8980/js/a/mes/mesProductDbjRecord/listData";
             StringBuilder params = new StringBuilder();
             params.append("__ajax=json");
             params.append("&lineSn=").append(lineSn);
             params.append("&pageNo=").append(pageNo);
             params.append("&pageSize=").append(pageSize);
             System.out.println("oprno="+oprno);
-            //todo
             if (oprno != null && !oprno.isEmpty()) {
-                params.append("&oprno=").append(oprno);
+                String queryOprno = oprno.trim();
+                if (queryOprno.length() == 5) {
+                    queryOprno = queryOprno + "A";
+                }
+                params.append("&oprno=").append(queryOprno);
             }
-            // 注意:不传auto参数,这样"所有工位"查询时会返回整条产线所有工位的数据
             if (sn != null && !sn.isEmpty()) {
                 params.append("&sn=").append(sn);
             }
             
-            log.info("查询工记录: url=" + url + ", params=" + params.toString());
+            log.info("查询单部件加工记录: url=" + url + ", params=" + params.toString());
             String result = doPost(url, params.toString());
-            log.info("查询工记录结果: result=" + result);
+            log.info("查询单部件加工记录结果: result=" + result);
             
             if (result == null || result.trim().isEmpty()) {
                 resp.setResult(false);
@@ -255,6 +258,14 @@ public class DataUtil {
                 resp.setMessage("解析响应失败");
                 return resp;
             }
+            if (jsonObj.containsKey("result") && jsonObj.getJSONArray("list") == null) {
+                resp.setResult(false);
+                String message = jsonObj.getString("message");
+                resp.setMessage(message == null || message.trim().isEmpty()
+                        ? "查询失败,请确认账号有单部件加工记录查看权限"
+                        : message);
+                return resp;
+            }
             
             resp.setResult(true);
             resp.setPageNo(jsonObj.getIntValue("pageNo"));
@@ -269,10 +280,14 @@ public class DataUtil {
                     WorkRecordData data = new WorkRecordData();
                     data.setId(item.getString("id"));
                     data.setSn(item.getString("sn"));
+                    data.setDbjSn(item.getString("dbjSn"));
                     data.setOprno(item.getString("oprno"));
-                    data.setUpdateBy(item.getString("updateBy"));
-                    data.setUpdateDate(item.getString("updateDate"));
-                    data.setContent(item.getString("content"));
+                    String userCode = item.getString("userCode");
+                    data.setUpdateBy(userCode != null && !userCode.isEmpty() ? userCode : item.getString("updateBy"));
+                    String createDate = item.getString("createDate");
+                    data.setUpdateDate(createDate != null && !createDate.isEmpty() ? createDate : item.getString("updateDate"));
+                    String resultVal = item.getString("result");
+                    data.setContent(resultVal != null && !resultVal.isEmpty() ? resultVal : item.getString("content"));
                     list.add(data);
                 }
             }
@@ -286,4 +301,20 @@ public class DataUtil {
         }
         return resp;
     }
+
+    private static String appendSid(String url) {
+        if (url == null || url.contains("__sid=") || url.contains("/login")) {
+            return url;
+        }
+        try {
+            String sid = MesClient.sessionid;
+            if (sid == null || sid.length() == 0) {
+                return url;
+            }
+            return url + (url.contains("?") ? "&" : "?") + "__sid=" + sid;
+        } catch (Exception e) {
+            return url;
+        }
+    }
+
 }

+ 5 - 8
src/com/mes/ui/LoginFarme.java

@@ -3,6 +3,7 @@ package com.mes.ui;
 import com.alibaba.fastjson2.JSONObject;
 import com.mes.component.MesRadio;
 import com.mes.component.MesWebView;
+import com.mes.util.ClientUpgrade;
 import com.mes.util.Base64Utils;
 import com.mes.util.HttpUtils;
 
@@ -94,6 +95,7 @@ public class LoginFarme extends JFrame {
         if (!MesClient.ensureOpConfig(MesClient.welcomeWin)) {
             return;
         }
+        ClientUpgrade.checkIfConfigured(MesClient.welcomeWin);
         String user_str = userNameTxt.getText().toString();
         String password_str = userPasswordTxt.getText().toString();
         if(user_str.equalsIgnoreCase("")||password_str.equalsIgnoreCase("")) {
@@ -126,6 +128,7 @@ public class LoginFarme extends JFrame {
         if (!MesClient.ensureOpConfig(MesClient.welcomeWin)) {
             return;
         }
+        ClientUpgrade.checkIfConfigured(MesClient.welcomeWin);
         //userNameTxt.setText("");
         //userPasswordTxt.setText("");
         String scanContent = JOptionPane.showInputDialog(null, "请扫码工牌二维码");
@@ -184,14 +187,8 @@ public class LoginFarme extends JFrame {
                     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?oprno=" + MesClient.mes_gw + "&lineSn=" + MesClient.mes_line_sn;
-						MesClient.jfxPanel = new MesWebView(url);
-						MesClient.jfxPanel.setSize(990, 550);
-						MesClient.jfxPanel.setBounds(0, 0, 990, 550);
-						MesClient.indexPanelB.add(MesClient.jfxPanel);
-					}
+					// 工作记录:登录后按当前工位拉取单部件加工记录
+					MesClient.refreshWorkRecordPanel();
 
 					// 开班点检
 					if (MesClient.jfxPanel2 == null) {

+ 41 - 8
src/com/mes/ui/MesClient.java

@@ -3,6 +3,8 @@ package com.mes.ui;
 import com.alibaba.fastjson2.JSONObject;
 import com.mes.component.MesRadio;
 import com.mes.component.MesWebView;
+import com.mes.util.ClientVersionUtils;
+import com.mes.util.ClientUpgrade;
 import com.mes.util.DateLocalUtils;
 import com.mes.util.HttpUtils;
 import com.mes.util.JdbcUtils;
@@ -35,7 +37,8 @@ public class MesClient extends JFrame {
     public static String mes_gw_des = ""; // 工位名称
     public static String mes_server_ip = ""; // 服务器IP地址
     public static int mes_heart_icon_cycle = 1;
-    public static String mes_line_sn = ""; // 产线编号
+    public static String mes_line_sn = "";
+    public static String client_version = "0.0.0"; // 打包发布版本,需和后台 JAR 管理版本号对齐 // 产线编号
     public static String mes_sn_prefix = ""; // 工件码开头格式,多个用逗号分隔
 
     //session
@@ -80,7 +83,8 @@ public class MesClient extends JFrame {
 
     public static JFrame welcomeWin;
 
-    public static JPanel indexPanelB; // 已弃用,改为使用 WorkRecordPanel
+    public static JPanel indexPanelB;
+    public static WorkRecordPanel workRecordPanel;
     public static MesWebView jfxPanel = null;
     public static JPanel indexPanelC;
     public static MesWebView jfxPanel2 = null;
@@ -93,6 +97,9 @@ public class MesClient extends JFrame {
     public static JLabel fxlabel;
 
     public static void main(String[] args) {
+        if (ClientUpgrade.handleInstallArgs(args)) {
+            return;
+        }
 
 //        if (LockUtil.getInstance().isAppActive() == false){
 ////            JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
@@ -106,6 +113,11 @@ public class MesClient extends JFrame {
 
                         //读文件配置
                         readProperty();
+                        if (ClientUpgrade.recoverIncompleteInstall()) {
+                            return;
+                        }
+                        ClientUpgrade.cleanupInstallLeftovers();
+                        ClientUpgrade.checkIfConfigured(null);
 
                         // 显示界面
                         mesClientFrame = new MesClient();
@@ -123,6 +135,14 @@ public class MesClient extends JFrame {
 //    }
 
     //读配置文件
+
+    private static String parseClientVersion(String value) {
+        if (value == null || value.trim().isEmpty()) {
+            return "0.0.0";
+        }
+        return ClientVersionUtils.normalize(value.trim());
+    }
+
     private static void readProperty() throws IOException{
         String enconding = "UTF-8";
         InputStream is = ClassLoader.getSystemResourceAsStream("config/config.properties");
@@ -137,6 +157,7 @@ public class MesClient extends JFrame {
         Properties pro = new Properties();
         BufferedReader br = new BufferedReader(new InputStreamReader(is, enconding));
         pro.load(br);
+        client_version = parseClientVersion(pro.getProperty("mes.client.version"));
         mes_gw =  pro.getProperty("mes.gw");
         mes_line_sn = pro.getProperty("mes.line_sn");
         mes_sn_prefix = pro.getProperty("mes.sn_prefix");
@@ -194,6 +215,18 @@ public class MesClient extends JFrame {
         }
     }
 
+    public static void refreshWorkRecordPanel() {
+        if (indexPanelB == null) {
+            return;
+        }
+        workRecordPanel = new WorkRecordPanel(mes_gw, mes_line_sn);
+        indexPanelB.removeAll();
+        indexPanelB.add(workRecordPanel, BorderLayout.CENTER);
+        indexPanelB.revalidate();
+        indexPanelB.repaint();
+        workRecordPanel.refreshData();
+    }
+
     // 打开并切换到“软件设置”页签(实际访问权限由页签切换监听统一控制)
     public static void openSoftwareSettings() {
         if (mesClientFrame != null) {
@@ -829,12 +862,9 @@ public class MesClient extends JFrame {
         tabbedPane.addTab("开班点检", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPaneDj, null);
 
 
-        // ========== 工作记录 Tab(改造:当前工位Tab + 所有工位Tab) ==========
-        // 旧代码已注释:
-         indexPanelB = new JPanel();
-         searchScrollPane = new JScrollPane(indexPanelB);
-         indexPanelB.setLayout(null);
-         tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), searchScrollPane, null);
+        // 工作记录:登录后由客户端表格拉取单部件加工记录
+        indexPanelB = new JPanel(new BorderLayout());
+        tabbedPane.addTab("工作记录", new ImageIcon(MesClient.class.getResource("/bg/menu_data_preprocess.png")), indexPanelB, null);
 
          // 创建“软件设置”页面,并加入主Tab页签
          JPanel softwareSettingPanel = buildSoftwareSettingPanel();
@@ -895,6 +925,9 @@ public class MesClient extends JFrame {
                 } else {
                     // 只记录非“软件设置”页签,便于校验失败时回退
                     lastNonSoftwareTabIndex = selectedIndex;
+                    if ("工作记录".equals(tabbedPane.getTitleAt(selectedIndex)) && workRecordPanel != null) {
+                        workRecordPanel.refreshData();
+                    }
                 }
             }
         });

+ 10 - 1
src/com/mes/ui/WorkRecordData.java

@@ -5,7 +5,8 @@ package com.mes.ui;
  */
 public class WorkRecordData {
     private String id;
-    private String sn;          // 工件码
+    private String sn;          // 精追码
+    private String dbjSn;       // 主工件码
     private String oprno;       // 工位号
     private String updateBy;    // 作业人员
     private String updateDate;  // 日期
@@ -27,6 +28,14 @@ public class WorkRecordData {
         this.sn = sn;
     }
 
+    public String getDbjSn() {
+        return dbjSn;
+    }
+
+    public void setDbjSn(String dbjSn) {
+        this.dbjSn = dbjSn;
+    }
+
     public String getOprno() {
         return oprno;
     }

+ 38 - 31
src/com/mes/ui/WorkRecordPanel.java

@@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory;
 import javax.swing.*;
 import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.DefaultTableModel;
-import javax.swing.table.TableColumn;
 import java.awt.*;
 import java.awt.event.ComponentAdapter;
 import java.awt.event.ComponentEvent;
@@ -21,7 +20,7 @@ public class WorkRecordPanel extends JPanel {
     private static final Logger log = LoggerFactory.getLogger(WorkRecordPanel.class);
 
     // 表格列名
-    private static final String[] COLUMN_NAMES = {"工件码", "工位号", "工位结果", "作业人员", "日期"};
+    private static final String[] COLUMN_NAMES = {"精追码", "工位号", "工位结果", "作业人员", "日期"};
 
     // 分页配置
     private static final int PAGE_SIZE = 20;
@@ -86,8 +85,8 @@ public class WorkRecordPanel extends JPanel {
         topPanel.add(statusLabel);
         xPos += 165;
 
-        // 工件码搜索
-        snLabel = new JLabel("工件码:");
+        // 精追码搜索
+        snLabel = new JLabel("精追码:");
         snLabel.setFont(new Font("微软雅黑", Font.PLAIN, 13));
         snLabel.setBounds(xPos, 10, 50, 30);
         topPanel.add(snLabel);
@@ -185,20 +184,12 @@ public class WorkRecordPanel extends JPanel {
 
         // 创建表格
         table = new JTable(tableModel);
+        table.setAutoCreateColumnsFromModel(true);
         table.setRowHeight(32);
         table.setFont(new Font("微软雅黑", Font.PLAIN, 13));
         table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 14));
         table.getTableHeader().setReorderingAllowed(false);
-
-        // 设置列宽
-        setColumnWidths();
-
-        // 设置居中渲染器
-        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
-        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
-        for (int i = 0; i < table.getColumnCount(); i++) {
-            table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
-        }
+        applyColumns();
 
         // 添加滚动面板
         JScrollPane scrollPane = new JScrollPane(table);
@@ -228,23 +219,25 @@ public class WorkRecordPanel extends JPanel {
     /**
      * 设置列宽
      */
-    private void setColumnWidths() {
-        TableColumn column;
-        // 工件码
-        column = table.getColumnModel().getColumn(0);
-        column.setPreferredWidth(260);
-        // 工位号
-        column = table.getColumnModel().getColumn(1);
-        column.setPreferredWidth(90);
-        // 工位结果
-        column = table.getColumnModel().getColumn(2);
-        column.setPreferredWidth(70);
-        // 作业人员
-        column = table.getColumnModel().getColumn(3);
-        column.setPreferredWidth(100);
-        // 日期
-        column = table.getColumnModel().getColumn(4);
-        column.setPreferredWidth(150);
+    private void applyColumns() {
+        tableModel.setColumnIdentifiers(COLUMN_NAMES);
+        for (int i = table.getColumnCount() - 1; i >= 0; i--) {
+            if ("主工件码".equals(table.getColumnName(i))) {
+                table.removeColumn(table.getColumnModel().getColumn(i));
+            }
+        }
+        if (table.getColumnCount() >= 5) {
+            table.getColumnModel().getColumn(0).setPreferredWidth(260);
+            table.getColumnModel().getColumn(1).setPreferredWidth(90);
+            table.getColumnModel().getColumn(2).setPreferredWidth(70);
+            table.getColumnModel().getColumn(3).setPreferredWidth(100);
+            table.getColumnModel().getColumn(4).setPreferredWidth(150);
+        }
+        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
+        centerRenderer.setHorizontalAlignment(JLabel.CENTER);
+        for (int i = 0; i < table.getColumnCount(); i++) {
+            table.getColumnModel().getColumn(i).setCellRenderer(centerRenderer);
+        }
     }
 
     /**
@@ -364,6 +357,8 @@ public class WorkRecordPanel extends JPanel {
         }
         statusLabel.setText("工位:" + oprnoDisplay + " 共" + totalCount + "条");
 
+        applyColumns();
+
         // 填充表格数据
         List<WorkRecordData> list = resp.getList();
         if (list != null && !list.isEmpty()) {
@@ -401,4 +396,16 @@ public class WorkRecordPanel extends JPanel {
     public String getOprno() {
         return oprno;
     }
+
+    /**
+     * 登录或切换工位后更新查询工位
+     */
+    public void setQueryOprno(String oprno) {
+        this.oprno = oprno;
+        this.isAllStation = (oprno == null || oprno.isEmpty());
+        if (statusLabel != null) {
+            String displayOprno = isAllStation ? "所有工位" : oprno;
+            statusLabel.setText("工位:" + displayOprno);
+        }
+    }
 }

File diff suppressed because it is too large
+ 1091 - 0
src/com/mes/util/ClientUpgrade.java


+ 75 - 0
src/com/mes/util/ClientVersionUtils.java

@@ -0,0 +1,75 @@
+package com.mes.util;
+
+/**
+ * 客户端版本号比较,格式 0.0.0 / 0.0.1。
+ * 没有点的旧数字(如 1)按 0.0.1 处理。
+ */
+public final class ClientVersionUtils {
+
+    private ClientVersionUtils() {
+    }
+
+    public static int compare(String left, String right) {
+        int[] a = parse(left);
+        int[] b = parse(right);
+        int n = Math.max(a.length, b.length);
+        for (int i = 0; i < n; i++) {
+            int va = i < a.length ? a[i] : 0;
+            int vb = i < b.length ? b[i] : 0;
+            if (va != vb) {
+                return va < vb ? -1 : 1;
+            }
+        }
+        return 0;
+    }
+
+    public static boolean isNewer(String latest, String current) {
+        return compare(latest, current) > 0;
+    }
+
+    public static String max(String left, String right) {
+        if (isBlank(left)) {
+            return normalize(right);
+        }
+        if (isBlank(right)) {
+            return normalize(left);
+        }
+        return compare(left, right) >= 0 ? normalize(left) : normalize(right);
+    }
+
+    public static String normalize(String version) {
+        if (isBlank(version)) {
+            return "0.0.0";
+        }
+        int[] parts = parse(version);
+        return parts[0] + "." + parts[1] + "." + parts[2];
+    }
+
+    private static int[] parse(String version) {
+        int[] result = new int[] {0, 0, 0};
+        if (isBlank(version)) {
+            return result;
+        }
+        String[] parts = version.trim().split("\\.");
+        if (parts.length == 1) {
+            result[2] = parseInt(parts[0]);
+            return result;
+        }
+        for (int i = 0; i < parts.length && i < 3; i++) {
+            result[i] = parseInt(parts[i]);
+        }
+        return result;
+    }
+
+    private static int parseInt(String value) {
+        try {
+            return Integer.parseInt(value.trim());
+        } catch (Exception e) {
+            return 0;
+        }
+    }
+
+    private static boolean isBlank(String value) {
+        return value == null || value.trim().length() == 0;
+    }
+}

+ 21 - 0
src/com/mes/util/HttpUtils.java

@@ -1,5 +1,7 @@
 package com.mes.util;
 
+import com.mes.ui.MesClient;
+
 import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URISyntaxException;
@@ -13,6 +15,7 @@ public class HttpUtils {
 	 */
     //http post请求
     public static String sendRequest(String urlParam) {
+        urlParam = appendSid(urlParam);
 		String requestType = "POST";
         //根据接收内容返回数据结果
     	String ret = "";
@@ -66,6 +69,7 @@ public class HttpUtils {
 
 
     public static String sendPostRequest(String urlParam, String params) {
+        urlParam = appendSid(urlParam);
         String requestType = "POST";
         //根据接收内容返回数据结果
         String ret = "";
@@ -124,6 +128,7 @@ public class HttpUtils {
     }
 
     public static String sendPostRequestJson(String apiUrl, String jsonData) throws IOException {
+        apiUrl = appendSid(apiUrl);
         URL url = new URL(apiUrl);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("POST");
@@ -154,4 +159,20 @@ public class HttpUtils {
         return response.toString();
     }
 
+
+    private static String appendSid(String url) {
+        if (url == null || url.contains("__sid=") || url.contains("/login")) {
+            return url;
+        }
+        try {
+            String sid = MesClient.sessionid;
+            if (sid == null || sid.length() == 0) {
+                return url;
+            }
+            return url + (url.contains("?") ? "&" : "?") + "__sid=" + sid;
+        } catch (Exception e) {
+            return url;
+        }
+    }
+
 }

+ 7 - 1
src/resources/config/config.properties

@@ -4,4 +4,10 @@
 #mes.server_ip=192.168.20.99
 mes.line_sn=XT
 #工件码开头格式,扫码后质量查询前校验;多个用逗号分隔
-mes.sn_prefix=40200000806,000020022029,000020021815
+#OP015
+#mes.sn_prefix=40200000806,000020022029,000020021815
+#OP023
+mes.sn_prefix=30100201543
+# 打包发布时改成和后台「JAR管理」里填的版本号一致(或更大),格式 0.0.0。
+# 每次打包切记去后台查看对应子工位最新版本,打包时版本号+1
+mes.client.version=0.0.0