使用uniapp开发app软件更新

4 篇文章 0 订阅

在首页或者登录页面判断是否有新版本发布,若有则跳到更新页面下载 

updateApp() {
			uni.getSystemInfo({
				success: res => {
					//检测当前平台,如果是安卓则启动安卓更新
					if (res.platform == 'android') {
						// 判断版本问题
						plus.runtime.getProperty(plus.runtime.appid, info => {
							uni.request({
								url: `http://localhost:8080/app-version/getLatestAppVersion&version=${info.version}`,
								success: res => {
									console.log(res);
									if (res.data.code == 0) {
										const version = res.data.data.newVersion;
										if (version == '1') {
											uni.showModal({
												title: '版本发布更新',
												content: '发现新版本,请进行更新',
												confirmText: '更新',
												confirmColor: '#EE8F57',
												success: res => {
													if (res.confirm) {
														uni.navigateTo({
															url: '/pages/downloadApp/downloadApp'
														});
													} else if (res.cancel) {
														// 在这可以写取消后再次点击更新的逻辑
														uni.showToast({
															icon: 'none',
															title: '你取消了更新'
														});
													}
												}
											});
										}
									}
								},
								fail(err) {
									console.log(err);
								}
							});
						});
					}
				}
			});
		},

下载页面

<template>
	<view class="container">
		<view class="progressBox"><progress :percent="progress" show-info stroke-width="12" /></view>
		<text class="textBox">{{ progress >= 100 ? '下载完成' : '下载中....' }}</text>
	</view>
</template>

<script>
import httpURL from '../../api/httpURL.js';
export default {
	data() {
		return {
			progress: 0
		};
	},
	/* 生命周期函数--监听页面加载 */
	onLoad: function() {
		this.doUpData();
	},
	methods: {
		doUpData: function() {
			let appUrl = `http://localhost:8080/userlimit/app-version/downloadLatestApp`;
			let downloadTask = uni.downloadFile({
				url: appUrl,
				success: downloadResult => {
					console.log(downloadResult);
					// 下载成功
					if (downloadResult.statusCode === 200) {
						uni.showModal({
							title: '',
							content: '下载成功,确定现在更新吗?',
							confirmText: '更新',
							confirmColor: '#EE8F57',
							success: function(res) {
								// console.log(res)
								if (res.confirm) {
									plus.runtime.install(
										//安装
										downloadResult.tempFilePath,
										{
											force: true
										},
										function() {
											console.log('更新成功');
											plus.runtime.restart();
											uni.navigateBack();
										},
										function(e) {
											console.log('更新失败');
										}
									);
								}
							}
						});
					}
				},
				complete: () => {
					// uni.hideLoading();
				}
			});
			downloadTask.onProgressUpdate(res => {
				// console.log('下载进度' + res.progress);
				this.progress = res.progress;
				// console.log('已经下载的数据长度' + res.totalBytesWritten);
				// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				// // 测试条件,取消下载任务。
				// if (res.progress > 50) {
				// 	// downloadTask.abort();
				// }
			});
		}
	}
};
</script>
<style>
.container {
	width: 100%;
}
.progressBox {
	padding: 50rpx 20rpx;
}
.textBox {
	display: flex;
	justify-content: center;
}
</style>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值