uni-app实现APP自动更新并安装

data:function(){  
            return{  
                version:102  
            }  
        },  
onLoad: function () {     
            uni.getSystemInfo({  
                success:(res) => {  
                    console.log(res.platform);  
                    //检测当前平台,如果是安卓则启动安卓更新  
                    if(res.platform=="android"){  
                        this.AndroidCheckUpdate();  
                    }  
                }  
            })  
        },  
        methods: {  
            AndroidCheckUpdate:function(){  
                var _this=this;  
                uni.request({  
                    url: 'http://xxxx.com/version.txt',  //103
                    method: 'GET',  
                    data: {},  
                    success: res => {  
                        if(res.data>this.version){  
                            if(plus.networkinfo.getCurrentType()!=3){  
                                uni.showToast({  
                                    title: '有新的版本发布,检测到您目前非Wifi连接,为节约您的流量,程序已停止自动更新,将在您连接WIFI之后重新检测更新。',  
                                    mask: false,  
                                    duration: 5000,  
                                    icon:"none"  
                                });  
                                return;  
                            }  
                            uni.showToast({  
                                title: '有新的版本发布,检测到您目前为Wifi连接,程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',  
                                mask: false,  
                                duration: 5000,  
                                icon:"none"  
                            });  
                            var dtask = plus.downloader.createDownload( "http://xxxx.com/app.apk", {}, function ( d, status ) {  
                                    // 下载完成  
                                    if ( status == 200 ) {   
                                        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  
                                         });  
                                    }    
                                });  
                            dtask.start();   
                        }  
                    },  
                    fail: () => {},  
                    complete: () => {}  
                });  
            },  
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值