uniapp资源在线升级/热更新,整包热更新

注意:不管是任何方式的更新都需要在minifast中配置好版本号后再打包!

先介绍整包更新:

一般iOS Appstore的安装包,无法直接更新。App启动后检查有新版本,只能调整到Appstore,然后用户在Appstore的详情页点击更新按钮。

而Android App,可以直接下载新的apk,只要包名和证书不变,就可以覆盖安装。

首先在app启动入口检测是否需要更新:

uni.request({
			url: 'you url', //仅为示例,并非真实接口地址。
			method: 'GET',
			success: res => {
				const {
					url,
					versionName,
					description
				} = res.data;
				if (versionName > api.version) {
					router.push('mineList_update', {
						url,
						description
					});
				}
			},
			complete() {
				uni.hideLoading();
			}
		});

即,服务器返回最新版本号,与当前APP版本号对比,如果服务器版本号高,则跳转到下载页面,让用户选择是否更新,确认后下载并重启

onConfirm() {
			uni.request({
				url: 'your url', //仅为示例,并非真实接口地址。
				method: 'GET',
				success: res => {
					const { url, versionName, description } = res.data;

					this.showModel = false;
					util.toast('后台下载中...', 'none', 'bottom');
					this.downLoadUpdateFiles(url);
				},
				complete() {
					uni.hideLoading();
				}
			});
		},
		
		downLoadUpdateFiles(url) {
			let _this = this;
			 downloadTask = uni.downloadFile({
				url,
				success(res) {
					if (res.statusCode === 200) {
						console.log('下载成功!');
						plus.runtime.install(
							res.tempFilePath,
							{force: true},
							function() { plus.runtime.restart() },
							function(e) {}
						);
					}
				}
			}),downloadTask.onProgressUpdate(function(a) {
				if (_loadingProgress != a.progress) {
					 _loadingProgress = a.progress;
					 _this.progress = a.progress;
					console.log(_this.progress, "111");
				}
			});
		},

可以参考https://ask.dcloud.net.cn/article/34972


以下为部分资源更新

在 HBuilderX 中生成升级包(wgt):菜单->发行->原生App-制作移动App资源升级包

将生成的wgt包给后台上传到服务器

客户端检测升级,建议在app每次打开时检测是否有更新包,即检测代码写在onShow中。

// #ifdef APP-PLUS  
plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
    uni.request({  
        url: 'your update url',  // 自己修改
        data: {  
            version: widgetInfo.version,  
            name: widgetInfo.name  
        },  
        success: (result) => {  
            var data = result.data;  
            if (data.update && data.wgtUrl) {  
                uni.downloadFile({  
                    url: data.wgtUrl,  
                    success: (downloadResult) => {  
                        if (downloadResult.statusCode === 200) {  
                            plus.runtime.install(downloadResult.tempFilePath, {  
                                force: false  
                            }, function() {  
                                console.log('install success...');  
                                plus.runtime.restart(); // 存在用户体验优化的问题,参考下面的代码 
                            }, function(e) {  
                                console.error('install fail...');  
                            });  
                        }  
                    }  
                });  
            }  
        }  
    });  
});  
// #endif

注意事项:

此段代码需要条件编译

安装 wgt 资源包成功后,必须执行 plus.runtime.restart(),否则新的内容并不会生效。在下载WGT包过程中APP内部不会存在提示,因此,直接restart可能会导致用户在使用某功能过程中APP突然自动刷新重启,可以在下载完成后,提示用户是否需要重启

if (downloadResult.statusCode === 200) {
					plus.runtime.install(downloadResult.tempFilePath, {
						force: false
					}, function() {
						console.log('install success...');
						uni.showModal({
							content: "部分资源已更新,重启后生效,是否立即重启?",
							cancelText: "稍后重启",
							confirmText: "立即重启",
							success(res) {
								console.log(res);
								if (res.confirm) {
									plus.runtime.restart();
								}
							},
						})
					}, function(e) {
						console.error('install fail...');
					});
				}

可以参考https://ask.dcloud.net.cn/article/35667

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值