App.vue
<script>
import {baseUrl} from './config/baseUrl.js'
export default {
onLaunch: function() {
// #ifdef APP-PLUS
uni.getSystemInfo({
success:(res) => {
//检测当前平台,如果是安卓则启动安卓更新
console.log(res.platform=="android")
if(res.platform=="android"){
this.AndroidCheckUpdate();
}
}
})
// #endif
},
methods:{
AndroidCheckUpdate() {
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
let version = wgtinfo.version //客户端版本号
console.log(version)
this.getUpdateVersion(version)
})
//this.getUpdateVersion()
},
async getUpdateVersion(version) {
// 获取当前app版本信息
let params ={
projectName:"wendeng",
actionName:"h5_pda_deposit", // 寄存PDA
appType:"android",
appVersion:version,
clientType:"byg"
}
let result = await uni.request({
url:`${baseUrl}/client/init`,
data:params,
method:'POST',
})
console.log(result,'接口')
let res = result[1].data
if(res.code ==1){
let {updateFlag, apkUrl} = res.data
//请求成功
// // 默认是不更新 是否强制更新 1=不升级,2=强制升级,3=可以升级
if(updateFlag!=1){
//更新
this.downWgt(apkUrl)
}
}else{
//请求失败
// uni.showToast({
// title:res.message,
// icon:'error'
// })
return
}
// uni.showToast({
// title:JSON.stringify(res),
// icon:"none",
// duration:100000000000000000000
// })
// alert('res.data:' + JSON.stringify(res.data))
},
downWgt(url) {
console.log(url,'开始下载')
uni.showLoading({
title: '更新中……'
})
const downloadTask = uni.downloadFile({//执行下载
url: url, //下载地址
timeout: 1000 * 60, //60秒超时时间
success: downloadResult => {//下载成功
uni.hideLoading();
console.log(downloadResult.statusCode,'下载状态')
if (downloadResult.statusCode == 200) {
uni.showModal({
title: '',
content: '更新成功,确定现在重启吗?',
confirmText: '重启',
confirmColor: '#EE8F57',
success: function(res) {
if (res.confirm == true) {
plus.runtime.install(//安装
downloadResult.tempFilePath, {
force: true
},
function(res) {
plus.runtime.restart();
}
);
}
}
});
}
},
fail: err => {
uni.hideLoading();
console.log(err)
},
complete: com => {
uni.hideLoading();
}
});
// 下载进度
// downloadTask.onProgressUpdate(res => {
// // that.$u.toast(res.progress)
// that.downloadNum = res.progress
// console.log('下载进度' + that.downloadNum);
// // console.log('已经下载的数据长度' + res.totalBytesWritten);
// // console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
// // 满足测试条件,取消下载任务。
// // if (res.progress > 50) {
// // downloadTask.abort();
// // }
// });
},
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
},
}
</script>
<style lang="scss">
/* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
@import "@/uni_modules/uview-ui/index.scss";
page{
width:100%;
height: 100%;
color: #030E1C;
}
body{
width:100%;
height: 100%;
}
view{
box-sizing: border-box;
}
</style>