先列出需要注意的地方,避免新手朋友们出错

  1. 如果uniapp运行app报错:ReferenceError: plus is not defined ,普通浏览器里没有plus环境,只有HBuilder真机运行、打包后、或流应用环境下才能运行plus api。
  2. 注意url,此功能需要后端配合,具体需要什么你们商量去,(放张图,对新手友好)
  3. version建议使用整数,版本号递增
  4. 如果APPID相同,软件会自动覆盖(所以需要让更新的appid和原来的appid相同,否则就是两个程序了)
// 检查版本更新
			let _this = this
             // 获取manifest.json里的配置信息
			plus.runtime.getProperty(plus.runtime.appid, function(appInfo) { 
				_this.globalData.versionCode = appInfo.versionCode
				_this.AndroidCheckUpdate()
			}    
 AndroidCheckUpdate:function(){  
                var _this=this;  
                //这个接口让后端返回新版本的信息
                uni.request({  
                    url: 'http://xxxx/version.txt',  
                    method: 'GET',  
                    data: {},  
                    success: res => {
                         // 如果版本号不相同,就下载新版的应用,有的写法是res.data.data.versionCode > this.globalData.versionCode,还是看自己需求吧
                        if (res.data.data.versionCode != this.globalData.versionCode) {
                            if(plusworkinfo.getCurrentType()!=3){  
                              uni.showModal({
								    content:'有新的版本发布,检测到您目前非Wifi连接,为节约您的流量,程序已停止自动更新,将在您连接WIFI之后重新检测更新。',
									showCancel:false,
								    success: function (res) {
								        if (res.confirm) {
								            console.log('用户点击确定');
								        }
								    }
								});
                                return;  
                            }  
                           uni.showModal({
							    content:'有新的版本发布,检测到您目前为Wifi连接,程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',
								showCancel:false,
							    success: function (res) {
							        if (res.confirm) {
							            console.log('用户点击确定');
							        }
							    }
							});
                            //downloadUrl接口返回的应用下载地址
                            var dtask = plus.downloader.createDownload( res.data.data.downloadUrl, {}, function ( d, status ) {  
                                    // 下载完成  
                                    if ( status == 200 ) {   
                                        plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename),{},{},function(error){  
                                            uni.showToast({  
                                                title: '安装失败',  
                                                mask: false,  
                                                duration: 1500  
                                            });  
                                        })  
                                    } else {  
                                         uni.showToast({  
                                            title: '更新失败',  
                                            mask: false,  
                                            duration: 1500  
                                         });  
                                    }    
                                });  
                            dtask.start();   
                        }  
                    },  
                    fail: () => {},  
                    complete: () => {}  
                });  
            },