uniapp中IOS安卓热更新和整包更新app更新


在App.vue中

onLaunch: function() {
        console.log('App Launch');
        // #ifdef APP-PLUS
        this.getVersion();
        // #endif
    }

 

App.vue中的methods的方法们

        // 获取APP版本号
        getVersion() {
            plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
                var version = wgtinfo.version;
                var version_num = version.split('.').join('');
                console.log(version_num, '版本号');
                this.getAppVersion(version_num);
            });
        },
        // 访问更新接口
        getAppVersion(version) {
            let that = this;
            uni.setStorageSync('version', version);
            // 获取版本号
            var system = uni.getSystemInfoSync().platform;
            // 是否更新接口(数据仅为我司定义,请勿照搬)
            that.$request
                .get('store/system/getAppVersion', {
                    system: system,
                    version_id: version
                })
                .then(res => {
                    console.log(res, '检测更新');
                    if (res.data.errno == 0) {
                        if (res.data.info != '') {
                            var type = res.data.info.version_type; //type判断为整包更新还是热更新(整包为.apk,热更新为.wgt)
                            var down_url = res.data.info.down_url; //更新包下载地址
                            uni.showModal({
                                //提醒用户更新
                                title: 'APP更新提示',
                                content: res.data.info.version_remark, //更新接口提示的信息
                                success: res => {
                                    // modal中点击确定
                                    if (res.confirm) {
                                        // 下载更新方法
                                        that.downLoadFileAndInstall(down_url, type);
                                    }
                                },
                                fail: error => {
                                    //发生错误
                                }
                            });
                        } else {
                            // 不需要更新
                        }
                    } else {
                        //发生错误
                    }
                });
        },
        // 下载更新包
        downLoadFileAndInstall(down_url, type) {
            // type仅为我司定义
            if (type == 1) {
                //热更新
                var that = this;
                plus.downloader
                    .createDownload(down_url, { filename: '_doc/update/' }, function(d, status) {
                        if (status == 200) {
                            plus.nativeUI.toast('下载wgt文件成功,安装中');
                            that.installWgt(d.filename); // 安装wgt包
                        } else {
                            plus.nativeUI.toast('下载wgt失败!');
                        }
                        plus.nativeUI.closeWaiting();
                    })
                    .start();
            } else if (type == 0) {
                // 整包
                var osname = plus.os.name;
                if (osname == 'Android') {
                    // 安卓打开网页下载
                    plus.runtime.openURL(down_url);
                } else {
                    // ios打开应用商店
                    var appleId = 123456789; //apple id  在 app conection 上传的位置可以看到  https://appstoreconnect.apple.com/
                    plus.runtime.launchApplication(
                        {
                            action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
                        },
                        function(e) {
                            console.log('Open system default browser failed: ' + e.message);
                        }
                    );
                }
            }
        },
        //更新资源包
        installWgt(path) {
            plus.runtime.install(
                path,
                {},
                function() {
                    plus.nativeUI.toast('应用资源更新完成!', function() {
                        plus.runtime.restart();
                    });
                },
                function(e) {
                    plus.nativeUI.toast('安装wgt文件失败[' + e.code + ']:' + e.message);
                }
            );
        }
    

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM