uniapp 在App端和微信小程序端热更新

hotUpdate() {
				//热更新 plus只适合于手机上 不能用于h5,小程序
				// 热更新不会更新app的版本号
				// 这里用一个常量定义(每次修改版本号将设置里面的版本号手动更新)

				//plus.runtime.getProperty(),获取本机安装的版本,具体使用请参照如下链接    
				//https://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.WidgetInfo
				// plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {

				// });	
				// #ifdef APP-PLUS
				
				uni.downloadFile({
					url: "http://10.128.5.64:8082/104.wgt",
					success: (res) => {
						if (res.statusCode === 200) {
							//console.log("下载完毕,临时保存路径是", res.tempFilePath);
							uni.saveFile({
								tempFilePath: res.tempFilePath,
								success: (res) => {
									//console.log("文件已保存至", res.savedFilePath);

									plus.runtime.install(res.savedFilePath, null, (res) => {

										uni.showModal({
											title: '安装成功!',
											content: '已重新进入应用',
											success: function(res) {
												if (res.confirm) {
													// 应用热重启,重新启动进入首页。
													plus.runtime.restart();
													//console.log('用户点击确定');
												} else if (res.cancel) {
													//console.log('用户点击取消');
												}
											}
										});
									}, (err) => {
										console.log(err);
										uni.showToast({
											title: '安装失败',
											icon: 'none'
										});
									});
								},
								fail: (err) => {
									console.log("文件保存失败" + err);
								}
							})
						}

					},
					fail: (err) => {
						console.log("下载失败");
						console.log(err);
					}
				})
				// #endif

				// #ifdef MP-WEIXIN || H5
				/////////////////////////////小程序上传新的版本后 代码自动触发,不需要开发者触发//////////
				if (wx.canIUse('getUpdateManager')) {
					const updateManager = wx.getUpdateManager()
					updateManager.onCheckForUpdate(function(res) {
						// console.log('onCheckForUpdate====', res)
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							// console.log('res.hasUpdate====')
							updateManager.onUpdateReady(function() {
								wx.showModal({
									title: '更新提示',
									content: '新版本已经准备好,是否重启应用?',
									success: function(res) {
										// console.log('success====', res)
										// res: {errMsg: "showModal: ok", cancel: false, confirm: true}
										if (res.confirm) {
											// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
											updateManager.applyUpdate()
										}
									}
								})
							})
							updateManager.onUpdateFailed(function() {
								// 新的版本下载失败
								wx.showModal({
									title: '已经有新版本了哟~',
									content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
								})
							})
						}
					})
				}
				// #endif
			}

 

posted @ 2022-09-27 10:45  liuw_flexi  阅读(672)  评论(0编辑  收藏  举报