uniapp app热更新

1、在app.vue里面获取当前版本号

plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
                    uni.setStorageSync('version', wgtinfo.version)
                })

2、在首页获取到后端返回的最新版本号做对比(不一定非要是首页,在哪里都行)

getSysVersion() {
                const _this = this;
                //获取缓存中,当前app的版本号
                this.version = uni.getStorageSync('version')
                console.log(this.version);
                this.$u.post('versionUpgrade').then(res => {
                    console.log(this.version);
                    console.log(String(res.data.versionNumber));
                    if (this.version != res.data.versionNumber) {
                        // 强制更新
                        if (res.data.versionDemand == 0) {
                            uni.showModal({
                                title: "发现新版本",
                                content: "确认下载更新",
                                success: (res1) => {
                                    if (res1.confirm == true) {
                                        //当用户确定更新,执行更新,下面方法进行下载app
                                        this.appdownLoad(res.data.versionAddress);
                                    } else {
                                        // 取消后退出应用
                                        if (uni.getSystemInfoSync().platform == 'ios') {
                                            plus.ios.import("UIApplication").sharedApplication()
                                                .performSelector("exit")
                                        } else if (uni.getSystemInfoSync().platform == 'android') {
                                            plus.runtime.quit();
                                        }
                                    }
                                }
                            })
                            // 非强制更新
                        } else if (res.data.versionDemand == 1) {
                            uni.showModal({
                                title: "发现新版本",
                                content: "确认下载更新",
                                success: (res1) => {
                                    if (res1.confirm == true) { //当用户确定更新,执行更新,下面方法进行下载app
                                        this.appdownLoad(res.data.versionAddress);
                                    } else {
                                        return false
                                    }
                                }
                            })
                        }

                    }
                })
            },

3、用户点击确认后开始更新

appdownLoad(url) {
                var that = this;
                uni.showLoading({
                    title: '安装包下载中……',
                    mask: true,
                })
                const downloadTask = uni.downloadFile({
                    url: url, //服务器 app 的下载地址
                    success: (downloadResult) => {
                        uni.hideLoading();
                        console.log(downloadResult)
                        if (downloadResult.statusCode === 200) {
                            uni.showModal({
                                title: '',
                                content: '下载成功,是否允许安装新版本?',
                                confirmText: '确定',
                                success: function(res) {
                                    if (res.confirm == true) {
                                        plus.runtime.install( //安装
                                            downloadResult.tempFilePath, {
                                                force: true
                                            },
                                            function(res) {
                                                utils.showToast('更新成功,重启中');
                                                plus.runtime.restart();
                                            }
                                        );
                                    }
                                }
                            });
                        }
                    }
                });
            },

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值