目 录CONTENT

文章目录

uniapp实现app的强制更新

萧瑟
2023-01-19 / 0 评论 / 0 点赞 / 566 阅读 / 1,141 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2023-08-04,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

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和图片3点击‘取消’都会强制退出app,再进app又会再次判断版本

weixin

0

评论区