APP版本更新功能

基于Hbuilder、mui 新增APP版本更新功能

参考地址: HBuilder打包成apk之后,如何让版本检测自动更新

//检查版本并更新
function checkversion(){
    mui.ajax(mui.check.url, {//需要从接口中获取最新版本信息,与当前版本比较,确认是否是最新版本
        data: {
            'token': mui.check.token,
            'module': 'template',
            'action': 'checkVersion'
        },
        dataType: 'json',
        type: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        success: function(res) {
            if (res.status != 0) {
                plus.runtime.getProperty(plus.runtime.appid,function(inf){
                    if(inf.version != res.data.version){
                        var _msg = "主人,有更强大的新版本了!";
                        mui.confirm(_msg, '更新提醒', ["立即更新","稍后更新"], function (e) {
                            if (e.index == 0) {
                                if(mui.os.ios){//区分ios和Android平台
                                    document.location.href = res.data.downloadurl;//上新APPStore下载地址
                                    // https://itunes.apple.com/cn/app/san-gu-hui/id131812xxxx?mt=8
                                }else{
                                    plus.nativeUI.toast("正在准备环境,请稍后!");
                                    var dtask = plus.downloader.createDownload(res.data.downloadurl, {}, function (d, status) {
                                        if (status == 200) {
                                            var path = d.filename;//下载apk
                                            plus.runtime.install(path); // 自动安装apk文件
                                        } else {
                                            plus.nativeUI.alert('版本更新失败:' + status);
                                        }
                                    });
                                    dtask.start();
                                }
                            }
                        });
                    }else{
                        //console.log('已是最新版本');
                    }
                });
            }
        }
    });
}
//接口返回数据示例:data.setting:{"version":"1.1.13","downloadurl":"https://itunes.apple.com/cn/app/san-gu-hui/id131812xxxx?mt=8"}

 

posted @ 2018-09-07 13:46  梦忘川  阅读(935)  评论(0编辑  收藏  举报