import $config from './config.js';
const update = {
checkUpdate(handUpdate) {
console.log('========检查更新======');
var _this = this;
var client_type = plus.os.name == "Android" ? 1 : 2;
var current_version = 0;
var apiurl = $config.baseUrl + "/download/index.php";
var response = "";
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
current_version = inf.version;
console.log("版本号", current_version)
uni.request({
method: 'POST',
url: apiurl + '?m=version',
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: {
current_version: current_version,
client_type: client_type
},
success: (ret) => {
response = ret.data;
console.log(response, "response====")
if (response.code == 200) {
var result = response.result;
plus.nativeUI.confirm("系统检测到更新,是否更新",
function(e) {
if (e.index == 0) {
if (result.pack_url != "") { // pack_url 不为空的时候 直接跳转
plus.runtime.openURL(result.pack_url);
}
if (result.pack_type == 2) { //IOS更新 直接打开下载页面
plus.runtime.openURL(apiurl);
} else { // 安卓或WGT更新,下载文件并执行安装
_this.install(result.pack_file);
}
}
})
} else {
if (handUpdate) {
uni.showToast({
icon: 'none',
title: "已是最新版本"
});
} else {
console.log('无更新');
}
}
}
});
});
},
install(fileUrl) {
// 创建更新任务
const dtask = plus.downloader.createDownload(fileUrl, {
filename: "_doc/update/"
}, function(d, status) {
if (status == 200) {
plus.runtime.install(d.filename, {
force: true
}, function() {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert("应用资源更新完成!", function() {
plus.runtime.restart();
});
}, function(e) {
plus.nativeUI.closeWaiting();
console.log("安装文件失败[" + e.code + "]:" + e.message);
});
} else {
console.log("下载失败!");
}
plus.nativeUI.closeWaiting();
})
// 监听更新进度
let prg = 0;
const showLoading = plus.nativeUI.showWaiting("更新中...");
dtask.start();
dtask.addEventListener("statechanged", function(task, status) {
switch (task.state) {
case 1:
showLoading.setTitle("正在下载");
break;
case 3:
prg = parseInt(
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
);
showLoading.setTitle(" 正在下载" + prg + "% ");
break;
case 4:
plus.nativeUI.closeWaiting();
break;
}
});
}
}
export default update
最后补充一下
plus.runtime.getProperty(plus.runtime.appid,(appInfo)=>{
// appInfo为当前应用程序的所有信息
console.log(JSON.stringify(appInfo));
// 获取版本名称
console.log(appInfo.version);
// 获取版本号
console.log(appInfo.versionCode);
// 获取当前应用id
console.log(appInfo.appid);
});