import configService from '@/common/service/config.service.js'
//APP更新
export default function appUpdate() {
plus.runtime.getProperty(plus.runtime.appid, function (wgtinfo) {
var wgtinfo = wgtinfo;
var versionCode = wgtinfo.versionCode
var platform = uni.getSystemInfoSync().platform;
uni.request({
url: configService.apiUrl + 你的地址, //检查更新的服务器地址
header: {
'content-type': 'application/json'
},
data: {
appId: plus.runtime.appid,
version: versionCode,
appType: platform == "android" ? 1 : platform == "ios" ? 2 : 3
},
method: "POST",
success: (res) => {
console.log(res.data)
if (null != res.data.result) {
let client_version = wgtinfo.version;
// 只对比小版本,只要小于服务器里的版本就更新
if ((Number(client_version.split(".")[2]) < Number(res.data.result.smallVersion))) {
// 安卓整包更新
if (res.data.result.appType == 1) {
// 提醒用户更新
uni.showModal({
title: '版本更新',
content: '有新的版本发版,检测到当前最新版本为:' + res.data.result.version + '(' + res.data.result.smallVersion + ')' + '是否立即进行版本下载?',
confirmText: '立即更新',
cancelText: '稍后进行',
success: (showResult) => {
if (showResult.confirm) {
plus.nativeUI.toast("正在准备环境,请稍后!");
var dtask = plus.downloader.createDownload(res.data.result.packageUrl, {
method: 'GET'
}, function(d, status) {
if (status == 200) {
var path = d.filename;
plus.runtime.install(path);
} else {
plus.nativeUI.alert('版本更新失败:' + status);
}
});
dtask.start();
}
}
})
} else if (res.data.result.appType == 2) {
// 提醒用户更新
uni.showModal({
title: '版本更新',
content: '有新的版本发版,检测到当前最新版本为:' + res.data.result.version + '(' + res.data.result.smallVersion + ')' + '是否立即进行版本下载?',
confirmText: '立即更新',
cancelText: '稍后进行',
success: (showResult) => {
if (showResult.confirm) {
// ios更新
let appleId = 1571073151
plus.runtime.launchApplication({
action: `https://itunes.apple.com/cn/app/id${appleId}?mt=8`
}, function(e) {
console.log('Open system default browser failed: ' + e.message);
});
}
},
})
} else if (res.data.result.appType == 3) {
// wgt 更新
uni.downloadFile({
url: res.data.result.packageUrl,
success: (downloadResult) => {
if (downloadResult.statusCode === 200) {
plus.runtime.install(downloadResult.tempFilePath, {
force: true
}, function() {
plus.runtime.restart();
}, function(e) {
plus.nativeUI.toast(`热更新失败:${e.message}`);
});
}
}
});
}
}
}
}
})
});
}
uniapp自动更新
最新推荐文章于 2024-08-09 15:22:18 发布