app热更新,整包更新

热更新会自动安装

整包更新会自动打开指定地址

// 检测升级
                // #ifdef APP-PLUS
                // var ver = plus.runtime.version;
                var aid = plus.runtime.appid;
                uni.request({
                    url: this.BaseUrl + '/api/update/version?version=' + this.localVersion + '&appid=' + aid + '&_t=' + new Date().getTime(),
                    method: 'GET',
                    success: result => {
                        var data = result.data;
                        if (data.update && data.wgtUrl) {
                            this.wgtUrl = data.wgtUrl;//保存下载地址
                                uni.showModal({
                                    title: "发现新版本",
                                    content: "确认下载更新",
                                    success: (res) => {
                                        if (res.confirm) {//当用户确定更新,执行更新
                                            this.doUpData();
                                        } else if (res.cancel) {
                                            // console.log('用户点击取消');
                                        }
                                    }
                                })
                        }
                    },
                    complete: () => {
                        that.total += 1;
                    }
                });
                // #endif


doUpData() {
                uni.showLoading({
                    title: '更新中……'
                })
                uni.downloadFile({//执行下载
                    url: this.wgtUrl,
                    success: downloadResult => {//下载成功
                        if (downloadResult.statusCode === 200) {
                            uni.showModal({
                                title: '',
                                content: '更新成功,确定现在重启吗?',
                                confirmText: '重启',
                                confirmColor: '#EE8F57',
                                success: function(res) {
                                    if (res.confirm) {
                                        plus.runtime.install(//安装
                                            downloadResult.tempFilePath, {
                                                force: true
                                            },
                                            function() {
                                                // utils.showToast('更新成功,重启中');
                                                plus.runtime.restart();
                                            },
                                            function(e) {
                                                // utils.showToast('更新失败');
                                            }
                                        );
                                    }
                                }
                            });
                        }
                    },
                    complete: () => {
                        uni.hideLoading();
                    }
                });
            }

首先在 vuex 里面存入 localVersion 属性代表本地版本,在每一次 app 启动的时候发出请求,判断当前版本是否过期。如果版本过期后端返回热更新包的下载地址,然后调用下载。版本的判断不是判断的 app 打包的版本,而是存在 vuex 的内在版本。