使用uniapp开发APP实现版本更新并安装

通过一个弹窗去实现版本更新提示

在 首页 中判断是否显示 更新提示 弹窗

弹窗组件

<template>
	<view>
		<u-overlay :show="show">
			<view class="updatetips-whole">
				<view class="updatetips">
					<view>
						<view class="updatetips-head">
							<image src="../static/images/updatetips.png"></image>
							<view class="updatetips-version">
								发现新版本</br>
								V{{versionNum}}
							</view>
						</view>
						<view class="updatetips-content">{{versionContent}}</view>
						<!-- 进度条 -->
						<u-line-progress
							v-if="isProgress"
							:percentage="30"
							activeColor="#55D88A"
							:showText="false"
							height="4"
						></u-line-progress>
					</view>
					<view class="updatetips-btn-disable" v-if="isProgress">立即更新</view>
					<view class="updatetips-btn" v-else @click="downloadBtn()">立即更新</view>
				</view>
			</view>
		</u-overlay>
	</view>
</template>

<script>
export default {
	props:['show','versionNum','versionContent','downloadUrl'],
	data() {
		return {
			progress:0,
			isProgress:false
		};
	},
	methods: {
		downloadBtn(){
			this.isProgress=true
			const downloadTask = uni.downloadFile({
				url:this.downloadUrl,
				success: (res) => {
					//安装
					plus.runtime.install(
						res.tempFilePath, {
							force: true
						},
						function(_res) {
							plus.runtime.restart();
						}
					)
				},
				fail: (err)=> {
					uni.$u.toast('下载失败')
				}
			})
			// 查看下载进度
			downloadTask.onProgressUpdate((res) => {
				this.progress=res.progress
			})
		}
	}
};
</script>
<style scoped>
	.updatetips-whole{
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
	}
	.updatetips{
		position: relative;
		width: 80%;
		min-height: 100rpx;
		background-color: #fff;
		border-radius: 20rpx;
	}
	.updatetips-head{
		position: relative;
		width: 100%;
		height: 200rpx;
	}
	.updatetips-head>image{
		position: absolute;
		top: -72rpx;
		width: 100%;
		height: 280rpx;
	}
	.updatetips-version{
		position: absolute;
		top: 30rpx;
		left: 50rpx;
		color: #fff;
		font-size: 40rpx;
	}
	.updatetips-content{
		width: 80%;
		min-height: 100rpx;
		margin: 40rpx auto;
	}
	.updatetips-btn-disable{
		height: 120rpx;
		line-height: 120rpx;
		text-align: center;
		color: #E6E6E6;
	}
	.updatetips-btn{
		height: 120rpx;
		line-height: 120rpx;
		text-align: center;
		color: #55D88A;
	}
	.updatetips-btn::before{
		content: "";
		width: 85%;
		height: 1px;
		background-color: #E6E6E6;
		position: absolute;
		left: 50%;
		transform: translate(-50%,-50%);
	}
</style>

在父组件中使用

onLoad() {
		// 获取版本号
		get().then(res=>{
			var versionNum = uni.getStorageSync('version')
			// 判断当前版本号和后端返回的版本号是否一致
			if(versionNum!=res.data.versionNumber){
				this.show=true
				this.versionContent=res.data.content
				this.versionNum=res.data.versionNumber
				this.downloadUrl=res.data.url
			}
		}).catch(err=>{
			
		})
	},

注:获取本地包版本号

APP.vue

onLaunch: function() {
		//获取本地版本信息
		plus.runtime.getProperty(plus.runtime.appid, (info) => {
			uni.setStorageSync('version', info.version)
		})
	},

在manifest.json中输入本次版本号

 

  • 3
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
UniApp中,实现app版本更新可以通过以下步骤进行操作。首先,在关于我们的页面中应添加版本更新操作,可以通过后端加入一个字段来标识是否需要进行强制更新。如果需要强制更新,那么在进入app时就需要进行版本检测和对比,如果当前版本与最新版本不一致,则必须进行更新,否则无法使用app。如果版本一致,则无需进行提示。这个逻辑基本与非强制更新相同。 在打包时,还需要注意两个重要的修改。首先,要修改应用版本名称,确保其在升级时高于上一次设置的版本号。其次,要修改应用版本号,确保其为一个整数,并且在升级时高于上一次设置的值,这样在更新app时需要下载最新的包才不会出现问题。 另外,UniApp还提供了一个版本更新的js文件,该文件省去了自己编写布局和进度条的麻烦。通过使用这个js文件,可以直接生成更新弹窗,并且可以配置是否进行强制更新,并且还可以看到更新的进度。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [uniapp版本更新](https://blog.csdn.net/m0_51431448/article/details/130326109)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [uni-app版本更新](https://download.csdn.net/download/weixin_44052462/13188942)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值