650完成,670完成部分
This commit is contained in:
153
utils/update.js
Normal file
153
utils/update.js
Normal file
@ -0,0 +1,153 @@
|
||||
/**
|
||||
* 检查并执行wgt热更新
|
||||
* @param {String} updateUrl - 检查更新的接口地址
|
||||
*/
|
||||
function checkAndUpdateWgt(updateUrl) {
|
||||
if(!plus){
|
||||
return;
|
||||
}
|
||||
// 显示加载提示
|
||||
|
||||
|
||||
// 1. 获取当前应用版本信息
|
||||
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
|
||||
const currentVersion = widgetInfo.version;
|
||||
console.log("当前版本:" + currentVersion);
|
||||
// 2. 调用后端接口检查是否有更新
|
||||
uni.request({
|
||||
url: updateUrl,
|
||||
method: 'GET',
|
||||
data: {
|
||||
currentVersion: currentVersion,
|
||||
platform: uni.getSystemInfoSync().platform
|
||||
},
|
||||
success: (res) => {
|
||||
uni.hideLoading();
|
||||
console.log("res=", res)
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
const updateInfo = res.data.data;
|
||||
if (!updateInfo.hasUpdate) {
|
||||
return;
|
||||
}
|
||||
// 3. 显示更新提示
|
||||
uni.showModal({
|
||||
title: '检测到更新',
|
||||
content: updateInfo.description || '有新版本可用,是否立即更新?',
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后更新',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
downloadAndInstallWgt(updateInfo.downloadUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '当前已是最新版本',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '检查更新失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
console.error('检查更新失败:', err);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载并安装wgt更新包
|
||||
* @param {String} wgtUrl - wgt包下载地址
|
||||
*/
|
||||
function downloadAndInstallWgt(wgtUrl) {
|
||||
// 显示下载进度
|
||||
var wating=plus.nativeUI.showWaiting({
|
||||
title:"下载中0%"
|
||||
});
|
||||
// uni.showLoading({
|
||||
// title: '更新下载中...',
|
||||
// mask: true
|
||||
// });
|
||||
|
||||
// 1. 下载wgt包
|
||||
const downloadTask = uni.downloadFile({
|
||||
url: wgtUrl,
|
||||
success: (downloadRes) => {
|
||||
wating.setTitle("下载完成,正在安装");
|
||||
if (downloadRes.statusCode === 200) {
|
||||
// 2. 安装wgt包
|
||||
plus.runtime.install(downloadRes.tempFilePath, {
|
||||
force: true // 是否强制安装
|
||||
}, () => {
|
||||
uni.removeSavedFile({
|
||||
filePath: downloadRes.tempFilePath,
|
||||
success() {
|
||||
console.log("临时文件已删除");
|
||||
},
|
||||
fail() {
|
||||
console.log("无法删除临时文件");
|
||||
},
|
||||
complete() {
|
||||
wating.close();
|
||||
uni.showModal({
|
||||
title: '更新完成',
|
||||
content: '应用已更新,是否重启应用?',
|
||||
showCancel: false,
|
||||
success: () => {
|
||||
// 3. 重启应用
|
||||
plus.runtime.restart();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}, (error) => {
|
||||
|
||||
wating.close();
|
||||
uni.showToast({
|
||||
title: '安装失败: ' + error.message,
|
||||
icon: 'none',
|
||||
duration: 3000
|
||||
});
|
||||
console.error('wgt安装失败:', error);
|
||||
});
|
||||
|
||||
} else {
|
||||
wating.close();
|
||||
uni.showToast({
|
||||
title: '下载失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
uni.showToast({
|
||||
title: '下载失败: ' + err.errMsg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
console.error('wgt下载失败:', err);
|
||||
}
|
||||
});
|
||||
|
||||
// 监听下载进度
|
||||
downloadTask.onProgressUpdate((progress) => {
|
||||
console.log('下载进度: ' + progress.progress + '%');
|
||||
wating.setTitle("下载中"+ progress.progress + '%');
|
||||
// 可以在这里更新自定义进度条
|
||||
});
|
||||
}
|
||||
|
||||
export default {
|
||||
checkAndUpdateWgt
|
||||
};
|
Reference in New Issue
Block a user