uniapp 更新APP实现

在我们进行uniapp开发的时候,一定会遇到版本更新的问题,而这个问题其实也很容易解决,请看如下代码,代码中有注释。(如果复制过去遇到问题,很有可能是括号匹配问题,请仔细看看)

<template>
    <view class="page">
        <view class="card">
            <view>
                <u-cell-group>
                   
<u-cell @click="updateVersion" icon="integral-fill" title="当前版本" :value="show+'  '+version"></u-cell>
                 
                </u-cell-group>
            </view>
        </view>
     
    </view>
</template>

<script>
    export default {
        data() {
            return {
                show:'',
                version: ''
            }
        },
        onLoad() {
            //获取当前的版本号   仅在使用手机APP调试的时候才能调用
            //#ifdef APP-PLUS
            plus.runtime.getProperty(plus.runtime.appid, (info) => {
                this.version ="v"+ info.version
            })
            //#endif
            
        },
        created() {
            uni.request({
                //服务器上版本号请求的网页连接地址
                url: 'xxx',
                method: 'GET',
                data: {},
                success: res => {
                    //你的获取需要更改
                    if (res.data.data.version == this.version) {
                          this.show = "最新版本"
                        }else{
                          this.show = "点击更新"
                        }
                    },
                            
            });
        },
        methods: {
            // 更新操作
updateVersion(){
    var _this = this;
        uni.request({
        //此处同上
        url: 'xxx',
        method: 'GET',
        data: {},
        success: res => {
            if (res.data.data.version > this.version) {
                uni.showToast({
                    title: '更新......',
                    mask: false,
                    duration: 5000,
                    icon: "none"
                });
                var dtask = plus.downloader.createDownload(
                // 我们能访问的外网apk安装包,此链接是你们能下载的apk路径
                "xxx.apk", {},
                function(d, status) {
                    //下载完成  
                    if (status == 200) {
                        // 调用app的安装方法
                        plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(error) {
                            uni.showToast({
                                title: '安装失败',
                                mask: false,
                                duration: 1500
                                });
                            })
                        } else {
                            uni.showToast({
                                title: '更新失败',
                                mask: false,
                                duration: 1500
                                        });
                                    }
                                });
                            try {
                                dtask.start(); // 开启下载的任务
                                var prg = 0;
                                var showLoading = plus.nativeUI.showWaiting("正在下载"); //创建一个showWaiting对象 
                                dtask.addEventListener('statechanged', function(task,status) {
                                        // 给下载任务设置一个监听 并根据状态  做操作
                                switch (task.state) {
                                    case 1:
                                        showLoading.setTitle("正在下载");
                                        break;
                                    case 2:
                                        showLoading.setTitle("已连接到服务器");
                                        break;
                                    case 3:
                                        prg = parseInt(
                                            (parseFloat(task.downloadedSize) /
                                                parseFloat(task.totalSize)) *
                                            100
                                        );
                                        showLoading.setTitle("  正在下载" + prg + "%  ");
                                        break;
                                    case 4:
                                        plus.nativeUI.closeWaiting();
                                        //下载完成
                                        break;
                                }
                                    });
                                } catch (err) {
                                    plus.nativeUI.closeWaiting();
                                    uni.showToast({
                                        title: '更新失败-03',
                                        mask: false,
                                        duration: 1500
                                    });
                                }
                            }else{
                                uni.showToast({
                                    title: '已是最新版本',
                                })
                            }
                        },
                    });
                }, 
         }
    }
</script>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值