Forráskód Böngészése

新增版本更新功能

hou 13 órája
szülő
commit
5dd6d48789

+ 3 - 0
src/com/mes/ui/LoginFarme.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
 import com.mes.component.MesRadio;
 import com.mes.component.MesWebView;
 import com.mes.util.Base64Utils;
+import com.mes.util.ClientUpgrade;
 import com.mes.util.HttpUtils;
 
 import javax.swing.*;
@@ -91,6 +92,7 @@ public class LoginFarme extends JFrame {
 
     //登录
     public static void login() {
+        ClientUpgrade.checkIfConfigured(MesClient.welcomeWin);
         String user_str = userNameTxt.getText().toString();
         String password_str = userPasswordTxt.getText().toString();
         if(user_str.equalsIgnoreCase("")||password_str.equalsIgnoreCase("")) {
@@ -120,6 +122,7 @@ public class LoginFarme extends JFrame {
     }
     //扫码登录
     public static void scanLogin() {
+        ClientUpgrade.checkIfConfigured(MesClient.welcomeWin);
         //userNameTxt.setText("");
         //userPasswordTxt.setText("");
         String scanContent = JOptionPane.showInputDialog(null, "请扫码工牌二维码");

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

@@ -41,6 +41,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 String client_version = "0.0.0"; // 打包发布版本,需和后台 JAR 管理版本号对齐
 
     //session
     public static String sessionid = "";
@@ -139,6 +140,9 @@ public class MesClient extends JFrame {
     public static TaskParam curTaskParam = null;
 
     public static void main(String[] args) {
+        if (ClientUpgrade.handleInstallArgs(args)) {
+            return;
+        }
         if (LockUtil.getInstance().isAppActive() == true){
 //            JOptionPane.showMessageDialog(null, "已有一个程序在运行,程序退出");
             return;
@@ -149,6 +153,11 @@ public class MesClient extends JFrame {
                 try{
                     //读文件配置
                     readProperty();
+                    if (ClientUpgrade.recoverIncompleteInstall()) {
+                        return;
+                    }
+                    ClientUpgrade.cleanupInstallLeftovers();
+                    ClientUpgrade.checkIfConfigured(null);
 
                     // 显示界面
                     mesClientFrame = new MesClient();
@@ -218,6 +227,13 @@ public class MesClient extends JFrame {
         }, 100, 1* 1000);
     }
 
+    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";
@@ -225,6 +241,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_server_ip = pro.getProperty("mes.server_ip");
         mes_heart_beat_cycle = Integer.parseInt(pro.getProperty("mes.heart_beat_cycle"));
         String gw = pro.getProperty("mes.gw");

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 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 org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -18,6 +20,7 @@ public class HttpUtils {
 	 */
     //http post请求
     public static String sendRequest(String urlParam) {
+        urlParam = appendSid(urlParam);
 		String requestType = "POST";
         //根据接收内容返回数据结果
     	String ret = "";
@@ -71,6 +74,7 @@ public class HttpUtils {
 
 
     public static String sendPostRequest(String urlParam, String params) {
+        urlParam = appendSid(urlParam);
         String requestType = "POST";
         //根据接收内容返回数据结果
         String ret = "";
@@ -129,6 +133,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");
@@ -161,4 +166,20 @@ public class HttpUtils {
 
 	
 
+
+    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 - 2
src/resources/config/config.properties

@@ -1,7 +1,10 @@
 mes.gw=OP022A
-#mes.server_ip=127.0.0.1
-mes.server_ip=192.168.16.99
+mes.server_ip=127.0.0.1
+#mes.server_ip=192.168.16.99
 # mes.tcp_port 已废弃,MES 通信已全部改用 HTTP (8980)
 mes.heart_beat_cycle=60
 mes.line_sn=XT
+# 打包发布时改成和后台「JAR管理」里填的版本号一致(或更大),格式 0.0.0。
+# 每次打包切记去后台查看对应子工位最新版本,打包时版本号+1
+mes.client.version=0.0.0
 portName1=COM3