uniapp 的 热更新

前期介绍

  1. 获取 uniapp 的包信息
    可参考 H5产业联盟 https://www.html5plus.org/doc/zh_cn/nativeui.html

  2. 制作热更新wgt包
    在这里插入图片描述
    在这里插入图片描述
    对配置的js/nvue文件进行原生混淆,这个类似于代码加密

  3. uniapp的wgt包好像不支持差异化热更新,每次都会拉取全部的代码,在云端制作并下发wgt包

  4. 将制作的wgt包,放到我们的服务器上,然后请求接口时,传递 app版本手机系统 两个参数,以此来获取 热更新包,并安装

下面是详细代码

<script>
    export default {
        globalData: {
        	  baseUrl: 'http://www.aliba.com', //用于热更新的地址
        	  hotVersion: 1, //热更新版本
		},
		onLaunch() {
			this.hotUpdate();
		},
		methods: {
			hotUpdate() {
				// #ifdef APP-PLUS
				const _this = this;
				//获取App的包信息
				plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
					//调取接口获取当前系统(Android/ios)的这个App版本是否有热更新
					_this.getHotUpdateInfo({
						appversion: widgetInfo.version, //app版本号
						phoneSystem: uni.getSystemInfoSync().platform //app系统
					}).then(res => {
						console.log(res);
						if (_this.globalData.hotVersion > res.hotVersion) {						
								_this.downloadHotUpdate(res.url);		
						}
					})
				})
				// #endif 
			},
			
			getHotUpdateInfo(params) {
				return new Promise((resolve, reject) => {
					uni.request({
					    url: this.globalData.baseUrl + '/api/getAppHot',
						data: params,
						method: 'GET',
						success: (res) => {
							resolve(res.data);
						},
						fail: (err) => {
							console.log(err);
							reject(err);
						},
						complete: () => {

						}
					})
				})
			},
			
			downloadHotUpdate(hotUrl) {
				uni.downloadFile({
					url: hotUrl,
					success: (res) => {
						console.log(res);
						plus.runtime.install(
							res.tempFilePath, {
								force: true //是否强制安装
							},
							function() {
								console.log('install success...');
							},
							function() {
								console.error('install fail...');
							},
						)
					},
					fail: (err) => {
						console.log(err)
						uni.showToast({
							title: '下载失败',
							icon: 'none'
						})
					}
				})
			},
			
		}
    }
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值