uniapp版本升级

1.样式

登录进到首页,弹出更新提示框,且不可以关闭,侧边返回直接退出!

有关代码:

html 复制代码
<uv-popup ref="popupUpdate" round="8" :close-on-click-overlay="false">
			<view style="width: 450rpx; display: flex; flex-direction: column; align-items: center; padding: 20rpx 0 50rpx 0">
				<text style="font-size: 23px">提示</text>
				<text style="font-size: 18px; margin-top: 20rpx">发现新版本,立即升级!</text>
				<view style="margin-top: 30rpx">
					<uv-button size="normal" type="primary" @click="sureUpdate" :customStyle="{ width: '300rpx' }">确认</uv-button>
				</view>
			</view>
		</uv-popup>

2.操作代码

javascript 复制代码
const sureUpdate = () => {
	showFly(true, '');
};
const showFly = (isShowCancel, url) => {
	if (true) {
		// clearInterval(updateAction)
		uni.showLoading({
			title: '更新中......'
		});
		try {
			const appUrl = config.appDownloadUrl;
			const downloadTask = uni.downloadFile({
				url: appUrl, // 这个是最新版本apk包的地址
				success: (res) => {
					uni.hideLoading();
					if (res.statusCode === 200) {
						console.log('res.tempFilePath :>> ', res.tempFilePath);
						plus.runtime.install(
							res.tempFilePath,
							{
								force: true
							},
							(_res) => {
								uni.showToast({
									title: '更新成功,重启中',
									duration: 1600
								});
								popupUpdate.value.close();
								plus.runtime.restart();
								uni.hideToast();
							}
						);
					} else {
						uni.showToast({
							title: '下载失败!',
							icon: 'none',
							duration: 800
						});
					}
				}
			});
			// 下载进度
			downloadTask.onProgressUpdate((res) => {
				// _this.startDown = true;
				// _this.calcPro(res.progress);
				console.log('下载进度' + res.progress);
				// console.log('已经下载的数据长度' + res.totalBytesWritten);
				// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				// // 测试条件,取消下载任务。
				// if (res.progress > 50) {
				//     downloadTask.abort();
				// }
			});
		} catch (error) {
			console.log('error :>> ', error);
			uni.hideLoading();
		} finally {
		}
	}
};

2.是否更新

在进到首页最重要的一环就是判断系统是否需要更新,在onShow里使用

如果接口返回的版本>系统现在本身的版本号,那么就弹出提示框!

javascript 复制代码
nextTick(() => {
		// popupUpdate.value.open();
		isUpdate();
	});
const isUpdate = () => {
	const WXP_APP_VERSION = uni.getStorageSync('appVersion');
	const curVersion = uni.getStorageSync('version'); // 当前版本

	let str = WXP_APP_VERSION.split('.').join('');

	let str1 = curVersion.split('.').join('');
	console.log(Number(str));
	console.log(Number(str1));
	if (curVersion && WXP_APP_VERSION) {
		if (Number(str) > Number(str1)) {
			popupUpdate.value.open();
		} else {
			popupUpdate.value.close();
		}
	} else {
		popupUpdate.value.close();
	}
};
相关推荐
PineappleCoder4 小时前
还在重复下载资源?HTTP 缓存让二次访问 “零请求”,用户体验翻倍
前端·性能优化
拉不动的猪4 小时前
webpack编译中为什么不建议load替换ast中节点删除consolg.log
前端·javascript·webpack
李姆斯4 小时前
Agent时代下,ToB前端的UI和交互会往哪走?
前端·agent·交互设计
源码获取_wx:Fegn08955 小时前
基于springboot + vue健身房管理系统
java·开发语言·前端·vue.js·spring boot·后端·spring
闲谈共视5 小时前
基于去中心化社交与AI智能服务的Web钱包商业开发的可行性
前端·人工智能·去中心化·区块链
CreasyChan5 小时前
C# 反射详解
开发语言·前端·windows·unity·c#·游戏开发
JIngJaneIL5 小时前
基于Java+ vue智慧医药系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
阿蒙Amon6 小时前
JavaScript学习笔记:6.表达式和运算符
javascript·笔记·学习
hashiqimiya7 小时前
两个步骤,打包war,tomcat使用war包
java·服务器·前端
小a杰.7 小时前
Flutter 设计系统构建指南
开发语言·javascript·ecmascript