uni-app 打包App ,实现App更新提醒,并下载最新App

安卓,可以实现App下载,ios只能做到跳转store

代码 

let params = {version:1.80,iosVersion:1.80}

在首页调用判断是否最新版本,当然也可以加入当前网络是否是Wife

getIsNews(params)

getIsNews: function(params) {
    uni.getSystemInfo({
        success: (res) => {
            //检测当前平台,如果是安卓则启动安卓更新  
            if (res.platform == "android") {
                // 判断版本问题
                plus.runtime.getProperty(plus.runtime.appid, (info) => {
                    // console.log(version > parseFloat(info.version))
                    if (params.version > parseFloat(info.version)) {
                        // this.doUpData()
                        uni.showModal({
                            title: '版本发布更新',
                            content: '发现新版本,请进行更新',
                            confirmText: '更新',
                            confirmColor: '#EE8F57',
                            success: function(res) {
                                if (res.confirm) {
                                    uni.navigateTo({
                                        url: "/pages/appDown/appDown",
                                    });
                                }
                            }
                        });
                    }
                });
            } else {
                plus.runtime.getProperty(plus.runtime.appid, (info) => {
                    // console.log(version > parseFloat(info.version))
                    if (params.iosVersion > parseFloat(info.version)) {
                        uni.showModal({
                            title: '版本发布更新',
                            content: '发现新版本,请进行更新',
                            confirmText: '更新',
                            confirmColor: '#EE8F57',
                            success: function(res) {
                                if (res.confirm) {
                                    let appleId = 1540**0527  // 替换成自己AppleId
                                    plus.runtime.launchApplication({
                                        action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
                                    }, function(e) {
                                        console.log('Open system default browser failed: ' + e.message);
                                    });
                                }
                            }
                        });
                    }
                });
            }
        }
    })
}

如果不是最新版本跳转至下载页面

下载显示进度条

<template>
	<view class="container">
		<view class="flex-col-ct">
			<view class="logo-wrap flex-col-ct">
				<view class="logo">
					<image class="wt100" src="https://image-1259258340.cos.ap-chengdu.myqcloud.com/newBeeWxApp/wxAppShopImg/newbee.jpg"
					 mode="widthFix"></image>
				</view>
				<text class="title">NewBee购</text>
			</view>
			<view class="progress-wrap flex-col-ct">
				<view class="progress-box">
					<!--  show-info  -->
					<progress :percent="progress" border-radius="30" stroke-width="12" />
				</view>
				<text class="text">
					{{progress>=100?'下载完成':'下载中....'}}
				</text>
			</view>
		</view>
	</view>
</template>

<script>
	const App = getApp();
	export default {
		data() {
			return {
				progress: 0
			};
		},
		/* 生命周期函数--监听页面加载 */
		onLoad: function() {
			this.doUpData()
		},
		methods: {
			doUpData: function() {
				let appUrl = "https://config-1259258340.cos.ap-shanghai.myqcloud.com/newBee/nbShopDownLoad/newBee.apk";
				let downloadTask = uni.downloadFile({ //执行下载
					url: appUrl,
					success: downloadResult => {
						//下载成功
						if (downloadResult.statusCode === 200) {
							uni.showModal({
								title: '',
								content: '下载成功,确定现在更新吗?',
								confirmText: '更新',
								confirmColor: '#EE8F57',
								success: function(res) {
									if (res.confirm) {
										plus.runtime.install( //安装
											downloadResult.tempFilePath, {
												force: true
											},
											function() {
												console.log("更新成功")
												// utils.showToast('更新成功,重启中');
												plus.runtime.restart();
												uni.navigateBack()
											},
											function(e) {
												console.log("更新失败")
												// utils.showToast('更新失败');
											}
										);
									}
								}
							});
						}
					},
					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>
	.logo-wrap {
		position: fixed;
		top: 220rpx;
	}

	.logo-wrap .logo {
		width: 160rpx;
		height: 160rpx;
		overflow: hidden;
		border-radius: 50%;
	}

	.logo-wrap .title {
		margin-top: 20rpx;
	}

	.progress-wrap {
		position: fixed;
		bottom: 120rpx;
	}

	.progress-wrap .progress-box {
		width: 420rpx;
	}

	.progress-wrap .text {
		font-size: 22rpx;
		color: #6F6969;
		margin-top: 20rpx;
	}
</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值