uniapp检查更新并安装

2023-02-07  本文已影响0人  扶得一人醉如苏沐晨

思路

1、获取当前app的版本号
2、调用接口获取新版app的版本号
3、如果新版本号大于旧版本号(需要更新,否则不需要更新)
4、根据新版本的url下载apk
5、下载成功执行install方法安装

checkVeision() {
            // 获取manifest.json 的信息 版本信息
            plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
                this.curVersion = wgtinfo.version; //应用版本名称
                this.curVersionCode = wgtinfo.versionCode; //应用版本号
                console.log('this.curVersion:', wgtinfo);
            });
            // getObj获取新版本信息
            getObj('1').then(res => {
                console.log(res.data.data);
                // 当前版本code
                let curVersionCode = parseInt(this.curVersionCode);
                // 新版本code
                let newVersionCode = parseInt(res.data.data.versionCode);
                // 有新版本
                if (newVersionCode > curVersionCode) {
                    // 检测新版本提示
                    uni.showModal({
                        title: '版本检查',
                        content: '检测到新版本,是否下载安装包?',
                        confirmText: '下载',
                        success: res => {
                            // 安装包下载地址
                            const url = 'https://hikpublic.oss-cn-shanghai.aliyuncs.com/eamsoft/EAM.apk';
                            uni.showLoading({
                                title: '安装包下载中'
                            });
                            // 新建下载任务
                            let dtask = plus.downloader.createDownload(url, {}, (d, status) => {
                                if (status == 200) {
                                    uni.hideLoading();
                                    uni.showModal({
                                        title: '提示',
                                        content: '安装包下载成功,是否确认安装?',
                                        success: res => {
                                            if (res.confirm) {
                                                plus.runtime.install(
                                                    plus.io.convertLocalFileSystemURL(d.filename),
                                                    {},
                                                    success => {
                                                        uni.showToast({
                                                            title: '安装成功',
                                                            mask: false,
                                                            duration: 1500
                                                        });
                                                    },
                                                    error => {
                                                        uni.showToast({
                                                            title: '安装失败',
                                                            mask: false,
                                                            duration: 1500
                                                        });
                                                    }
                                                );
                                            }
                                        }
                                    });
                                } else {
                                    uni.hideLoading();
                                    uni.showToast({
                                        title: '安装包下载失败',
                                        mask: false,
                                        duration: 1500
                                    });
                                }
                            });
                            // 开始下载
                            dtask.start();
                        }
                    });
                } else {
                    // 已经是最新版本
                    uni.showModal({
                        title: '版本检查',
                        content: '当前已经是最新版本',
                        showCancel: false,
                        confirmText: '我知道了',
                        success: function(res) {}
                    });
                }
            });
        },
上一篇 下一篇

猜你喜欢

热点阅读