uni-app 更新、热更新

2020-10-31  本文已影响0人  MiSiTeWang
// 更新
            this.$u.api.setting({}).then(res => {    //请求封装调用接口
                for (let app of res) {
                    if (app.key == "app_version") {
                        // android应用版本
                        var app_version = app.value
                    } else if (app.key == "app_download") {
                        // android应用下载地址
                        var app_download = app.value
                    } else if (app.key == "app_wget_version") {
                        // 应用更新包版本
                        var app_wget_version = app.value
                    } else if (app.key == "app_wget_download") {
                        // 应用更新包下载地址
                        var app_wget_download = app.value
                    } else if (app.key == "apple_version") {
                        // ios应用版本
                        var apple_version = app.value
                    }
                }

                // #ifndef H5
                let android_version = app_version.replace(/\./g, '');
                let ios_version = apple_version.replace(/\./g, '');
                //热更新
                var resource_version = 128
                if (app_wget_version > resource_version) {
                    uni.downloadFile({
                        url: app_wget_download,
                        success: (downloadResult) => {
                            if (downloadResult.statusCode === 200) {
                                plus.runtime.install(downloadResult.tempFilePath, {
                                    force: true
                                }, function() {
                                    plus.runtime.restart();
                                }, function(e) {
                                    console.error(e)
                                });
                            }
                        }
                    });
                }

                //应用更新
                plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
                    let now_version = wgtinfo.version.replace(/\./g, '');
                    // android
                    if (uni.getSystemInfoSync().platform === 'android' && android_version > now_version) {
                        uni.showModal({
                            title: '应用有更新',
                            success: (res) => {
                                if (res.confirm) {
                                    plus.runtime.openURL(app_download);
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        })
                    }
                    // ios
                    if (uni.getSystemInfoSync().platform == 'ios' && ios_version > now_version) {
                        uni.showModal({
                            title: '应用有更新',
                            success: (res) => {
                                if (res.confirm) {
                                    let appleId = 1532692938 //app的appleId
                                    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);
                                    });
                                } else if (res.cancel) {
                                    console.log('用户点击取消');
                                }
                            }
                        })
                    }
                })
                // #endif
            })
上一篇 下一篇

猜你喜欢

热点阅读