自动更新,从多个接口更新,避免单一的接口挂掉了无法更新
This commit is contained in:
217
utils/update.js
217
utils/update.js
@ -1,106 +1,155 @@
|
||||
import request,{baseURL} from '@/utils/request.js'
|
||||
import request, {
|
||||
baseURL
|
||||
} from '@/utils/request.js'
|
||||
|
||||
/**
|
||||
* 检查并执行wgt热更新
|
||||
* @param {String} updateUrl - 检查更新的接口地址
|
||||
*/
|
||||
function checkAndUpdateWgt(updateUrl) {
|
||||
if(!plus){
|
||||
if (!plus) {
|
||||
return;
|
||||
}
|
||||
// 显示加载提示
|
||||
|
||||
let urls = ['http://114.55.111.217/app/CheckUpdate', 'https://t3v3e2xsjv.by.takin.cc/app/CheckUpdate'];
|
||||
|
||||
|
||||
|
||||
|
||||
// 1. 获取当前应用版本信息
|
||||
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
|
||||
const currentVersion = widgetInfo.version;
|
||||
console.log("当前版本:" + currentVersion);
|
||||
// request({
|
||||
// url:'/app/auth/version',
|
||||
// method: 'get'
|
||||
// }).then(res=>{
|
||||
// console.log("检查版本更新:",res);
|
||||
// let os=plus.os.name.toLowerCase();
|
||||
// if(res.code!=200){
|
||||
// return;
|
||||
// }
|
||||
// let f=res.data.find(v=>{
|
||||
// if(v.dictLabel.toLowerCase()==os){
|
||||
// return true;
|
||||
// }
|
||||
// return false;
|
||||
// });
|
||||
// if(f){
|
||||
// if(f.dictValue==currentVersion){
|
||||
// return;
|
||||
// }
|
||||
|
||||
// uni.showModal({
|
||||
// title: '检测到更新',
|
||||
// content: '当前版本“'+currentVersion+'”,发现新版本“'+f.dictValue+'”,是否立即更新?',
|
||||
// confirmText: '立即更新',
|
||||
// cancelText: '稍后更新',
|
||||
// success: (modalRes) => {
|
||||
// if (modalRes.confirm) {
|
||||
// downloadAndInstallWgt(f.remark);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
|
||||
// }).catch(ex=>{
|
||||
// console.log("检查更新失败:",ex);
|
||||
// });
|
||||
// 2. 调用后端接口检查是否有更新
|
||||
uni.request({
|
||||
url: updateUrl,
|
||||
method: 'GET',
|
||||
data: {
|
||||
currentVersion: currentVersion,
|
||||
platform: uni.getSystemInfoSync().platform
|
||||
},
|
||||
success: (res) => {
|
||||
uni.hideLoading();
|
||||
console.log("检查更新成功=", 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
|
||||
});
|
||||
|
||||
|
||||
let callbck = (res) => {
|
||||
uni.hideLoading();
|
||||
console.log("检查更新成功=", res)
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
const updateInfo = res.data.data;
|
||||
if (!updateInfo.hasUpdate) {
|
||||
return;
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.hideLoading();
|
||||
// 3. 显示更新提示
|
||||
uni.showModal({
|
||||
title: '检测到更新',
|
||||
content: updateInfo.description || '有新版本可用,是否立即更新?',
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后更新',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
downloadAndInstallWgt(updateInfo.downloadUrl);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '检查更新失败',
|
||||
title: '当前已是最新版本',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
console.error('检查更新失败:', err);
|
||||
}
|
||||
};
|
||||
|
||||
let Update = (url) => {
|
||||
console.log("url=",url);
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: url,
|
||||
method: 'GET',
|
||||
data: {
|
||||
currentVersion: currentVersion,
|
||||
platform: uni.getSystemInfoSync().platform
|
||||
},
|
||||
success: (res) => {
|
||||
console.log("res=>",res)
|
||||
res.type='1';
|
||||
resolve(res);
|
||||
},
|
||||
fail: (err) => {
|
||||
console.log("err=",err);
|
||||
reject(err);
|
||||
},
|
||||
complete:()=>{
|
||||
console.log("complete");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
let Callback1 = (res) => {
|
||||
console.log("检查版本更新:", res);
|
||||
let os = plus.os.name.toLowerCase();
|
||||
if (res.code != 200) {
|
||||
return;
|
||||
}
|
||||
let f = res.data.find(v => {
|
||||
if (v.dictLabel.toLowerCase() == os) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
if (f) {
|
||||
if (f.dictValue == currentVersion) {
|
||||
return;
|
||||
}
|
||||
|
||||
uni.showModal({
|
||||
title: '检测到更新',
|
||||
content: '当前版本“' + currentVersion + '”,发现新版本“' + f.dictValue + '”,是否立即更新?',
|
||||
confirmText: '立即更新',
|
||||
cancelText: '稍后更新',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm) {
|
||||
downloadAndInstallWgt(f.remark);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let Update1 = () => {
|
||||
return request({
|
||||
url: '/app/auth/version',
|
||||
method: 'get'
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
let promises = [];
|
||||
for (var i = 0; i < urls.length; i++) {
|
||||
promises.push(Update(urls[i]));
|
||||
}
|
||||
promises.push(Update1());
|
||||
|
||||
Promise.allSettled(promises).then(results => {
|
||||
let length = results.length;
|
||||
let flag=false;
|
||||
for (var i = 0; i < length; i++) {
|
||||
console.log('results[i]=',results[i]);
|
||||
if (results[i].status == 'fulfilled' && results[i].value.type==='1' ) {
|
||||
flag=true;
|
||||
callbck(results[i].value)
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if(results[i].status == 'fulfilled' && results[i].value.type!=='1'){
|
||||
flag=true;
|
||||
Callback1(results[i].value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!flag){
|
||||
console.log("检查更新失败了");
|
||||
}else{
|
||||
console.log("检查更新成功");
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -110,8 +159,8 @@ function checkAndUpdateWgt(updateUrl) {
|
||||
*/
|
||||
function downloadAndInstallWgt(wgtUrl) {
|
||||
// 显示下载进度
|
||||
var wating=plus.nativeUI.showWaiting({
|
||||
title:"下载中0%"
|
||||
var wating = plus.nativeUI.showWaiting({
|
||||
title: "下载中0%"
|
||||
});
|
||||
// uni.showLoading({
|
||||
// title: '更新下载中...',
|
||||
@ -184,7 +233,7 @@ function downloadAndInstallWgt(wgtUrl) {
|
||||
// 监听下载进度
|
||||
downloadTask.onProgressUpdate((progress) => {
|
||||
console.log('下载进度: ' + progress.progress + '%');
|
||||
wating.setTitle("下载中"+ progress.progress + '%');
|
||||
wating.setTitle("下载中" + progress.progress + '%');
|
||||
// 可以在这里更新自定义进度条
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user