uniapp實(shí)現(xiàn)app熱更新的方法
啊~時隔多月終于閑下來了。最近整理了下資料發(fā)現(xiàn)熱更新在app開發(fā)是經(jīng)常見的,基本必備而且確實(shí)很方便,所以就總結(jié)了點(diǎn)東西給大家看看,有問題可以一起討論
一、實(shí)現(xiàn)熱更新需要那些東西
需要服務(wù)器存放更新包資源,后端提供接口用于檢測當(dāng)前版本是否為最新版本。(增刪改查) 熱更新的流程其實(shí)很簡單,如下圖所示
二、具體流程代碼
1.獲取當(dāng)前應(yīng)用app版本
// 保存 app 版本信息 // #ifdef APP-PLUS plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> { // console.log('widgetInfo', widgetInfo); this.version = widgetInfo.version; }); // #endif
2.獲取服務(wù)器上更新包的資源(包含下載鏈接,更新包版本),比較當(dāng)前版本是否為最新版本,不是則彈出提示更新最新版本
checkWgtFun() { //loginApi.getPatchManage() 獲取更新包接口 loginApi.getPatchManage().then(res=> { console.log('檢查更新包', res); if(res.code == 200) { let result = res.data.versionNum // 更新包版本 if(this.version.substr(0, 3) * 1 >= result.substr(0, 3) * 1){ this.$toast('當(dāng)前為最新版本'); return } if(this.version.replace(/\./g, "") * 1 >= result.replace(/\./g, "") * 1){ this.$toast('當(dāng)前為最新版本'); return } uni.showModal({ title: '提示', content: '發(fā)現(xiàn)有新版本可以升級', cancelText: '取消更新', confirmText: '立即更新', success: res1 => { if (res1.confirm) { console.log('用戶點(diǎn)擊確定'); // 補(bǔ)丁下載安裝 // this.versionNum=res.data.versionNum this.downWgt(res.data.patchUrl) } else if (res1.cancel) { console.log('用戶點(diǎn)擊取消'); } }, fail: (err) => { console.log('下載失敗', err); } }); } else { this.$toast(res.msg); } }) },
3.用戶選擇更新版本下載更新包
// 下載補(bǔ)丁 // patchUrl 更新包下載路徑 downWgt(patchUrl) { let _this=this this.downloadTask = uni.downloadFile({ url:patchUrl, success: (downloadResult) => { if (downloadResult.statusCode === 200) { // 安裝應(yīng)用 plus.runtime.install( downloadResult.tempFilePath, {force: false}, ()=> { plus.nativeUI.toast('最新版本下載完成') // 安裝成功之后關(guān)閉應(yīng)用重啟app plus.runtime.restart(); }, (e)=> { plus.nativeUI.toast("補(bǔ)丁安裝失敗")// 常見問題:版本號,appId }); } }, fail: (err) => { plus.nativeUI.toast("補(bǔ)丁下載失敗") } }) }, // 用戶取消更新,中斷下載 cancel() { if(this.downloadTask) { this.$toast('取消下載安裝!') this.downloadTask.abort() this.downloadTask = null } },
到此就完成了熱更新的功能,看著不難吧,最后貼出我寫的完整的代碼,最好封裝成一個組件
<template> <view> <view @click="checkApp" v-if="verShow"> <u-cell-item title="檢查新版本" :value='version' :arrow='false'></u-cell-item> </view> <u-mask :show="show"> <view class="warp"> <view class="version"> <view class="new-version">發(fā)現(xiàn)新版本</view> <view style="color: #fff;">v {{versionNum}}</view> <view class="be-updating">正在更新</view> <u-line-progress :percent='schedule.progress' :show-percent='false' active-color='#4B86FE' striped striped-active> </u-line-progress> <view class="down-prog">{{schedule.totalBytesWritten}}/{{schedule.totalBytesExpectedToWrite}} </view> <view class="cancel" @click="cancel">取消升級</view> </view> </view> </u-mask> </view> </template> <script> import {mineApi,loginApi} from '@/api/myAjax.js' import filters from '@/common/filters.js' export default { props:{ verShow:{ type:Boolean, default:true }, }, data() { return { versionNum:'', schedule:{}, show: false, downloadTask:null, time:10, isCheck:false // versionText:'' }; }, computed:{ version() { // 獲取版本號(在其他地方需要用到所以存了全局變量) return getApp().globalData.version } }, methods:{ // 檢查補(bǔ)丁更新 checkWgtFun() { //loginApi.getPatchManage 為更新包的請求接口方法 loginApi.getPatchManage().then(res=> { console.log('檢查補(bǔ)丁更新包', res); console.log('<this.globalData.version>', uni.getStorageSync('appVersion')); if(res.code == 200) { let result = res.data.versionNum if(this.version.substr(0, 3) * 1 > result.substr(0, 3) * 1){ if(this.verShow){ this.$toast('當(dāng)前為最新版本'); } return } if(this.version.replace(/\./g, "") * 1 >= result.replace(/\./g, "") * 1){ if(this.verShow){ this.$toast('當(dāng)前為最新版本'); } return } uni.showModal({ title: '提示', content: '發(fā)現(xiàn)有新版本可以升級', cancelText: '取消更新', confirmText: '立即更新', success: res1 => { if (res1.confirm) { console.log('用戶點(diǎn)擊確定'); console.log(res); // 補(bǔ)丁下載安裝 this.versionNum=res.data.versionNum this.downWgt(res.data.patchUrl) } else if (res1.cancel) { console.log('用戶點(diǎn)擊取消'); } }, fail: (err) => { console.log('下載失敗', err); } }); } else { this.isCheck = false } }) }, // 下載補(bǔ)丁 downWgt(patchUrl) { let _this=this console.log(patchUrl); this.isCheck = false this.show = true this.downloadTask = uni.downloadFile({ url:patchUrl, success: (downloadResult) => { if (downloadResult.statusCode === 200) { // 安裝應(yīng)用 plus.runtime.install(downloadResult.tempFilePath, {force: false}, ()=> { _this.show =false plus.nativeUI.toast('最新版本下載完成') // 安裝成功之后重啟 plus.runtime.restart(); }, (e)=> { _this.show =false plus.nativeUI.toast("補(bǔ)丁下載失敗") }); } }, fail: (err) => { _this.show =false plus.nativeUI.toast("補(bǔ)丁下載失敗") } }) this.downloadTask.onProgressUpdate((res) => { // 當(dāng)前下載進(jìn)度 if(this.time%10==0){ this.schedule=res this.schedule.totalBytesExpectedToWrite=filters.sizeMB(res.totalBytesExpectedToWrite) this.schedule.totalBytesWritten=filters.sizeMB(res.totalBytesWritten) } this.time+=1 }); }, // 關(guān)閉蒙層 中斷下載 cancel() { if(this.downloadTask) { this.$toast('取消下載安裝!') this.downloadTask.abort() this.downloadTask = null this.show=false this.schedule={} } }, } } </script> <style lang="scss" scoped> .version{ width: 521rpx; height: 583rpx; font-size: 24rpx; padding: 207rpx 44rpx 33rpx; background: url(/static/mine/gxt.png) no-repeat; background-size: 100% 100%; .new-version{ font-size: 30rpx; color: #fff; margin-bottom: 7rpx; height: 45rpx; line-height: 45rpx; } .be-updating{ margin-top: 96rpx; margin-bottom: 14rpx; } .down-prog{ margin: 14rpx 0; } .cancel{ text-align: right; color: #2CA6F8; } } </style>
filters.js文件
function sizeMB(size){ if(size<1024){ return size+'B'; }else if(size/1024>=1 && size/1024/1024<1){ return Math.floor(size/1024*100)/100+'KB'; }else if(size/1024/1024>=1){ return Math.floor(size/1024/1024*100)/100+'MB'; } } export default { sizeMB }
最后在附上如何打包更新包,這個也簡單,跟打包安裝包一樣
選擇制作wgt包,然后點(diǎn)擊生成就可以了,然后就等待打包,更新包一般比安裝包快且沒有次數(shù)限制
還有值得注意的是記得每次打包記得更改版本號跟版本名稱這兩個地方哦
到此這篇關(guān)于uniapp實(shí)現(xiàn)app熱更新的方法的文章就介紹到這了,更多相關(guān)uniapp app熱更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實(shí)現(xiàn)網(wǎng)頁導(dǎo)航條特效
這篇文章主要為大家詳細(xì)介紹了JS實(shí)現(xiàn)網(wǎng)頁導(dǎo)航條特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10easyui combobox開啟搜索自動完成功能的實(shí)例代碼
下面小編就為大家?guī)硪黄猠asyui combobox開啟搜索自動完成功能的實(shí)例代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11微信小程序 JS動態(tài)修改樣式的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于微信小程序JS動態(tài)修改樣式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12javascript實(shí)現(xiàn)PC網(wǎng)頁里的拖拽效果
這篇文章主要介紹了javascript實(shí)現(xiàn)PC網(wǎng)頁里的拖拽效果的相關(guān)資料,需要的朋友可以參考下2016-03-03js實(shí)現(xiàn)鼠標(biāo)劃過給div加透明度的方法
這篇文章主要介紹了js實(shí)現(xiàn)鼠標(biāo)劃過給div加透明度的方法,涉及javascript動態(tài)操作頁面元素屬性的相關(guān)技巧,該方法可兼容火狐與IE瀏覽器,需要的朋友可以參考下2015-05-05JavaScript Window 打開新窗口(window.location.href、windo
本文主要介紹了JavaScript Window 打開新窗口的三種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05