uniapp: version update (package wgt)

1. Automatically detect updates:

   Add the update() method to the App.vue page, and then call it. After entering the app, the update package will be automatically detected later

onLaunch: function() {
	console.log('App Launch')
	this.update()
},

  

2. Write method click manual update

 

​
// 系统升级
update(){
	const that = this
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { 
    console.log(widgetInfo)    
	uni.request({  
        //更新接口
		url: 'http://localhost:8000/prod-api/api/app/verifyVersion', 
		data: {  
            // 版本号
			// version: widgetInfo.version,  
			// versionCode: widgetInfo.versionCode  
		},  
		header:{
            // 请求格式
			//'Content-Type':'application/x-www-form-urlencoded'  
		}, 
		method:'GET',
		success: function(result){ 
			var data = result.data;  
			console.log(data,widgetInfo.versionCode,data.data.versionNum)
			// 判断版本号
			if (data.code == '200' && Number(widgetInfo.versionCode<data.data.versionNum) {  
				//console.log('进入方法',data.data.appPath)
				uni.downloadFile({   
					url: that.risun.utils.dealImg(data.data.appPath),  
					success: function(downloadResult){  
						console.log('downloadResult',downloadResult)
						if (downloadResult.statusCode === 200) {  
							 uni.showModal({
								 title: '有新版本更新',
								 content: '是否下载更新',
								 success: function (res) { 
									if (res.confirm) {
										//console.log('用户点击确定');
														 
                                       plus.runtime.install(downloadResult.tempFilePath, {
											force: true  
										}, function() {  
											uni.showToast({
												title:"安装成功",
												icon:2000,
												duration:1000
											})
											setTimeout(function(){
												plus.runtime.restart(); 
											},1100)
										}, function(e) {// }) 
											console.log(e)
											uni.showToast({
												title:"安装失败",
												icon:2000,
												duration:1000
											}) 
										}); 
									} else if (res.cancel) {
										console.log('用户点击取消');
										return;
									}
								}
							 })
											
						}  
					},
					fail:function(res){
						 console.log('失败')
					},
					error:function(res){
						 console.log(res)
					}
				});  
			 } 
		  }  
	   });  
    });
},

​

Guess you like

Origin blog.csdn.net/Ygaidi/article/details/128202243