uniapp檢查版本更新

代碼

//#ifdef APP-PLUS
// 版本升級   this.curVersion 定義到data里面
            checkUpdate(){
                // 獲取manifest.json 的信息 版本信息
                plus.runtime.getProperty( plus.runtime.appid, ( wgtinfo ) => {
                    this.curVersion = wgtinfo.version; //應(yīng)用版本名稱
                                        this.curVersionCode = wgtinfo.versionCode; //應(yīng)用版本號
                    console.log( "this.curVersion:" + this.curVersion );
                } );
                // 更新版本接口  
                var server = "接口"; 
                var that = this;
                 uni.request({
                    url: server,  //請求更新地址
                    data: that.curVersion,
                    success(res) {
                        if (res.statusCode == 200 && that.curVersion < res.data.version) {
                            uni.showModal({
                                title: '版本更新' + res.data.version,
                                content: res.data.content,
                                confirmText: "更新",
                                success: function(e) {
                                    if (e.confirm) {
                                        if (plus.os.name.toLowerCase() == 'ios') {
                                            // 跳轉(zhuǎn)到下載頁面
                                            plus.runtime.openURL(res.data.apk)
                                        } else {
                                            // res.data.downloadUrl返回的安裝包
                                            // url 最新的apk == res.data.apk接口返回的數(shù)據(jù)
                                            const url = res.data.apk
                                            // 新建下載任務(wù)
                                            var dtask = plus.downloader.createDownload(url, {},function(d,status){
                                                if(status == 200){  //下載完成
                                                    plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, 
                                                    function(success) {
                                                        uni.showToast({
                                                            title: '安裝成功',
                                                            mask: false,
                                                            duration: 1500
                                                        });
                                                    }, function(error) {
                                                        uni.showToast({
                                                            title: '安裝失敗',
                                                            mask: false,
                                                            duration: 1500
                                                        });
                                                    })
                                                }else{
                                                    uni.showToast({
                                                        title: '更新失敗',
                                                        mask: false,
                                                        duration: 1500
                                                    });
                                                }
                                            })
                                            // 開始下載
                                            dtask.start()
                                        }
                                    } else {
                                        //取消
                                        uni.showToast({
                                            title: '請求失敗',
                                            mask: false,
                                            duration: 1500
                                        });
                                    }
                                }
                            });
                        } else {
                            uni.showModal({
                                title: '提示',
                                content: '已是最新版本',
                                showCancel: false
                            });
                        }
                    },
                })
            },

補(bǔ)充

應(yīng)用版本名稱和版本號的位置


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容