前言
第七章演示一下怎么发布H5、App、和微信小程序。
这一章讲解一下怎么在线更新APP
正言
这一章节没什么好讲的,当初是写这个功能的时候,还不知道怎么将项目运行到手机,写得很痛苦。
都是通过uni.showLoading调试的,一趟下来要打好多包。
所以章节介绍是先介绍怎么运行到手机,再将怎么在线更新APP。
直接上代码
<script>
export default {
onLoad() {
// uni.hideTabBar()
},
onLaunch: function() {
// #ifdef APP-PLUS
this.update()
// #endif
console.log('App Launch')
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
methods:{
update() {
let _this = this;
uni.hideLoading();
let system = uni.getSystemInfoSync().platform
if(system == 'android' || system == 'ios'){
let reqParams = {
url:_this.$service.api.checkVersion,
}
_this.$service.httpRequest(reqParams).then(res => {
let version = res.val;
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
//当前版本号跟数据库版本号不一致
if(inf.version != version){
uni.showModal({
title: '',
content: '是否更新最新版APP?',
confirmText: '更新',
confirmColor: '#EE8F57',
success: function(res) {
if (res.confirm == true) {
system === 'ios' ? _this.downLoadIosApk() : _this.downLoadAndroidApk();
}
if (res.cancel == true) {
uni.showLoading({title: '当前APP非最新版无法使用...'})
setTimeout(()=>{
_this.update()
},5000)
}
}
});
}
})
})
}
},
downLoadAndroidApk() {
//当前app不是最新版本,执行下载最新版本APP
uni.showLoading({
title: '正在更新APP,请耐心等待...'
})
uni.downloadFile({//执行下载
url: this.$service.api.downLoadAndroidUrl, //下载地址
success: downloadResult => {//下载成功
if (downloadResult.statusCode == 200) {
uni.showLoading({
title: '更新成功,安装后自动重启...'
})
setTimeout(()=>{
plus.runtime.install(//安装
downloadResult.tempFilePath, {
force: true
},
function(res) {
utils.showToast('更新成功,重启中');
plus.runtime.restart();
}
);
},5000)
}
}
});
},
downLoadIosApk() {
//提示IOS用户去APPstore更新最新版本的APP
uni.showLoading({
title: '尊敬的IOS用户,请您移步到App Store更新到最新版本'
})
setTimeout(()=>{
uni.hideLoading();
},5000)
}
}
}
</script>
<style lang="scss">
/*每个页面公共css */
@import "uview-ui/index.scss";
page{color: #222222;font-size: 30rpx;}
</style>
总结
貌似DCloud有很多解决方案,但是看起来有点点复杂,秉着怎么容易怎么来,我就不写太多冗余的内容了。只关注代码层。