App实现版本更新

众所周知,app上架后,会进行版本更新。那么是如何实现的呢?

一种是小更新,可以进行热更新;一种是大更新,需要重新安装。

下面是我自己用的比较简单的一版更新,希望对大家有所帮助。

一、在根目录下创建components文件夹,在components中创建 unit.js :

unit.js:

//widgetInfo.version 是当前版本号

//res.data.edition_id 是更新版本号

function geteditionApi() {
	return new Promise((resolve, reject) => {
		uni.request({
			url: 'https://xxxxxxx', // 获取更新信息的接口
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: 'POST',
			data: {
				id: 15
			},
			success: (res) => {
				resolve(res.data.data)
			}
		});
	})
}

// 热更新安卓
function getupdate() {
	return new Promise((resolve, reject) => {
		plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
			geteditionApi().then(res => {
				var currentVersions = widgetInfo.version.split('.'); //widgetInfo.version 是当前版本号
				var resultVersions = res.data.edition_id.split('.'); //res.data.edition_id 是更新版本号
				if (widgetInfo.version == res.data.edition_id) {
					res.data.zdstatus = 2
					resolve(res);
				} else {
					if (currentVersions[1] < resultVersions[1]) {
						res.data.zdstatus = 0
						resolve(res);
					} else {
						res.data.zdstatus = 1
						resolve(res);
					}
				}
			});
		});
	})
}

// 热更新IOS
function getupdateios() {
	return new Promise((resolve, reject) => {
		plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
			geteditionApi().then(res => {
				if (widgetInfo.version != res.data.apple_edition) {
					res.data.zdstatus = 1
					resolve(res);
				} else {
					res.data.zdstatus = 2
					resolve(res);
				}
			})
		});
	})
}

module.exports = {
	getupdate,
	getupdateios
}

二、在main.js中全局引入

import unit from '@/components/unit.js';
Vue.prototype.$unit = unit;

三、页面中使用:

index.vue:

<template>
    <view>
        <u-popup border-radius="20" v-model="show" :mask-close-able="false" mode="center">
			<view class="popfull">
				<view class="popfullcontent">
					<view class="popfullcontentitle">新版本更新</view>
					<view class="popfullcontenttext">{{upform.comm || '新版本更新'}}</view>
					<button class="popfullbtn" @click="uploadnow">立即更新</button>
				</view>
			</view>
		</u-popup>
    </view>
</template>

<script>
    export default {
		data() {
            return {
                upform: {
					comm: '' // 接口返回的版本更新内容。例如:8.11号进行的更新。参数名根据后端接口返回的填写即可
				}
            }
        },
        onShow() {
            let port = uni.getSystemInfoSync().platform //判断安卓、ios
			switch (port) {
				case 'android':
					this.exupdate()
					break;
				case 'ios':
					this.exupdateIOS()
					break;
				default:
					console.log('小程序');
					break;
			}
        },
        methods: {
            // 判断app版本更新IOS
			async exupdateIOS() {
				let res = await this.$unit.getupdateios()
				this.upform = res.data;
				let _data = res.data;
				if (_data.zdstatus == 2) {
					// uni.$u.toast('您已经是最新版')
				} else {
					this.show = true;
				}
			},
			// 判断App版本更新android
			async exupdate() {
				let res = await this.$unit.getupdate()
				this.upform = res.data;
				let _data = res.data;
				if (_data.zdstatus == 2) {
					// uni.$u.toast('您已经是最新版')
				} else {
					this.show = true;
				}
			},
			uploadnow() {
				let _data = this.upform;
				if (_data.zdstatus == 0) {
					if (plus.os.name == "Android") {
						plus.runtime.openURL(_data.src);
					} else {
						plus.runtime.launchApplication({
							action: `itms-apps://itunes.apple.com/cn/app/id${_data.apple_id}?mt=8`
						}, function(e) {});
					}
				} else {
					uni.downloadFile({
						url: _data.src,
						success: (ress) => {
							if (ress.statusCode === 200) {
								plus.runtime.install(ress.tempFilePath, {
									force: true
								}, function() {
									plus.runtime.restart();
								}, function(e) {});
							}
						},
						fail: (err) => {
							console.log(err)
						}
					})
				}
			}
        }
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乁*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值