祈求
祈求
  • 发布:2020-08-01 11:47
  • 更新:2020-09-26 01:38
  • 阅读:2012

热更新

分类:uni-app
//APP更新  
import httpUtil from "./HttpUtils.js"  
import Urls from "./Urls.js"  
var update = function(callback) {  
    uni.showToast({  
        title: '开始检查更新',  
        mask: false,  
        duration: 5000,  
        icon: "none"  
    });  

    uni.getSystemInfo({  
        success: (res) => {  
            console.log(res)  
            CheckUpdate(res.platform, callback);  
        }  
    })  

    function autoUpdateRes(url) {  
        //#ifdef APP-PLUS  
        const dtask = plus.downloader.createDownload(url, {  

            },  
            function(d, status) {  
                // 下载完成    
                if (status == 200) {  
                    // var files = plus.io.convertLocalFileSystemURL(d.filename);  

                    //下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径  
                    //var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);  
                    // plus.runtime.openFile(d.filename); //选择软件打开文件  

                    plus.nativeUI.showWaiting("安装wgt文件...");  
                    plus.runtime.install(d.filename, {}, function() {  
                        plus.nativeUI.closeWaiting();  
                        console.log("安装wgt文件成功!");  
                        plus.nativeUI.alert("应用资源更新完成!", function() {  
                            plus.runtime.restart();  
                        });  
                    }, function(e) {  
                        plus.nativeUI.closeWaiting();  
                        console.log("安装wgt文件失败[" + e.code + "]:" + e.message);  
                        plus.nativeUI.alert("安装wgt文件失败[" + e.code + "]:" + e.message);  
                    });  
                } else {  
                    uni.showToast({  
                        title: '更新失败' + status,  
                        mask: false,  
                        duration: 1500  
                    });  
                    //下载失败  
                    plus.downloader.clear(); //清除下载任务  
                }  
            });  
        dtask.start();  

        //#endif  
    };  

    // 自动更新  
    function CheckUpdate(platform, callback) {  

        httpUtil.httpUtil(Urls.URLDATA.UPDATE_VERSION, {  
            'platform': platform  
        }, function(res) {  
            console.log(res)  
            if (res.data.data && (res.data.data.number > plus.runtime.version)) {  
                /* if (plus.networkinfo.getCurrentType() != 3) {  
                    uni.showToast({  
                        title: '有新的版本发布,检测到您目前非Wifi连接,为节约您的流量,程序已停止自动更新,将在您连接WIFI之后重新检测更新。',  
                        mask: false,  
                        duration: 5000,  
                        icon: "none"  
                    });  
                    return;  
                } */  

                if (res.data.data.type < 3) {  

                    uni.showModal({  
                        title: '新版本提示',  
                        content: res.data.data.desc,  
                        success: function(re) {  
                            if (re.confirm) {  
                                if (res.data.data.type == 1) { // 热更新   
                                    autoUpdateRes(res.data.data.path);  
                                } else {  
                                    downloadFile(res.data.data.path, callback);  
                                }  

                            } else if (re.cancel) {  
                                uni.showToast({  
                                    title: '已取消更新',  
                                    mask: false,  
                                    duration: 5000,  
                                    icon: "none"  
                                });  
                            }  
                        }  
                    });  
                } else {  
                    // 强制更新  

                    if (res.data.data.type == 3) { // 热更新  
                        autoUpdateRes(res.data.data.path);  
                    } else {  
                        downloadFile(res.data.data.path, callback);  
                    }  
                }  

            } else {  
                uni.showToast({  
                    title: '当前已是最新版',  
                    mask: false,  
                    duration: 5000,  
                    icon: "none"  
                });  
            }  
        })  
    }  

    //整包更新  

    function packageDownload(downloadUrl) {  

        plus.runtime.openURL(downloadUrl) //整包下载  

    }  

    function downloadFile(url, callback) {  
        //#ifdef APP-PLUS  
        const dtask = plus.downloader.createDownload(url, {},  
            function(d, status) {  
                // 下载完成    
                if (status == 200) {  
                    // plus.runtime.openFile(d.filename); //选择软件打开文件  
                    plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, function(e) {  
                        uni.showToast({  
                            title: '更新成功,需要重启' + JSON.stringify(e),  
                            mask: false,  
                            duration: 1000  
                        });  
                        uni.removeSavedFile({  
                            filePath: plus.io.convertLocalFileSystemURL(d.filename),  
                            complete: function(res) {  
                                console.log(res);  
                            }  
                        });  
                        plus.runtime.restart();  

                    }, function(error) {  
                        uni.showToast({  
                            title: '安装失败' + error.message,  
                            mask: false,  
                            duration: 1000  
                        });  
                        uni.removeSavedFile({  
                            filePath: plus.io.convertLocalFileSystemURL(d.filename),  
                            complete: function(res) {  
                                console.log(res);  
                            }  
                        });  
                    })  

                } else {  
                    uni.showToast({  
                        title: '更新失败' + status,  
                        mask: false,  
                        duration: 1000  
                    });  
                    //下载失败  
                    plus.downloader.clear(); //清除下载任务  
                }  
            });  
        dtask.addEventListener('statechanged', (task) => {  
            if (callback) {  
                callback(task);  
            }  

        });  
        dtask.start();  

        //#endif  

    }  

}  
module.exports = {  
    update  
}  
1 关注 分享
w***@hnu.edu.cn

要回复文章请先登录注册

w***@hnu.edu.cn

w***@hnu.edu.cn

思路很清晰了
2020-09-26 01:38