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