uniapp App更新

import $config from './config.js';
const update = {
	checkUpdate(handUpdate) {
		console.log('========检查更新======');
		var _this = this;
		var client_type = plus.os.name == "Android" ? 1 : 2;
		var current_version = 0;
		var apiurl = $config.baseUrl + "/download/index.php";
		var response = "";
		plus.runtime.getProperty(plus.runtime.appid, function(inf) {
			current_version = inf.version;
			console.log("版本号", current_version)
			uni.request({
				method: 'POST',
				url: apiurl + '?m=version',
				header: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				data: {
					current_version: current_version,
					client_type: client_type
				},
				success: (ret) => {
					response = ret.data;
					console.log(response, "response====")
					if (response.code == 200) {
						var result = response.result;
						plus.nativeUI.confirm("系统检测到更新,是否更新",
							function(e) {
								if (e.index == 0) {
									if (result.pack_url != "") { // pack_url 不为空的时候 直接跳转
										plus.runtime.openURL(result.pack_url);
									}
									if (result.pack_type == 2) { //IOS更新 直接打开下载页面
										plus.runtime.openURL(apiurl);
									} else { // 安卓或WGT更新,下载文件并执行安装
										_this.install(result.pack_file);
									}
								}
							})
					} else {
						if (handUpdate) {
							uni.showToast({
								icon: 'none',
								title: "已是最新版本"
							});
						} else {
							console.log('无更新');
						}
					}
				}
			});
		});
	},
	install(fileUrl) {
		// 创建更新任务
		const dtask = plus.downloader.createDownload(fileUrl, {
			filename: "_doc/update/"
		}, function(d, status) {
			if (status == 200) {
				plus.runtime.install(d.filename, {
					force: true
				}, function() {
					plus.nativeUI.closeWaiting();
					plus.nativeUI.alert("应用资源更新完成!", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装文件失败[" + e.code + "]:" + e.message);
				});
			} else {
				console.log("下载失败!");
			}
			plus.nativeUI.closeWaiting();
		})
		// 监听更新进度
		let prg = 0;
		const showLoading = plus.nativeUI.showWaiting("更新中...");
		dtask.start();
		dtask.addEventListener("statechanged", function(task, status) {
			switch (task.state) {
				case 1:
					showLoading.setTitle("正在下载");
					break;
				case 3:
					prg = parseInt(
						(parseFloat(task.downloadedSize) /
							parseFloat(task.totalSize)) *
						100
					);
					showLoading.setTitle("  正在下载" + prg + "%  ");
					break;
				case 4:
					plus.nativeUI.closeWaiting();
					break;
			}
		});
	}
}
export default update

最后补充一下

plus.runtime.getProperty(plus.runtime.appid,(appInfo)=>{
	// appInfo为当前应用程序的所有信息
	console.log(JSON.stringify(appInfo));
	// 获取版本名称
	console.log(appInfo.version);
	// 获取版本号
	console.log(appInfo.versionCode);
	// 获取当前应用id
	console.log(appInfo.appid);
});

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值