乐于分享
好东西不私藏

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

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

1、封装一个组件,方便在任何地方调用
<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 {                versionShowfalse//是否弹出更新提示框                progressShowfalse//是否显示进度条                android: {}, //是否更新数据,请求接口后存起来备用。                progress0//下载进度            }        },        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_updatereturn                                        this.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,                                {forcefalse},                                () => {                                    uni.showModal({                                        title'系统提示',                                        content:'新版本已经更新完成,请重启应用',                                        cancelText:'取消',                                        confirmText:'确定',                                        success:  (res)=> {                                            if (res.confirm) {                                                plus.runtime.restart();                                            }                                        }                                    });                                },                                err => {                                    uni.showModal({                                        title'安装失败',                                        content: err.code+err.message,                                        showCancelfalse                                    });                                });                            }else{                                // apk整包安装 升级                                plus.runtime.install(res.tempFilePath)                            }                        }                    },                    fail(err) => {                        console.log(err);                        uni.showToast({                            icon'none',                            masktrue,                            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 {                width590rpx;                .text {                    border-radius8rpx;                    padding290rpx 30rpx 20rpx 30rpx;                    min-height270rpx;                    position: relative;                    backgroundurl('../../static/icon/2.png') no-repeat left top #fff;                    background-size100% 260rpx;                    .cont {                        padding10rpx 35rpx;                        height120rpx;                        background#F7F7F7;                        border-radius10rpx;                        margin: auto;                        overflow: hidden;                        uni-text {                            line-height40rpx;                            display: block;                            font-size24rpx;                            color#000;                            word-break:break-all;                         }                    }                    .btn {                        height78rpx;                        margin24rpx auto;                        .btn1 {                            border-radius78rpx;                            height78rpx;                            line-height78rpx;                            background#FF9901;                            font-weight24rpx;                            color#fff;                            margin: auto;                        }                    }                }                .bom {                    display: block;                    width64rpx;                    height64rpx;                    margin100rpx auto 0;                }            }        }    }</style>
2、首页调用组件出触发更新
<!-- 版本更新 --><version:show="showVersion"></version>
data() {    return {        showVersion:false,    }},onLoad() {    // #ifdef APP    this.$nextTick(() => {        this.showVersion = true;    })    // #endif},

请在微信客户端打开

本站文章均为手工撰写未经允许谢绝转载:夜雨聆风 » uniapp:版本更新,apk下载安装功能

评论 抢沙发

3 + 9 =
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
×
订阅图标按钮