实现app更新其实很简单,就是什么时候更新,更新包资源下载...
第一步 获取在线资源包和版本号,根据更新类型选择下载的文件 废话不多说直接上代码
//获取更新包
AppUpdateInfo() {
uni.request({
url: 'xxx',
method: 'GET',
header: uni.getStorageSync('header'),
success: (res) => {
console.log(res.data, 'dfasdadsfdsaf')
if (Number(res.data.versionCode) > Number(this.version)) {
console.log(res);
uni.showModal({
title: '提示',
content: '发现有新版本可以升级',
cancelText: '取消更新',
confirmText: '立即更新',
success: res1 => {
if (res1.confirm) {
console.log('用户点击确定');
if (res.data.type === 'wgt') {
this.downWgt(res.data.wgtUrl)
} else {
this.downApk(res)
}
} else if (res1.cancel) {
console.log('用户点击取消');
}
},
fail: (err) => {
console.log('下载失败', err);
}
})
} else {
if(this.isShow){
uni.showToast({
title: '暂无新版本',
duration: 1000,
icon:'none'
});
}
}
},
fail(e) {
console.log(e, 'fail');
}
})
}
第二步 下载资源 根据接口返回类型判断下载wgt还是apk,apk下载我这边采用是跳转浏览器的方式比较简单
downWgt(url) {
uni.showToast({
title: '更新中...',
duration: 6 * 1000 * 10,
icon: 'none'
});
this.downloadTask = uni.downloadFile({
url,
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
plus.runtime.install(
downloadResult.tempFilePath, {
force: false
},
() => {
uni.hideToast();
plus.nativeUI.toast('最新版本下载完成')
plus.runtime.restart();
}, (e) => {
uni.hideToast();
plus.nativeUI.toast("补丁安装失败")
});
}
},
fail: (err) => {
uni.hideToast();
plus.nativeUI.toast("补丁下载失败")
}
})
}
downApk(res) {
if (this.platform === 'android') {
plus.runtime.openURL(res.data.downloadUrl);
} else {
plus.runtime.openURL(res.data.iosdownloadUrl);
}
}
下面是完整代码 仅供参考。建议在App.vue中调用
// #ifdef APP-PLUS
export default class updateApp {
//手机类型
platform = uni.getSystemInfoSync().platform;
// platform = 1101
//app版本
version = void 0
//wgt实例
downloadTask = null
//没有更新版本是否提示
isShow = false
constructor(isShow=false) {
// 获取app版本
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
this.version = widgetInfo.version;
uni.setStorageSync('versionCode', this.version)
});
this.isShow=isShow
}
//获取更新包
AppUpdateInfo() {
uni.request({
url: 'xxx',
method: 'GET',
header: uni.getStorageSync('header'),
success: (res) => {
console.log(res.data, 'dfasdadsfdsaf')
if (Number(res.data.versionCode) > Number(this.version)) {
console.log(res);
uni.showModal({
title: '提示',
content: '发现有新版本可以升级',
cancelText: '取消更新',
confirmText: '立即更新',
success: res1 => {
if (res1.confirm) {
console.log('用户点击确定');
if (res.data.type === 'wgt') {
this.downWgt(res.data.wgtUrl)
} else {
this.downApk(res)
}
} else if (res1.cancel) {
console.log('用户点击取消');
}
},
fail: (err) => {
console.log('下载失败', err);
}
})
} else {
if(this.isShow){
uni.showToast({
title: '暂无新版本',
duration: 1000,
icon:'none'
});
}
}
},
fail(e) {
console.log(e, 'fail');
}
})
}
downWgt(url) {
uni.showToast({
title: '更新中...',
duration: 6 * 1000 * 10,
icon: 'none'
});
this.downloadTask = uni.downloadFile({
url,
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
plus.runtime.install(
downloadResult.tempFilePath, {
force: false
},
() => {
uni.hideToast();
plus.nativeUI.toast('最新版本下载完成')
plus.runtime.restart();
}, (e) => {
uni.hideToast();
plus.nativeUI.toast("补丁安装失败")
});
}
},
fail: (err) => {
uni.hideToast();
plus.nativeUI.toast("补丁下载失败")
}
})
}
downApk(res) {
if (this.platform === 'android') {
plus.runtime.openURL(res.data.downloadUrl);
} else {
plus.runtime.openURL(res.data.iosdownloadUrl);
}
}
cancel() {
if (this.downloadTask) {
this.downloadTask.abort()
this.downloadTask = null
}
}
}
// #endif
接口参数说明:
versionCode:版本号; versionName:版本名称; versionInfo:版本信息; updateType:更新类型; downloadUrl:安卓apk地址; iosdownloadUrl:ios地址; type:下载包类型; wgtUrl:wgt地址