uni-app学习笔记之版本更新

实现逻辑

1. 获取版本号 2. 版本号对比 3. 下载新版本

1、APP.vue页面中:

//初始化完成时,会触发 onLaunch(全局只触发一次)
onLaunch: function() {
		   //获取你打包时设置的版本号,然后设置到storage里		    
			console.log("version:", config.version);
			uni.setStorage({
		           version: config.version		           
		       })			
		},

2、Home首页中调用:进入app后直接进入到首页,我是在首页做的判断。

//页面加载时调用查询版本的方法
onLoad() {
		this.getSysVersion()
	},

以下代码在Home页面中的methods中

//获取版本列表
getSysVersion() {
	const _this = this;
	//获取缓存中,当前app的版本号
	uni.getStorage({
		key: 'wgtinfoVersionCode',
		success: (res) => {
			this.wgtinfoVersionCode = res.data;
			API.queryAppList({
					page: 1,
					limit: 10
				}).then(res => {
					if (res.status) {
						let appdata = res.data.records[0]
						// 后端返回的app版本和当前app版本比较
						if (Number(appdata.versionCode) != Number(this.wgtinfoVersionCode)) {
							uni.showModal({
								title: "发现新版本",
								content: "确认下载更新",
								success: (res) => {
									if (res.confirm ==
										true) { //当用户确定更新,执行更新,下面方法进行下载app
											this.appdownLoad(appdata.url);
									} else {
										return false
									}
								}
							})
						}
					}
				})
				.catch(err => {
					console.log(err)
				})
		}
	})
},
 
//执行下载app
appdownLoad(url) {
	var that = this;
	uni.showLoading({
		title: '安装包下载中……'
	})
	const downloadTask = uni.downloadFile({
		url: url, //服务器 app 的下载地址
		success: (downloadResult) => {
			uni.hideLoading();
			console.log(downloadResult)
			if (downloadResult.statusCode === 200) {
				uni.showModal({
					title: '',
					content: '下载成功,是否允许安装新版本?',
					confirmText: '确定',
					success: function(res) {
						if (res.confirm == true) {
							plus.runtime.install( //安装
								downloadResult.tempFilePath, {
									force: true
								},
								function(res) {
									utils.showToast('更新成功,重启中');
									plus.runtime.restart();
								}
							);
						}
					}
				});
			}
		}
	});
},
opendb-app-versions
// 文档教程: https://uniapp.dcloud.net.cn/uniCloud/schema
{
	"bsonType": "object",
	"required": ["appid", "platform", "version", "url", "contents", "type"],
	"permission": {
		"read": false,
		"create": false,
		"update": false,
		"delete": false
	},
	"properties": {
		"_id": {
			"description": "记录id,自动生成"
		},
		"appid": {
			"bsonType": "string",
			"trim": "both",
			"description": "应用的AppID",
			"label": "AppID",
			"componentForEdit": {
				"name": "uni-easyinput",
				"props": {
					"disabled": true
				}
			}
		},
		"name": {
			"bsonType": "string",
			"trim": "both",
			"description": "应用名称",
			"label": "应用名称",
			"componentForEdit": {
				"name": "uni-easyinput",
				"props": {
					"disabled": true
				}
			}
		},
		"title": {
			"bsonType": "string",
			"description": "更新标题",
			"label": "更新标题"
		},
		"contents": {
			"bsonType": "string",
			"description": "更新内容",
			"label": "更新内容",
			"componentForEdit": {
				"name": "textarea"
			},
			"componentForShow": {
				"name": "textarea",
				"props": {
					"disabled": true
				}
			}
		},
		"platform": {
			"bsonType": "array",
			"enum": [{
				"value": "Android",
				"text": "安卓"
			}, {
				"value": "iOS",
				"text": "苹果"
			}],
			"description": "更新平台,Android || iOS || [Android, iOS]",
			"label": "平台"
		},
		"type": {
			"bsonType": "string",
			"enum": [{
				"value": "native_app",
				"text": "原生App安装包"
			}, {
				"value": "wgt",
				"text": "Wgt资源包"
			}],
			"description": "安装包类型,native_app || wgt",
			"label": "安装包类型"
		},
		"version": {
			"bsonType": "string",
			"description": "当前包版本号,必须大于当前线上发行版本号",
			"label": "版本号"
		},
		"min_uni_version": {
			"bsonType": "string",
			"description": "原生App最低版本",
			"label": "原生App最低版本"
		},
		"url": {
			"bsonType": "string",
			"description": "可下载安装包地址",
			"label": "包地址"
		},
		"stable_publish": {
			"bsonType": "bool",
			"description": "是否上线发行",
			"label": "上线发行"
		},
		"is_silently": {
			"bsonType": "bool",
			"description": "是否静默更新",
			"label": "静默更新",
			"defaultValue": false
		},
		"is_mandatory": {
			"bsonType": "bool",
			"description": "是否强制更新",
			"label": "强制更新",
			"defaultValue": false
		},
		"create_date": {
			"bsonType": "timestamp",
			"label": "上传时间",
			"forceDefaultValue": {
				"$env": "now"
			},
			"componentForEdit": {
				"name": "uni-dateformat"
			}
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光明有我16620122910

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值