UNIAPP 检查APP最新版本 并自更新

onShow: function() {
    console.log('App Show')
    plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
    this.version = widgetInfo.version
    uni.request({
        url: 'http://xxxx.cczu.edu.cn:8081/api/dict',
        success: (res) => {
            function compareVersion(version1, version2) {
                const newVersion1 = `${version1}`.split('.').length < 3 ? `${version1}`.concat('.0') : `${version1}`;
                const newVersion2 = `${version2}`.split('.').length < 3 ? `${version2}`.concat('.0') : `${version2}`;
                  //计算版本号大小,转化大小
                 function toNum(a){
                     const c = a.toString().split('.');
                     const num_place = ["", "0", "00", "000", "0000"],
                         r = num_place.reverse();
                     for (let i = 0; i < c.length; i++){
                         const len=c[i].length;
                         c[i]=r[len]+c[i];
                     }
                         return c.join('');
                     }
                     // 检测版本号是否需要更新
                     function checkPlugin(a, b) {
                         const numA = toNum(a);
                         const numB = toNum(b);
                         return numA > numB ? 1 : numA < numB ? -1 : 0;
                     }
                         return checkPlugin(newVersion1 ,newVersion2);
                     }
                     for (let i of res.data.content) {
                         if (i.description === 'app版本') {
                            // 1代表app新包版本号大于本地版本号
                             if (compareVersion(i.dictDetails[0].value, this.version) === 1) {
                                 uni.showModal({
                                     title: '提示',
                                     content: '发现新的应用安装包,点击确定立即更新',
                                     success: function (res) {
                                        if (res.confirm) {
                                                console.log('用户点击确定');
                                            uni.showLoading({
                                                title: '更新中……'
                                            })
                                             uni.downloadFile({
                                                 // 存放最新安装包的地址
                                                 url: 'http://xxxx.xxxx.com/__UNI__xxxx.apk',
                                                 success: (downloadResult) => {  
                                                    uni.hideLoading();
                                                     if (downloadResult.statusCode === 200) { 
                                                        uni.hideLoading();
                                                         plus.runtime.install(downloadResult.tempFilePath,{ 
                                                             force: false 
                                                         }, function() {
                                                             console.log('install success...');  
                                                             plus.runtime.restart();  
                                                         }, function(e) {  
                                                            uni.hideLoading();
                                                             console.error('install fail...');  
                                                         });  
                                                     }
                                                 }  
                                             }); 
                                        } else if (res.cancel) {
                                                console.log('用户点击取消');
                                        }
                                     }
                                 });
                             } else {
                         }
                     }
                 }
            }
        });
    });
},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值