feat: OTA热更新 — 版本检测API + App端WGT下载安装 + app_versions表
This commit is contained in:
@ -4,11 +4,18 @@ import { getNotifications } from "./api/notification";
|
||||
export default {
|
||||
onLaunch() {
|
||||
console.log("生产流转 T1.0.1 启动");
|
||||
|
||||
// 无 Token 跳转登录页
|
||||
const token = uni.getStorageSync("access_token");
|
||||
if (!token) {
|
||||
uni.reLaunch({ url: "/pages/login/login" });
|
||||
return;
|
||||
}
|
||||
|
||||
// 🚀 OTA 热更新检测(仅 App 端生效)
|
||||
// #ifdef APP-PLUS
|
||||
this.checkUpdate();
|
||||
// #endif
|
||||
},
|
||||
onShow() {
|
||||
console.log("App 显示");
|
||||
@ -18,7 +25,124 @@ export default {
|
||||
console.log("App 隐藏");
|
||||
},
|
||||
methods: {
|
||||
/** 更新底部 TabBar「消息」红点角标 */
|
||||
// ==========================================================
|
||||
// OTA 热更新 — 版本检测 + WGT 下载 + 静默安装
|
||||
// ==========================================================
|
||||
checkUpdate() {
|
||||
// 获取当前 App 版本号
|
||||
const currentVersion = plus.runtime.version;
|
||||
const baseUrl = uni.getStorageSync("env_base_url") || "http://172.16.0.198:8011/api/v1";
|
||||
|
||||
console.log("[OTA] 当前版本:", currentVersion, "检测服务器:", baseUrl);
|
||||
|
||||
uni.request({
|
||||
url: `${baseUrl}/app/version?current=${encodeURIComponent(currentVersion)}`,
|
||||
method: "GET",
|
||||
timeout: 8000,
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200) return;
|
||||
const data = res.data;
|
||||
if (!data || !data.has_update) {
|
||||
console.log("[OTA] 已是最新版本");
|
||||
return;
|
||||
}
|
||||
|
||||
console.log("[OTA] 发现新版本:", data.version);
|
||||
this.downloadAndInstall(data.wgt_url, data.version, data.description);
|
||||
},
|
||||
fail: () => {
|
||||
console.log("[OTA] 版本检测网络失败,跳过");
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
downloadAndInstall(wgtUrl, newVersion, description) {
|
||||
if (!wgtUrl) {
|
||||
console.log("[OTA] 无 WGT 下载地址");
|
||||
return;
|
||||
}
|
||||
|
||||
// 弹窗询问用户是否更新
|
||||
const content = description
|
||||
? `发现新版本 ${newVersion}\n\n${description}\n\n是否立即更新?`
|
||||
: `发现新版本 ${newVersion},是否立即更新?`;
|
||||
|
||||
uni.showModal({
|
||||
title: "版本更新",
|
||||
content,
|
||||
confirmText: "立即更新",
|
||||
cancelText: "稍后再说",
|
||||
success: (modalRes) => {
|
||||
if (!modalRes.confirm) return;
|
||||
|
||||
// 显示下载进度
|
||||
uni.showLoading({ title: "下载中 0%", mask: true });
|
||||
|
||||
const downloadTask = uni.downloadFile({
|
||||
url: wgtUrl,
|
||||
success: (downloadRes) => {
|
||||
if (downloadRes.statusCode !== 200) {
|
||||
uni.hideLoading();
|
||||
uni.showToast({ title: "下载失败", icon: "none" });
|
||||
return;
|
||||
}
|
||||
|
||||
uni.showLoading({ title: "安装中...", mask: true });
|
||||
|
||||
// 调用 plus.runtime.install 安装 WGT
|
||||
plus.runtime.install(
|
||||
downloadRes.tempFilePath,
|
||||
{ force: false },
|
||||
() => {
|
||||
uni.hideLoading();
|
||||
console.log("[OTA] WGT 安装成功");
|
||||
uni.showModal({
|
||||
title: "更新完成",
|
||||
content: "新版本已安装,重启后生效。是否立即重启?",
|
||||
confirmText: "立即重启",
|
||||
cancelText: "稍后",
|
||||
success: (restartRes) => {
|
||||
if (restartRes.confirm) {
|
||||
plus.runtime.restart();
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
(err) => {
|
||||
uni.hideLoading();
|
||||
console.error("[OTA] 安装失败:", err.message);
|
||||
uni.showToast({
|
||||
title: "更新失败: " + (err.message || "未知错误"),
|
||||
icon: "none",
|
||||
duration: 4000,
|
||||
});
|
||||
}
|
||||
);
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
console.error("[OTA] 下载失败:", err.errMsg);
|
||||
uni.showToast({ title: "下载失败,请检查网络", icon: "none" });
|
||||
},
|
||||
});
|
||||
|
||||
// 下载进度回调
|
||||
if (downloadTask && downloadTask.onProgressUpdate) {
|
||||
downloadTask.onProgressUpdate((res) => {
|
||||
const pct = res.progress;
|
||||
uni.showLoading({ title: `下载中 ${pct}%`, mask: true });
|
||||
if (pct >= 100) {
|
||||
uni.showLoading({ title: "安装中...", mask: true });
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
// ==========================================================
|
||||
// TabBar 消息红点
|
||||
// ==========================================================
|
||||
async updateTabBarBadge() {
|
||||
try {
|
||||
let user = uni.getStorageSync("user");
|
||||
|
||||
Reference in New Issue
Block a user