uni-app 版本更新

import request from '@/utils/request'
import config from '@/config'

const versionUpdate ={
	param: {
		version : 0,// 当前版本
		downloadUrl: "",//下载地址
		isForceUpdate: false,// 是否强制更新
		showdownLine: true,
	},
	checkUpdate: function(isShowNoUpdatesTip) {
		uni.getSystemInfo({
		  success:(res) => {  
		    console.log(res.platform)
		    //检测当前平台,如果是安卓则启动安卓更新  
		    if(res.platform=="android"){  
		      // 获取当前app版本信息
		      plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
		      	versionUpdate.param.version = wgtinfo.version //客户端版本号
		      	console.log('当前app版本信息:' + versionUpdate.param.version)
		      })
		      request({
		        url: '/api/app/newest',
		        method: 'get'
		      }).then(res => {
		      	console.log("现在的版本" + versionUpdate.param.version + ";数据库版本" + res.version.fileVersion + "进入查找app版本")
		      	if (res.version.fileVersion > versionUpdate.param.version) {
		      		// 这里下载apkurl从/appUpdate/queryUpdate接口请求返回数据中获取
		      		versionUpdate.param.downloadUrl = config.baseUrl+ res.version.fileUrl
		      		// 是否强制更新
		      		versionUpdate.param.isForceUpdate = res.version.forceUpdate == "Y"?true:false
		      		let showMsg = '当前版本号:' + versionUpdate.param.version + '\r\n待更新版本号:' + res.version.fileVersion
		      		if (versionUpdate.param.isForceUpdate) {
		      			 showMsg = showMsg+'\r\n(此版本为强制更新版本如不升级将退出)'
		      		}
		      		uni.showModal({
		      			// 更新提醒
		      			title: '发现新版本,是否更新?',
		      			content: showMsg,
		      			success: res => {
		      				if (res.confirm) {
		      					versionUpdate.downWgt() //下载文件
		      					// versionUpdate.param.showdownLine = true
		      					// plus.runtime.openURL(androidUrl)
		      				} 
		      				if (res.cancel) {
		      					console.log('versionUpdate.param.isForceUpdate:' + versionUpdate.param.isForceUpdate)
		      					if (versionUpdate.param.isForceUpdate) {
		      						plus.runtime.quit()
		      					}
		      				}
		      			}
		      		})
		      		//dtask.start()
		      	}else{
					if(isShowNoUpdatesTip){
						uni.showModal({
							// 更新提醒
							title: '当前版本已是最新版本',
							showCancel: false,
							content: '当前版本号:' + versionUpdate.param.version,
							success: res => {
								
							}
						})
					}
				}
		      })
		    }  
		  }  
		})
		
		
	},
	downWgt: function() {
		console.log('url:' + versionUpdate.param.downloadUrl)
		uni.showLoading({
			title: '更新中……'
		})
	
		const downloadTask = uni.downloadFile({ //执行下载
			url: versionUpdate.param.downloadUrl, //下载地址
			timeout: 1000 * 30, //30秒超时时间
			success: downloadResult => { //下载成功
				versionUpdate.param.showdownLine = false
				uni.hideLoading()
				console.log('downloadResult.statusCode' + downloadResult.statusCode)
				if (downloadResult.statusCode == 200) {
					console.log('更新中')
					uni.showModal({
						title: '',
						content: '更新成功,确定现在安装吗?',
						confirmText: '安装',
						confirmColor: '#EE8F57',
						success: function(res) {
							if (res.confirm == true) {
								plus.runtime.install( //安装
									downloadResult.tempFilePath, {
										force: true
									},
									function(res) {
										plus.runtime.restart()
									}
								)
							}
						}
					})
				}else{
					uni.showToast({
						title:"更新失败",
						icon:"error"
					})
				}
			},
			fail: err => {
				uni.hideLoading()
				versionUpdate.param.showdownLine = false
				versionUpdate.param.$u.toast(err.errMsg)
				console.log(err)
			},
			complete: com => {
				console.log(com)
			}
		})
	
		// 下载进度
		downloadTask.onProgressUpdate(res => {
			// versionUpdate.param.$u.toast(res.progress)
			versionUpdate.param.downloadNum = res.progress
			console.log('下载进度' + versionUpdate.param.downloadNum)
			// console.log('已经下载的数据长度' + res.totalBytesWritten)
			// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite)
			// 满足测试条件,取消下载任务。
			// if (res.progress > 50) {
			// 	downloadTask.abort()
			// }
		})
	},
}

export default versionUpdate

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值