大能猫蹲坑逗蛆
大能猫蹲坑逗蛆
  • 发布:2017-07-06 10:44
  • 更新:2017-07-06 10:44
  • 阅读:3681

DCloud APP升级代码的实现

分类:HBuilder
//系统自动更新  
    update: function() {  
        if(window.plus) {  
            this.updateCheck();  
        } else {  
            document.addEventListener('plusready', this.updateCheck, false);  
        }  
    },  
    //系统升级检测  
    updateCheck: function() {  
        var appVer = ''; //app版本  
        var newVer = ''; //最新版本  
        //获得当前版本  
        plus.runtime.getProperty(plus.runtime.appid, function(inf) {  
            appVer = inf.version;  
            $('.copyright').html('湖南互联移动科技有限公司提供技术支持v' + appVer);  
            //发起新版本请求检测  
            sysCom.get(urlCom.apiUrl.update_path + "/version.html?v=" + (new Date().getTime()), {}, function(data) {  
                newVer = data.version;  
                if(appVer != newVer) {  
                    var waiting = plus.nativeUI.showWaiting("正在下载数据更新资源包 0%...", {  
                        width: '100%',  
                        height: '100%',  
                        back: 'none',  
                        round: 1,  
                    });  

                    //模拟下载百分比动画  
                    var _sec = 1;  
                    var _add = 1;  
                    var _filename = ''; //下载好的更新文件  
                    var _timer = setInterval(function() {  
                        _add = Math.floor(Math.random() * 10);  
                        _sec += _add;  
                        if(_sec >= 100) {  
                            _sec = 99;  
                            if(_filename) {  
                                clearInterval(_timer);  
                                waiting.setTitle('正在下载数据资源包 100%...');  

                                // 安装更新包  
                                plus.runtime.install(_filename, {}, function() {  
                                    waiting.setTitle('数据资源包下载完成,正在重启应用...');  
                                    //更新完毕删除更新包  
                                    plus.io.resolveLocalFileSystemURL(_filename, function(entity) {  
                                        entity.remove();  
                                    }, function(error) {  
                                        debugCom.log(error.message);  
                                    });  
                                    //应用重启并显示启动页  
                                    dataCom.remove(dataBase.names.data_guide);  
                                    setTimeout(function() {  
                                        plus.nativeUI.closeWaiting();  
                                        plus.runtime.restart();  
                                    }, 1000)  
                                }, function(e) {  
                                    waiting.setTitle('更新失败:' + e.message);  
                                });  
                            }  

                        }  
                        waiting.setTitle('正在下载数据资源包 ' + _sec + '%...');  
                    }, 500);  
                    //--  

                    //下载更新包  
                    plus.downloader.createDownload(urlCom.apiUrl.update_path + "/" + newVer + ".wgt", {  
                        filename: "_doc/update/"  
                    }, function(d, status) {  
                        if(status == 200) {  
                            _filename = d.filename;  
                        } else {  
                            debugCom.log('下载资源包出现问题:' + status)  
                        }  
                    }).start();  
                    //--  
                }  

            });  
        })  
    }
1 关注 分享
lufei

要回复文章请先登录注册

距离您98米

距离您98米

能不能写真实的下载进度?
2018-01-03 09:33