亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

uniapp實現(xiàn)app熱更新的方法

 更新時間:2023年01月03日 10:06:52   作者:清慕  
本文主要介紹了uniapp實現(xiàn)app熱更新的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

啊~時隔多月終于閑下來了。最近整理了下資料發(fā)現(xiàn)熱更新在app開發(fā)是經(jīng)常見的,基本必備而且確實很方便,所以就總結了點東西給大家看看,有問題可以一起討論

一、實現(xiàn)熱更新需要那些東西

需要服務器存放更新包資源,后端提供接口用于檢測當前版本是否為最新版本。(增刪改查) 熱更新的流程其實很簡單,如下圖所示

二、具體流程代碼

1.獲取當前應用app版本

// 保存 app 版本信息
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> {
    // console.log('widgetInfo', widgetInfo);
    this.version = widgetInfo.version;
});
// #endif

2.獲取服務器上更新包的資源(包含下載鏈接,更新包版本),比較當前版本是否為最新版本,不是則彈出提示更新最新版本

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('當前為最新版本');
                return
            } 
            if(this.version.replace(/\./g, "") * 1 >= result.replace(/\./g, "") * 1){
                this.$toast('當前為最新版本');
                return
            }
            uni.showModal({
                title: '提示',
                content: '發(fā)現(xiàn)有新版本可以升級',
                cancelText: '取消更新',
                confirmText: '立即更新',
                success: res1 => {
                    if (res1.confirm) {
                        console.log('用戶點擊確定');
                        // 補丁下載安裝
                        // this.versionNum=res.data.versionNum
                        this.downWgt(res.data.patchUrl)
                    } else if (res1.cancel) {
                        console.log('用戶點擊取消');
                    }
                },
                fail: (err) => {
                    console.log('下載失敗', err);
                }
            });
        } else {
            this.$toast(res.msg);
        }
    })
},

3.用戶選擇更新版本下載更新包

// 下載補丁
// patchUrl 更新包下載路徑
downWgt(patchUrl) {
    let _this=this
    this.downloadTask = uni.downloadFile({
        url:patchUrl,
        success: (downloadResult) => {
            if (downloadResult.statusCode === 200) {  
                // 安裝應用
                plus.runtime.install(
                    downloadResult.tempFilePath, 
                    {force: false}, 
                ()=> {  
                    plus.nativeUI.toast('最新版本下載完成')
                    // 安裝成功之后關閉應用重啟app
                    plus.runtime.restart();  
                }, (e)=> {  
                    plus.nativeUI.toast("補丁安裝失敗")// 常見問題:版本號,appId
                });  
            }
        },
        fail: (err) => {
            plus.nativeUI.toast("補丁下載失敗")
        }
    })
},
// 用戶取消更新,中斷下載
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:{
        // 檢查補丁更新
        checkWgtFun() {
            //loginApi.getPatchManage 為更新包的請求接口方法
            loginApi.getPatchManage().then(res=> {
                console.log('檢查補丁更新包', 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('當前為最新版本');
                        }
                        return
                    } 
                    if(this.version.replace(/\./g, "") * 1 >= result.replace(/\./g, "") * 1){
                        if(this.verShow){
                            this.$toast('當前為最新版本');
                        }
                        return
                    }
                    uni.showModal({
                        title: '提示',
                        content: '發(fā)現(xiàn)有新版本可以升級',
                        cancelText: '取消更新',
                        confirmText: '立即更新',
                        success: res1 => {
                            if (res1.confirm) {
                                console.log('用戶點擊確定');
                                console.log(res);
                                // 補丁下載安裝
                                this.versionNum=res.data.versionNum
                                this.downWgt(res.data.patchUrl)
                            } else if (res1.cancel) {
                                console.log('用戶點擊取消');
                            }
                        },
                        fail: (err) => {
                            console.log('下載失敗', err);
                        }
                    });
                } else {
                    this.isCheck = false
                }
            })
        },
        // 下載補丁
        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) {  
                        // 安裝應用
                        plus.runtime.install(downloadResult.tempFilePath, {force: false}, ()=> {
                            _this.show =false
                            plus.nativeUI.toast('最新版本下載完成')
                            // 安裝成功之后重啟
                            plus.runtime.restart();  
                        }, (e)=> {  
                            _this.show =false
                            plus.nativeUI.toast("補丁下載失敗")
                        });  
                    }
                },
                fail: (err) => {
                    _this.show =false
                    plus.nativeUI.toast("補丁下載失敗")
                }
            })
            this.downloadTask.onProgressUpdate((res) => {
                // 當前下載進度
                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
            });
        },
        // 關閉蒙層 中斷下載
        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包,然后點擊生成就可以了,然后就等待打包,更新包一般比安裝包快且沒有次數(shù)限制

還有值得注意的是記得每次打包記得更改版本號跟版本名稱這兩個地方哦

到此這篇關于uniapp實現(xiàn)app熱更新的方法的文章就介紹到這了,更多相關uniapp app熱更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論