fix: OTA检测闭环 — 新增GET /check-update + App端uni.getSystemInfoSync版本对比
This commit is contained in:
@ -26,24 +26,35 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
// ==========================================================
|
||||
// OTA 热更新 — 版本检测 + WGT 下载 + 静默安装
|
||||
// OTA 热更新雷达 — 版本检测 + WGT 下载 + 静默安装
|
||||
// ==========================================================
|
||||
checkUpdate() {
|
||||
// 获取当前 App 版本号
|
||||
const currentVersion = plus.runtime.version;
|
||||
// 获取当前 App 的 WGT 资源版本号(uni-app 编译后的版本标识)
|
||||
const sysInfo = uni.getSystemInfoSync();
|
||||
const currentWgtVersion = sysInfo.appWgtVersion || sysInfo.appVersion || "0";
|
||||
const currentVersionCode = this.parseVersionCode(currentWgtVersion);
|
||||
const baseUrl = uni.getStorageSync("env_base_url") || "http://172.16.0.198:8011/api/v1";
|
||||
|
||||
console.log("[OTA] 当前版本:", currentVersion, "检测服务器:", baseUrl);
|
||||
console.log("[OTA] 本地WGT版本:", currentWgtVersion, "| 数字版本:", currentVersionCode);
|
||||
|
||||
uni.request({
|
||||
url: `${baseUrl}/app/version?current=${encodeURIComponent(currentVersion)}`,
|
||||
url: `${baseUrl}/app/check-update`,
|
||||
method: "GET",
|
||||
timeout: 8000,
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200) return;
|
||||
const data = res.data;
|
||||
if (!data || !data.has_update) {
|
||||
console.log("[OTA] 已是最新版本");
|
||||
if (!data || !data.wgt_url) {
|
||||
console.log("[OTA] 服务端无可用更新包");
|
||||
return;
|
||||
}
|
||||
|
||||
const serverVersionCode = data.version_code || 0;
|
||||
console.log("[OTA] 服务端版本:", data.version, "| 数字版本:", serverVersionCode);
|
||||
|
||||
// 客户端自行对比:服务端 > 本地 = 需要更新
|
||||
if (serverVersionCode <= currentVersionCode) {
|
||||
console.log("[OTA] 已是最新版本,无需更新");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -56,6 +67,15 @@ export default {
|
||||
});
|
||||
},
|
||||
|
||||
/** 从版本字符串提取数字版本号用于对比 */
|
||||
parseVersionCode(versionStr) {
|
||||
if (!versionStr) return 0;
|
||||
const nums = versionStr.match(/\d+/g);
|
||||
if (!nums) return 0;
|
||||
// 取后三位拼成整数: "T1.0.1" → [1,0,1] → 101
|
||||
return parseInt(nums.slice(-3).join("").padEnd(3, "0").slice(0, 3), 10);
|
||||
},
|
||||
|
||||
downloadAndInstall(wgtUrl, newVersion, description) {
|
||||
if (!wgtUrl) {
|
||||
console.log("[OTA] 无 WGT 下载地址");
|
||||
|
||||
Reference in New Issue
Block a user