feat: 增加了更新的代码实现
This commit is contained in:
@@ -1,27 +1,83 @@
|
||||
export const updateManager = () => {
|
||||
// #ifdef APP-PLUS
|
||||
import checkUpdate from "@/uni_modules/uni-upgrade-center-app/utils/check-update";
|
||||
// #endif
|
||||
|
||||
let isCheckingAppUpdate = false;
|
||||
let hasRegisteredMiniProgramUpdate = false;
|
||||
const APP_UPDATE_CHECK_TIMEOUT = 30000;
|
||||
|
||||
const formatUpdateError = (error) => {
|
||||
if (!error) return "";
|
||||
if (typeof error === "string") return error;
|
||||
return error.message || error.errMsg || JSON.stringify(error);
|
||||
};
|
||||
|
||||
export const checkMiniProgramUpdate = () => {
|
||||
// #ifdef MP-WEIXIN || MP-TOUTIAO
|
||||
const updateManager = uni.getUpdateManager();
|
||||
if (hasRegisteredMiniProgramUpdate) return;
|
||||
hasRegisteredMiniProgramUpdate = true;
|
||||
|
||||
const miniProgramUpdateManager = uni.getUpdateManager();
|
||||
|
||||
// 当向小程序后台请求完新版本信息,会进行回调
|
||||
updateManager.onCheckForUpdate((res) => {
|
||||
miniProgramUpdateManager.onCheckForUpdate((res) => {
|
||||
console.log("onCheckForUpdate res:", res.hasUpdate);
|
||||
});
|
||||
|
||||
// 当新版本下载完成,会进行回调
|
||||
updateManager.onUpdateReady(() => {
|
||||
miniProgramUpdateManager.onUpdateReady(() => {
|
||||
uni.showModal({
|
||||
title: "更新提示",
|
||||
content: "新版本已经准备好,是否重启应用?",
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
||||
updateManager.applyUpdate();
|
||||
miniProgramUpdateManager.applyUpdate();
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// 当新版本下载失败,会进行回调
|
||||
updateManager.onUpdateFailed(() => {});
|
||||
miniProgramUpdateManager.onUpdateFailed(() => {});
|
||||
// #endif
|
||||
};
|
||||
|
||||
export const checkAppUpdate = () => {
|
||||
// #ifdef APP-PLUS
|
||||
if (isCheckingAppUpdate) return Promise.resolve(null);
|
||||
|
||||
isCheckingAppUpdate = true;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let finished = false;
|
||||
const finish = (result = null) => {
|
||||
if (finished) return;
|
||||
finished = true;
|
||||
clearTimeout(timeoutTimer);
|
||||
isCheckingAppUpdate = false;
|
||||
resolve(result);
|
||||
};
|
||||
|
||||
const timeoutTimer = setTimeout(() => {
|
||||
finish();
|
||||
}, APP_UPDATE_CHECK_TIMEOUT);
|
||||
|
||||
checkUpdate()
|
||||
.then(finish)
|
||||
.catch((error) => {
|
||||
console.warn("App更新检查失败:", formatUpdateError(error));
|
||||
finish();
|
||||
});
|
||||
});
|
||||
// #endif
|
||||
|
||||
// #ifndef APP-PLUS
|
||||
return Promise.resolve(null);
|
||||
// #endif
|
||||
};
|
||||
|
||||
export const updateManager = () => {
|
||||
checkMiniProgramUpdate();
|
||||
return checkAppUpdate();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user