使用uni-app开发app,发布线上后仍需不断更新版本。更新方式可分为:
1、静默更新:wgt包。用户不用再次下载安装包,只需要根据提示重启即可更新,但静默更新只适用于代码的更新,无法更新插件。
2、整包更新:apk包。需要更新安装包,但也能更新插件。
1、使用插件:
插件地址:https://ext.dcloud.net.cn/plugin?id=7286
2、在manifest.json -> App权限配置勾选权限:
3、代码实现:
在App.vue中:
import silenceUpdate from '@/uni_modules/rt-uni-update/js_sdk/silence-update.js'
onLaunch() {
// 加载系统信息
this.$store.dispatch('SystemInfo');
this.getUpdate();
},
methods:{
getUpdate() {
//#ifdef APP-PLUS
const apkUrl = environment.baseURL + "/download?fileName=app.apk&delete=false" // 文件包下载地址
const wgtUrl = environment.baseURL + "/download?fileName=app.wgt&delete=false"
const token = uni.getStorageSync('token');
var that = this
plus.runtime.getProperty(plus.runtime.appid, (inf) => {
uni.request({
url: environment.baseURL + '/system/config', //调用后台接口,要返回我们更新所需要的版本号、更新类型等内容
method: "GET",
header: {
Authorization: 'Bearer ' + token,
'Content-Type': 'application/json;charset=UTF-8'
},
success: (res) => {
if (res.data.code == 200) {
let result = JSON.parse(res.data.msg)
if (result.type === 1 && (Number(uni.getStorageSync(
"currentVersion")) < result.wgtVersion)) { // 静默更新
silenceUpdate(wgtUrl)
uni.setStorageSync("currentVersion", Number(result.wgtVersion))
}
if (result.type === 0 && (inf.version < result.apkVersion)) { // 整包更新
let data = {
edition_url: apkUrl, // 文件包下载地址
edition_force: 1, // 强制更新
package_type: 0,
edition_name: result.apkVersion, // 要更新的版本
}
uni.navigateTo({
url: '/uni_modules/rt-uni-update/components/rt-uni-update/rt-uni-update?obj=' +
JSON.stringify(data)
});
}
}
}
})
});
//#endif
},
}