众所周知,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>