uni-app 實現熱更新


  前端打包 app 即把寫好的靜態資源文件套殼打包成 app ,而熱更新即下載並替換 app 內部的靜態資源文件,實現 app 的版本升級。

  在uni-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();
                    }
                });
            }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM