App.vue 是首次加载项目时,走的一个vue页面,这个页面时全局的。

// App.vue
var server = baseUrl + "baseInfo/wmsAppVersion/update"; //检查更新地址
plus.runtime.getProperty(plus.runtime.appid, (inf) => {
    this.wgtVer = inf.version;
    uni.request({
        url: server,
        data: {
            version: this.wgtVer
        },
        success: (r) => {
            console.log(r.data)
            console.log(inf.version)
            if (r.data.success) {
                if (r.data.result.identification == 'Y') {
                    uni.showModal({
                        title: "发现新版本",
                        content: "确认下载更新",
                        success: (res) => {
                            if (res.confirm == true) { //当用户确定更新,执行更新
                                // _this.doUpData();
                                uni.showLoading({
                                    title: '更新中……'
                                })
                                uni.downloadFile({ //执行下载
                                    url: r.data.result.url, //下载地址
                                    success: downloadResult => { //下载成功
                                        uni.hideLoading();
                                        if (downloadResult.statusCode ==
                                            200
                                        ) {
                                            uni.showModal({
                                                title: '',
                                                content: '更新成功,确定现在重启吗?',
                                                confirmText: '重启',
                                                confirmColor: '#EE8F57',
                                                success: function (
                                                    q
                                                ) {
                                                    if (q.confirm ==
                                                        true) {
                                                        plus.runtime
                                                            .install( //安装
                                                                downloadResult
                                                                .tempFilePath, {
                                                                    force: true
                                                                },
                                                                function (
                                                                    res
                                                                ) {
                                                                    utils
                                                                        .showToast(
                                                                            '更新成功,重启中'
                                                                        );
                                                                    plus
                                                                        .runtime
                                                                        .restart();
                                                                }
                                                            )
                                                    } else {
                                                        plus.runtime
                                                            .quit(); // 退出应用
                                                    }
                                                }
                                            })
                                        }
                                    }
                                })

                            } else {
                                plus.runtime.quit(); // 退出应用
                            }
                        }
                    })
                }
            }
        }
    })
});
图片1

 点击‘确定’,开始下载任务,到下图所示:

图片2

 更新完毕之后,显示效果如下:

图片3

 图片1和图片3点击‘取消’都会强制退出app,再进app又会再次判断版本

欢迎大家关注我的公众号「踏浪而行生活圈」,这里不仅有更多好玩的内容,还会同步更新 踏浪而行 最新资讯,让你时刻掌握科技前沿,快来加入我们吧!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐