瀏覽代碼

新增客户端版本更新功能

hou 1 天之前
父節點
當前提交
fd58e5106a

+ 17 - 0
src/com/mes/ui/DataUtil.java

@@ -218,6 +218,7 @@ public class DataUtil {
     }
 
     public static String doPost(String httpUrl, String param) {
+        httpUrl = appendSid(httpUrl);
         HttpURLConnection connection = null;
         InputStream is = null;
         OutputStream os = null;
@@ -364,4 +365,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;
+        }
+    }
+
 }

+ 3 - 0
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, "请扫码工牌二维码");

+ 21 - 1
src/com/mes/ui/MesClient.java

@@ -5,6 +5,8 @@ import com.mes.component.MesRadio;
 import com.mes.component.MesWebView;
 import com.fazecast.jSerialComm.SerialPort;
 import com.mes.component.ProductTypePanel;
+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;
@@ -42,7 +44,8 @@ public class MesClient extends JFrame {
     public static String mes_server_ip = ""; // 服务器IP地址
     public static int mes_heart_icon_cycle = 1;
     public static boolean mes_ut5310_enable = false;
-    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_plc_ip = "192.168.0.3"; // PLC IP地址
 
     public static final int PLC_PHASE_IDLE = 0;
@@ -130,6 +133,9 @@ public class MesClient extends JFrame {
     private static final ExecutorService ut5310Executor = Executors.newSingleThreadExecutor();
 
     public static void main(String[] args) {
+        if (ClientUpgrade.handleInstallArgs(args)) {
+            return;
+        }
 
 //        if (LockUtil.getInstance().isAppActive() == false){
 ////            JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
@@ -143,6 +149,11 @@ public class MesClient extends JFrame {
 
                         //读文件配置
                         readProperty();
+                        if (ClientUpgrade.recoverIncompleteInstall()) {
+                            return;
+                        }
+                        ClientUpgrade.cleanupInstallLeftovers();
+                        ClientUpgrade.checkIfConfigured(null);
 
                         // 显示界面
                         mesClientFrame = new MesClient();
@@ -160,6 +171,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");
@@ -173,6 +192,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");
         String plcIp = pro.getProperty("mes.plc_ip");

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;
+        }
+    }
+
 }

+ 3 - 0
src/resources/config/config.properties

@@ -20,3 +20,6 @@ mes.ut5310_modbus_max_steps=4
 mes.ut5310_modbus_expected_steps=2
 # false=工装/PLC已触发测试,MES只读结果;true=MES通过0x0500发START
 mes.ut5310_modbus_send_start=true
+# 打包发布时改成和后台「JAR管理」里填的版本号一致(或更大),格式 0.0.0。
+# 每次打包切记去后台查看对应子工位最新版本,打包时版本号+1
+mes.client.version=0.0.0