admin管理员组文章数量:1530018
检查的方法是放在app.vue里面的
<script>
import { hostCharge } from "util/httpUrl.js"
import Util from "util/util.js"
export default {
onLaunch: function() {
// console.log('App Launch')
//#ifdef APP-PLUS
this.checkUpdateVersion()
//#endif
},
methods: {
//检查版本更新
async checkUpdateVersion(){
//锁定屏幕方向
plus.screen.lockOrientation('portrait-primary')
try{
//获取版本更新
let { data: { data, code, flag, message } } = await Util.httpRequest({
url: `${hostCharge}/check/version`,
data: {
versionCode: uni.getStorageSync('versionCode')
}
})
if(code === 20000){
uni.showModal({
title: '温馨提示',
content: `有新的版本可下载${data.versionCode}是否下载?`,
success:(res) => {
if(res.confirm){
console.log(res.confirm)
//判断手机类型
// 也可以用5+app的方法:plus.os.name.toLowerCase() == 'ios'
if(uni.getSystemInfoSync().platform === 'ios'){
plus.runtime.openURL(data.updateUrl)
} else {
//新建下载任务
var downloadTask = plus.downloader.createDownload(data.updateUrl, {}, (download,status) => {
if(status === 200){
//安装APK文件
plus.runtime.install(download.filename, {}, function(error) {
uni.showModal({
showCancle: false,
content: '安装失败'
})
})
} else {
uni.showModal({
showCancle: false,
title: '更新失败!!!'
})
}
})
try {
// 开始下载
downloadTask.start()
//监听下载
var that = this
var showLoading = plus.nativeUI.showWaiting("正在下载")
downloadTask.addEventListener('statechanged', function(download,status){
switch (download.state) {
case 1:
uni.showToast({
title: '下载中'
})
break;
case 2:
//出现下载弹框和进度
that.$scope.globalData.updateProgress.popup = true
that.$scope.globalData.updateProgress.percent = parseInt(parseFloat(download.downloadedSize) / parseFloat(download.totalSize) * 100)
// uni.showLoading({
// title: `正在下载${that.$scope.globalData.updateProgress.percent}%`
// })
break;
case 3:
plus.nativeUI.closeWaiting()
}
})
} catch (err) {
plus.nativeUI.closeWaiting()
uni.showModal({
showCancle: false,
content: '下载失败,请稍后重试'
})
}
}
//检验过一次版本就加入缓存,不再检测
uni.setStorageSync('isCheckVersion', true)
}
}
})
} else {
uni.showModal({
showCancle: false,
content: message
})
}
} catch(e) {
uni.showModal({
showCancle: false,
content: '获取数据失败'
})
}
}
},
globalData: {
userInfo: {},
restaurantData: {},
//下载弹窗和进度条
updateProgress: {
popup: false,
precent: 0
}
}
}
</script>
在首页的onReady生命周期进行判断updateProgress的popup,为true的显示弹框,对precent进行赋值
弹框我就没展示了,虽然页面有点丑,哈哈哈
版权声明:本文标题:uniapp检查app更新并下载,监听下载进度 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1726628600a1079003.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论