uni-app更新APP,版本更新

export default {
	onLaunch: function() {
		console.log('App Launch');
		// 檢查更新
		//#ifdef APP-PLUS  
		plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
			var version = wgtinfo.version // 客戶端版本號
			let update = {
				url: this.$URL + '/api/xxxx/xxx?version=' + version,
				success: (res) => {
				if (res.data.updated === true) { // 如果服務器版本大於客戶端版本則提醒更新
					var url = res.data.url
					uni.showModal({
					  title: '版本更新',
					  content: '有新的版本發佈,是否立即進行新版本下載',
					  success: (res) => {
					  if (res.confirm) {
					  console.log('用戶點擊確定');
                                          // 調用下載方法
					  this.updateAPP(url)
					  } else if (res.cancel) {
					  console.log('用戶點擊取消');
					      }
					    }
					 });
				      }
				   }
				}
                               // 請求自己的api,與服務器版本進行比對,落後則執行官方的API更新應用
				this.$apiReq.req(update) 
			})
			//#endif  
			
	},
	methods:{
		 updateAPP(url) {
			 console.log('正在更新: url='+ url)
                        // 官方API
			 var dtask = plus.downloader.createDownload(url,{},function(d,status){ 
			 	//d爲下載的文件對象
			 	if(status==200){
			 		//下載成功,d.filename是文件在保存在本地的相對路徑,使用下面的API可轉爲平臺絕對路徑
			 		var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
			 		plus.runtime.openFile(d.filename);	   //選擇軟件打開文件
			     }else{	
			     	//下載失敗
			     	plus.downloader.clear();        //清除下載任務
			     }
			 })
			 dtask.start();
		}
	},
	 onShow: function() {
			console.log('App Show');
		},
      onHide: function() {
			console.log('App Hide');
		}
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章