<script>
export default {
data() {
return {
androidVersion: '',
newappUrl: '',
getVersion: '',
}
},
methods: {
AndroidCheckUpdate() {
this.http.post('api/App_Update/GetAppVersion', {}, true).then((res) => {
//后端传入数据进行处理
// this.newappUrl = this.http.ipAddress+res.rows.AppUrl;//apk下载链接
this.newappUrl = res.rows.AppUrl //apk下载链接
//后端传入最新版本的版本号,每次发布新版都要修改
this.getVersion = res.rows.Versions
console.log('res', this.newappUrl, this.getVersion)
//对版本号进行对比
if (parseFloat(res.rows.Versions) > parseFloat(this.androidVersion)) {
uni.showModal({
content: '当前版本为旧版,邀您更新最新版本',
success: (res) => {
if (res.confirm) {
uni.showLoading({
title: '正在更新中'
})
this.upload()
} else if (res.cancel) {
uni.hideLoading()
}
}
})
}
})
},
upload() {
//使用下载api(uni.downloadFile)
let uploadTask = uni.downloadFile({
url: this.newappUrl,
success: (downloadResult) => {
this.cs = downloadResult
//使用安装wgtapi(plus.runtime.install)
plus.runtime.install(downloadResult.tempFilePath, { force: true }, function () {
uni.hideLoading()
uni.showModal({
title: '系统提示',
content: '新版本已经更新完成,需要重启应用',
showCancel: false,
//用户点击确认重启
success: function (res) {
if (res.confirm) {
// 调用重启api(plus.runtime.restart)
plus.runtime.restart()
} else if (res.cancel) {
console.log('用户点击取消')
}
}
})
})
},
fail() {
plus.nativeUI.toast('安装失败')
}
})
//在这里监听用户下载进度
uploadTask.onProgressUpdate((res) => {
console.log('rr23', res)
this.pro = res.progress
console.log(this.pro)
if (this.pro == 100) {
this.isDown = false
}
})
},
},
onLaunch() {
// #ifdef APP-PLUS
const script = document.createElement('script')
script.src = './static/config.js'
document.head.appendChild(script)
// #endif
},
created() {
// #ifdef APP-PLUS
this.androidVersion = plus.runtime.version //获取版本,并且记录
uni.getSystemInfo({
success: (res) => {
if (res.platform == 'ios') {
} else if (res.platform == 'android') {
this.AndroidCheckUpdate()
}
}
})
// #endif
},
onShow() {
// #ifdef APP-PLUS
this.androidVersion = plus.runtime.version //获取版本,并且记录
uni.getSystemInfo({
success: (res) => {
if (res.platform == 'ios') {
} else if (res.platform == 'android') {
this.AndroidCheckUpdate()
}
}
})
// this.AndroidCheckUpdate()
// #endif
},
onHide: function () {
console.log('App Hide')
},
mounted() {}
}
</script>
uniapp APP版本自动更新安装的方法应用
于 2024-05-21 09:25:44 首次发布