6***@qq.com
6***@qq.com
  • 发布:2018-10-15 15:09
  • 更新:2022-02-11 11:22
  • 阅读:28481

uni APP自动更新并安装

分类:uni-app
data:function(){  
            return{  
                version:1  
            }  
        },  
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',  
                    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: () => {}  
                });  
            },  
        }
24 关注 分享
崔先生 l***@163.com DCloud_heavensoft Geeker 沧桑 1***@qq.com 8***@qq.com 喜欢技术的前端 2***@qq.com 罪途 1***@qq.com bugAT HealerLZH foolma 雨夜敬清秋 7***@qq.com 涂1 qiang98 6***@qq.com 刃吹雪 Yacheck 1***@qq.com 1***@qq.com 1***@163.com

要回复文章请先登录注册

一只会思考的猪

一只会思考的猪

不错,谢谢分享。
2019-01-10 17:11
fox2338

fox2338

很不错的demo
2019-01-05 17:36
1***@qq.com

1***@qq.com

请问这个下载进度在哪看呢?是偷偷的下载吗?
2018-12-24 16:31
1***@163.com

1***@163.com

有ios的热更新吗
2018-12-18 15:29
沧桑

沧桑

收了
2018-12-18 15:06