app热更新 app自动更新版本

app自动更新,我这里实现的思路就是,在pc端有个版本更新的增删改查页面,可以上传打包好的安装包以及版本号。

在app里再通过调后端接口来和本地版本比较,如果当前版本比接口中的版本低就弹出提示更新的弹窗。也可以通过点击来触发事件

我是直接写到app.vue中 一启动项目就看是否需要更新

onLaunch: function() {
			// 初始化系统
			this.initSystem()
}

methods: {
/**
			 * 初始化系统
			 */
	initSystem() {
				const that = this
				uni.getSystemInfo({
					success: function(e) {
						// app整包更新检测
						// console.log(e)
						if (e.platform == 'android') {
							that.appUpgrade(e.platform)
						}

					}
				})
			},
/**
			 * app整包更新检测
			 */
	async appUpgrade(platform) {
				let that = this
				//#ifdef APP-PLUS
				// 我的获取版本  这是我们后端给的接口 获取版本信息
				let urrl = config.baseUrl + 'xxxxx'
				try {
					await uni.request({
						url: urrl,
						method: 'GET',
						success: (res => {
							this.versionNums = res.data.data
						})
					})
					plus.runtime.getProperty(plus.runtime.appid, inf => {
						const wgtVer = inf.version; //获取当前版本号 这里我没用,我是直接从vuex里拿出来用的
//*************this.$utils.version是我自己封装的方法,引入的,看下一个代码块儿!!!!!!!!!!!!!!!
						const flag = this.$utils.version(this.versionNums.systemVersion, this.vuex_vesion)
						console.log(this.versionNums.systemVersion, '数据库版本');
						console.log(flag, that.vuex_vesion, '版本检测');
						// localStorage.setItem('wgtVer',that.vuex_vesion)
						if (flag) {
							uni.showModal({
								title: "检测到有新版本!",
								content: "请更新app到最新版本!",
								cancelText: "暂不更新", // 取消按钮的文字  
								confirmText: "立即更新", // 确认按钮的文字 
								success: (res) => {
									if (res.confirm) { //当用户确定更新,执行更新
										uni.$u.vuex("vuex_vesion", this.versionNums.systemVersion);
										that.doUpData();
									}
								}
							})
						}
					});

				} catch (e) {
					console.log(e);
				}
				//#endif
			},
	doUpData() {
				let _this = this,
					// downUrl = config.baseUrl + '/admin/sys-file/nsbdby/244866379463402d98e994f02513c52b.apk';
					downUrl = config.baseUrl + this.versionNums.systemVersionAddress;
				console.log(downUrl, 'downUrl')
				// 创建下载任务
				var dtask = plus.downloader.createDownload(downUrl, {}, function(d, status) {
					// 下载完成
					if (status == 200) {
						plus.runtime.install(plus.io.convertLocalFileSystemURL(d
							.filename), {}, {}, function(error) {
							uni.showToast({
								title: '安装失败',
								mask: false,
								duration: 1500
							});
						});
					} else {
						// console.log("Download failed: " + status);
						uni.showToast({
							title: '更新失败',
							mask: false,
							duration: 1500
						});
					}
				});
				try {
					dtask.start(); // 开启下载的任务
					var prg = 0;
					var showLoading = plus.nativeUI.showWaiting(
						"正在下载"); //创建一个showWaiting对象 
					dtask.addEventListener('statechanged', function(
						task,
						status
					) {
						// 给下载任务设置一个监听 并根据状态  做操作
						switch (task.state) {
							case 1:
								showLoading.setTitle("正在下载");
								break;
							case 2:
								showLoading.setTitle("已连接到服务器");
								break;
							case 3:
								prg = parseInt(
									(parseFloat(task.downloadedSize) /
										parseFloat(task.totalSize)) *
									100
								);
								showLoading.setTitle("  正在下载" + prg + "%  ");
								break;
							case 4:
								plus.nativeUI.closeWaiting();
								//下载完成 
								break;
						}
					});
				} catch (err) {
					plus.nativeUI.closeWaiting();
					uni.showToast({
						title: '更新失败-03',
						mask: false,
						duration: 1500
					});
				}
			},

}
/**

	  * 比较版本号的大小,如果curV 大于 reqV,则返回true,否则返回false

	  * @param {String} curV   最新

	  * @param {String} reqV   当前

	  */
	version(curV, reqV) {
		var arr1 = curV.toString().split('.');
		var arr2 = reqV.toString().split('.');
		//将两个版本号拆成数字
		var minL = Math.min(arr1.length, arr2.length);
		var pos = 0; //当前比较位
		var diff = 0; //当前为位比较是否相等
		var flag = false;
		//逐个比较如果当前位相等则继续比较下一位
		while (pos < minL) {
			diff = parseInt(arr1[pos]) - parseInt(arr2[pos]);
			if (diff == 0) {
				pos++;
				continue;
			} else if (diff > 0) {
				flag = true;
				break;
			} else {
				flag = false;
				break;
			}
		}
		return flag;
	},

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值