1
This commit is contained in:
@@ -1,14 +1,7 @@
|
||||
package com.tashow.erp.controller;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.tashow.erp.entity.UpdateStatusEntity;
|
||||
import com.tashow.erp.repository.UpdateStatusRepository;
|
||||
import com.tashow.erp.service.IAuthService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
@@ -31,12 +24,7 @@ public class UpdateController {
|
||||
private String currentVersion;
|
||||
@Value("${project.build.time:}")
|
||||
private String buildTime;
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
@Autowired
|
||||
private IAuthService authService;
|
||||
|
||||
@Autowired
|
||||
private UpdateStatusRepository updateStatusRepository;
|
||||
// 简化:移除与鉴权/版本转发/SQLite 存储相关的依赖
|
||||
|
||||
// 下载进度跟踪
|
||||
private volatile int downloadProgress = 0;
|
||||
@@ -61,137 +49,9 @@ public class UpdateController {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查版本更新
|
||||
*
|
||||
* @return 版本更新信息
|
||||
*/
|
||||
@GetMapping("/check")
|
||||
public Map<String, Object> checkUpdate() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
String response = authService.checkVersion(currentVersion);
|
||||
// 解析JSON响应
|
||||
JsonNode responseNode = objectMapper.readTree(response);
|
||||
|
||||
result.put("success", true);
|
||||
result.put("currentVersion", currentVersion);
|
||||
|
||||
// 检查响应格式并提取数据
|
||||
JsonNode dataNode = null;
|
||||
dataNode = responseNode.get("data");
|
||||
|
||||
// 直接使用服务器返回的needUpdate字段
|
||||
boolean needUpdate = dataNode.has("needUpdate") &&
|
||||
dataNode.get("needUpdate").asBoolean();
|
||||
// 添加跳过版本信息,让前端自己判断
|
||||
String skippedVersion = updateStatusRepository.findByKeyName("skippedUpdateVersion")
|
||||
.map(entity -> entity.getValueData()).orElse(null);
|
||||
result.put("skippedVersion", skippedVersion);
|
||||
|
||||
result.put("needUpdate", needUpdate);
|
||||
|
||||
// 获取最新版本号
|
||||
if (dataNode.has("latestVersion")) {
|
||||
result.put("latestVersion", dataNode.get("latestVersion").asText());
|
||||
}
|
||||
|
||||
if (needUpdate) {
|
||||
if (dataNode.has("downloadUrl")) {
|
||||
String downloadUrl = dataNode.get("downloadUrl").asText();
|
||||
result.put("downloadUrl", downloadUrl);
|
||||
saveUpdateInfo("downloadUrl", downloadUrl);
|
||||
}
|
||||
if (dataNode.has("updateTime")) {
|
||||
// 转换时间戳为可读格式
|
||||
long updateTime = dataNode.get("updateTime").asLong();
|
||||
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String releaseDate = sdf.format(new java.util.Date(updateTime));
|
||||
result.put("releaseDate", releaseDate);
|
||||
saveUpdateInfo("releaseDate", releaseDate);
|
||||
}
|
||||
if (dataNode.has("updateNotes")) {
|
||||
String updateNotes = dataNode.get("updateNotes").asText();
|
||||
result.put("updateNotes", updateNotes);
|
||||
saveUpdateInfo("updateNotes", updateNotes);
|
||||
}
|
||||
if (dataNode.has("fileSize")) {
|
||||
String fileSize = dataNode.get("fileSize").asText();
|
||||
result.put("fileSize", fileSize);
|
||||
saveUpdateInfo("fileSize", fileSize);
|
||||
}
|
||||
if (dataNode.has("latestVersion")) {
|
||||
String latestVersion = dataNode.get("latestVersion").asText();
|
||||
saveUpdateInfo("latestVersion", latestVersion);
|
||||
}
|
||||
} else {
|
||||
// 如果不需要更新,清理之前保存的更新信息
|
||||
clearUpdateInfo();
|
||||
}
|
||||
|
||||
// 从SQLite读取之前保存的更新信息
|
||||
result.putAll(getStoredUpdateInfo());
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
result.put("message", "版本检查失败:" + e.getMessage());
|
||||
authService.reportError("UPDATE_CHECK_ERROR", "Web版本检查失败", e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存更新信息到SQLite
|
||||
*/
|
||||
private void saveUpdateInfo(String key, String value) {
|
||||
try {
|
||||
UpdateStatusEntity entity = updateStatusRepository.findByKeyName(key)
|
||||
.orElse(new UpdateStatusEntity());
|
||||
entity.setKeyName(key);
|
||||
entity.setValueData(value);
|
||||
updateStatusRepository.save(entity);
|
||||
} catch (Exception e) {
|
||||
System.err.println("保存更新信息失败: " + key + " = " + value + ", 错误: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从SQLite获取存储的更新信息
|
||||
*/
|
||||
private Map<String, Object> getStoredUpdateInfo() {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
try {
|
||||
String[] keys = {"downloadUrl", "releaseDate", "updateNotes", "fileSize", "latestVersion"};
|
||||
for (String key : keys) {
|
||||
updateStatusRepository.findByKeyName(key).ifPresent(entity ->
|
||||
info.put(key, entity.getValueData())
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("读取更新信息失败: " + e.getMessage());
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理更新信息
|
||||
*/
|
||||
private void clearUpdateInfo() {
|
||||
try {
|
||||
String[] keys = {"downloadUrl", "releaseDate", "updateNotes", "fileSize", "latestVersion"};
|
||||
for (String key : keys) {
|
||||
updateStatusRepository.findByKeyName(key).ifPresent(entity ->
|
||||
updateStatusRepository.delete(entity)
|
||||
);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("清理更新信息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取下载进度
|
||||
*
|
||||
@@ -243,7 +103,6 @@ public class UpdateController {
|
||||
@PostMapping("/cancel")
|
||||
public Map<String, Object> cancelDownload() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
|
||||
if ("downloading".equals(downloadStatus)) {
|
||||
downloadCancelled = true;
|
||||
downloadStatus = "cancelled";
|
||||
@@ -251,10 +110,9 @@ public class UpdateController {
|
||||
result.put("success", true);
|
||||
result.put("message", "下载已取消");
|
||||
} else if ("completed".equals(downloadStatus)) {
|
||||
// 下载完成时点击取消,不删除文件,只是标记为稍后更新
|
||||
// 下载完成后“稍后更新”:仅关闭弹窗,不触发安装,不改变文件
|
||||
result.put("success", true);
|
||||
result.put("message", "已设置为稍后更新,文件保留");
|
||||
System.out.println("用户选择稍后更新,文件路径: " + tempUpdateFilePath);
|
||||
result.put("message", "已设置为稍后更新");
|
||||
} else {
|
||||
result.put("success", false);
|
||||
result.put("message", "无效的操作状态");
|
||||
@@ -388,6 +246,7 @@ public class UpdateController {
|
||||
result.put("downloadUrl", downloadUrl);
|
||||
String finalDownloadUrl = downloadUrl;
|
||||
new Thread(() -> {
|
||||
|
||||
performUpdate(finalDownloadUrl);
|
||||
}).start();
|
||||
|
||||
@@ -585,19 +444,19 @@ public class UpdateController {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存跳过的版本
|
||||
*/
|
||||
@PostMapping("/skip-version")
|
||||
public Map<String, Object> saveSkippedVersion(@RequestBody Map<String, String> request) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
saveUpdateInfo("skippedUpdateVersion", request.get("version"));
|
||||
result.put("success", true);
|
||||
} catch (Exception e) {
|
||||
result.put("success", false);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
// /**
|
||||
// * 保存跳过的版本
|
||||
// */
|
||||
// @PostMapping("/skip-version")
|
||||
// public Map<String, Object> saveSkippedVersion(@RequestBody Map<String, String> request) {
|
||||
// Map<String, Object> result = new HashMap<>();
|
||||
// try {
|
||||
// saveUpdateInfo("skippedUpdateVersion", request.get("version"));
|
||||
// result.put("success", true);
|
||||
// } catch (Exception e) {
|
||||
// result.put("success", false);
|
||||
// }
|
||||
// return result;
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user