uniapp自动更新

import configService from '@/common/service/config.service.js'
//APP更新
export default function appUpdate() {
	plus.runtime.getProperty(plus.runtime.appid, function (wgtinfo) {
		var wgtinfo = wgtinfo;
		var versionCode = wgtinfo.versionCode
		var platform = uni.getSystemInfoSync().platform;
		uni.request({
			url: configService.apiUrl + 你的地址, //检查更新的服务器地址
			header: {
				'content-type': 'application/json'
			},
			data: {
				appId: plus.runtime.appid,
				version: versionCode,
				appType: platform == "android" ? 1 : platform == "ios" ? 2 : 3
			},
			method: "POST",
			success: (res) => {
				console.log(res.data)
				if (null != res.data.result) {
					let client_version = wgtinfo.version;
					// 只对比小版本,只要小于服务器里的版本就更新
					if ((Number(client_version.split(".")[2]) < Number(res.data.result.smallVersion))) {
						// 安卓整包更新
						if (res.data.result.appType == 1) {
							// 提醒用户更新
							uni.showModal({
								title: '版本更新',
								content: '有新的版本发版,检测到当前最新版本为:' + res.data.result.version + '(' + res.data.result.smallVersion + ')' + '是否立即进行版本下载?',
								confirmText: '立即更新',
								cancelText: '稍后进行',
								success: (showResult) => {
									if (showResult.confirm) {
										plus.nativeUI.toast("正在准备环境,请稍后!");
										var dtask = plus.downloader.createDownload(res.data.result.packageUrl, {
											method: 'GET'
										}, function(d, status) {
											if (status == 200) {
												var path = d.filename;
												plus.runtime.install(path);
											} else {
												plus.nativeUI.alert('版本更新失败:' + status);
											}
										});
										dtask.start();
									}
								}
							})
						} else if (res.data.result.appType == 2) {
							// 提醒用户更新
							uni.showModal({
								title: '版本更新',
								content: '有新的版本发版,检测到当前最新版本为:' + res.data.result.version + '(' + res.data.result.smallVersion + ')' + '是否立即进行版本下载?',
								confirmText: '立即更新',
								cancelText: '稍后进行',
								success: (showResult) => {
									if (showResult.confirm) {
										// ios更新
										let appleId = 1571073151
										plus.runtime.launchApplication({
											action: `https://itunes.apple.com/cn/app/id${appleId}?mt=8`
										}, function(e) {
											console.log('Open system default browser failed: ' + e.message);
										});
									}
								},
							})
						} else if (res.data.result.appType == 3) {
							// wgt 更新
							uni.downloadFile({
								url: res.data.result.packageUrl,
								success: (downloadResult) => {
									if (downloadResult.statusCode === 200) {
										plus.runtime.install(downloadResult.tempFilePath, {
											force: true
										}, function() {
											plus.runtime.restart();
										}, function(e) {
											plus.nativeUI.toast(`热更新失败:${e.message}`);
										});
									}
								}
							});
						}
					}
				}
			}
		})
	});
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值