uniapp熱更新/在線升級(jí)(各個(gè)框架及原生通用)

沒做過熱更新的,可能感覺很難,但是當(dāng)你知道邏輯流程后,就非常簡(jiǎn)單了。

一、下面先給大家介紹一下流程:

1、打一個(gè)wgt包,放在服務(wù)器上,確保能被訪問。

2、前端獲取到當(dāng)前版本號(hào),傳到服務(wù)端

3、服務(wù)端對(duì)比客戶端傳來的版本號(hào)和即將更新的版本號(hào),不一致就返回wgt地址

4、客戶端接收到wgt地址,下載,安裝即可

二、具體實(shí)現(xiàn)

1、打wgt包

修改版本號(hào)


制作wgt包

2、客戶端代碼

? ? 獲取當(dāng)前版本號(hào)

// #ifdef APP-PLUS // 獲取當(dāng)前項(xiàng)目信息

let that=this;

plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {

that.comparisonVersionNo(widgetInfo.version); });

// #endif????

? ? 檢查接口,是否需要更新,獲取wgt包

comparisonVersionNo(version){

let that=this;

console.log(version);

let data = {

versionNo:version

};

this.$uniRequest.get('/app/comparisonVersionNo', {

data: data

})

.then(res => {

// console.log(222222);

if (res.data.retCode == 'SUCCESS') {

let versionNos=res.data.retData.versionNos;

console.log(versionNos);

let filePath=res.data.retData.filePath;

if(versionNos==1){//服務(wù)器返回1更新,0則不更新

uni.showModal({

? ? title: '提示',

? ? content: '發(fā)現(xiàn)新版本,是否升級(jí)',

? ? success: function (res) {

? ? // console.log(widgetInfo.version);

? ? ? ? if (res.confirm) {

? ? ? ? ? ? console.log('用戶點(diǎn)擊確定');

? ? ? ? ? ? that.getProperty(filePath);

? ? ? ? } else if (res.cancel) {

? ? ? ? ? ? console.log('用戶點(diǎn)擊取消');


? ? ? ? }

? ? }

});

}

}

})

.catch(error => {

console.log(error);

});

},

下載并升級(jí)

getProperty(getProperty){

let that=this;

// 在線升級(jí)app/熱更新

that.showwri=true;

? ? const downloadTask=uni.downloadFile({ //下載文件

? ? ? ? ? ? ? ? ? ? url: 'https://wechat.zhishidao.com/'+getProperty,

? ? ? ? ? ? ? ? ? ? success: (downloadResult) => {

? ? ? ? ? ? ? ? ? ? // console.log(downloadResult);

? ? ? ? ? ? ? ? ? ? ? ? if (downloadResult.statusCode === 200) {

? ? ? ? ? ? ? ? ? ? ? ? // 更新升級(jí)

? ? ? ? ? ? ? ? ? ? ? ? ? ? plus.runtime.install(downloadResult.tempFilePath, {?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? force: false

? ? ? ? ? ? ? ? ? ? ? ? ? ? }, function() {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // console.log('install success...');

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? that.showwri=false;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plus.nativeUI.alert("應(yīng)用資源更新完成!",function(){?

? ? ? ? plus.runtime.restart();

? ? });?

? ? ? ? ? ? ? ? ? ? ? ? ? ? }, function(e) {?

? ? ? ? ? ? ? ? ? ? ? ? ? ? that.showwri=false;

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? plus.nativeUI.alert("更新失敗,請(qǐng)稍后再試");

? ? ? ? ? ? ? ? ? ? ? ? ? ? });?

? ? ? ? ? ? ? ? ? ? ? ? }?

? ? ? ? ? ? ? ? ? ? }?

? ? ? ? ? ? ? ? });

? ? downloadTask.onProgressUpdate((res) => {//下載文件的進(jìn)度

? ? that.totalBytesWritten=res.totalBytesWritten;//當(dāng)前下載大小

? ? that.progress=res.progress;//當(dāng)前下載比例

? ? that.totalBytesExpectedToWrite=res.totalBytesExpectedToWrite;//

});

},

3、服務(wù)端實(shí)現(xiàn)

服務(wù)端實(shí)現(xiàn)非常簡(jiǎn)單,大致為:

接收客戶端傳入的版本號(hào),然后與數(shù)據(jù)庫(kù)中的版本號(hào)對(duì)吧,不一致就返回wgt地址及更新信號(hào)。版本號(hào)數(shù)據(jù)庫(kù)手動(dòng)改就好,如果有更加好的方法,歡迎補(bǔ)充

好了,大致就是這樣,以上關(guān)鍵代碼ios及Android通用,根據(jù)自己的需求修改即可,希望對(duì)你有用

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

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