uniapp:版本更新,apk下载安装功能



<template><viewclass="version"><u-popupv-model="versionShow"mode="center":mask-close-able="false"><viewclass="upVersion"><viewclass="text"><viewclass="fsz-26 fw-b mb-30">版本升级</view><viewclass="cont u-line-3"><textv-html="android.update_desc"></text></view><viewclass="btn"><u-buttonclass="btn1"v-if="!progressShow" @click="up">立即升级</u-button><u-line-progressv-elseheight="78"active-color="#FF9901 ":percent="progress"></u-line-progress></view></view><imageclass="bom"src="../../static/icon/3.png"mode="" @click="close"v-if="!android.is_force"></image></view></u-popup></view></template><script>export default {props: {//show: 加载组件,但是不弹出提示框show: {type: [Boolean],},//url: 如果是首页,不提示 已是最新版本url: {type: [String],}},data() {return {versionShow: false, //是否弹出更新提示框progressShow: false, //是否显示进度条android: {}, //是否更新数据,请求接口后存起来备用。progress: 0, //下载进度}},watch: {//show:监听组件加载,判断是否需要弹出更新提示show(newVal, oldVal) {if (newVal) {plus.runtime.getProperty(plus.runtime.appid, (appid) => {uni.getSystemInfo({success:(res)=>{this.$http.get('/api/appVersion/checkUpdate',{version:appid.version,device: res.osName // 设备类型 ios/android}).then(res=>{if(res.code == 1){if(!res.data.has_update) returnthis.android = res.data;if(this.android.is_force){console.log('隐藏tabbar');this.$emit('data', false);}if(this.android.download_url){this.versionShow = true; //可以弹出更新提示框}}})}})})}}},methods: {//升级up() {let _this = this;this.progress = 0;this.progressShow = true; //切换进度条显示//开启下载任务const downloadTask = uni.downloadFile({url: _this.android.download_url,success: (res) => {if (res.statusCode === 200) {// wgt热更新 升级if(this.android.type == 'wgt'){plus.runtime.install(res.tempFilePath,{force: false},() => {uni.showModal({title: '系统提示',content:'新版本已经更新完成,请重启应用',cancelText:'取消',confirmText:'确定',success: (res)=> {if (res.confirm) {plus.runtime.restart();}}});},err => {uni.showModal({title: '安装失败',content: err.code+err.message,showCancel: false});});}else{// apk整包安装 升级plus.runtime.install(res.tempFilePath)}}},fail: (err) => {console.log(err);uni.showToast({icon: 'none',mask: true,title: '安装失败,请重新下载',});if(this.android.is_force){this.$emit('data', true);}setTimeout(() => {_this.close();}, 1500)},// 接口调用结束complete() {downloadTask.offProgressUpdate(); //取消监听加载进度setTimeout(() => {_this.close();}, 1500)}});//监听下载进度downloadTask.onProgressUpdate((res) => {_this.progress = res.progress;});},close() {this.versionShow = false; //关闭更新弹窗this.progressShow = false; //关闭进度条this.$emit('closeVersion'); //关闭show组件if(this.android.is_force){this.$emit('data', true);}}},}</script><stylescopedlang="scss">.version {/deep/.u-drawer {.u-mode-center-box {background: transparent;}.upVersion {width: 590rpx;.text {border-radius: 8rpx;padding: 290rpx 30rpx 20rpx 30rpx;min-height: 270rpx;position: relative;background: url('../../static/icon/2.png') no-repeat left top #fff;background-size: 100% 260rpx;.cont {padding: 10rpx 35rpx;height: 120rpx;background: #F7F7F7;border-radius: 10rpx;margin: auto;overflow: hidden;uni-text {line-height: 40rpx;display: block;font-size: 24rpx;color: #000;word-break:break-all;}}.btn {height: 78rpx;margin: 24rpx auto;.btn1 {border-radius: 78rpx;height: 78rpx;line-height: 78rpx;background: #FF9901;font-weight: 24rpx;color: #fff;margin: auto;}}}.bom {display: block;width: 64rpx;height: 64rpx;margin: 100rpx auto 0;}}}}</style>
<!-- 版本更新 --><version:show="showVersion"></version>
data() {return {showVersion:false,}},onLoad() {// #ifdef APPthis.$nextTick(() => {this.showVersion = true;})// #endif},
请在微信客户端打开
夜雨聆风
