data: function() {
return {
version: "1.0.3"//版本号
}
},
onLaunch: function() {
```// 设置横屏方向
plus.screen.lockOrientation('landscape-primary');
//获取mac地址
var net = plus.android.importClass("java.net.NetworkInterface")
var wl0 = net.getByName('wlan0')
var macByte = wl0.getHardwareAddress()
var str = ''
for (var i = 0; i < macByte.length; i++) {
var tmp = "";
var num = macByte[i];
if (num < 0) {
tmp =(255+num+1).toString(16);
} else {
tmp = num.toString(16);
}
if (tmp.length == 1) {
tmp = "0" + tmp;
}
str += tmp.length == 1 ?"0"+ tmp+":":tmp+":";
}
str = str.substring(0,str.length-1);
//获取系统信息
uni.getSystemInfo({
success: (res) => {
//检测当前平台,如果是安卓则启动安卓更新
if (res.platform == "android") {
this.AndroidCheckUpdate();
}
}
})
},
methods: {
AndroidCheckUpdate: function() {
var _this = this;
this.$http.get("/queuing/web/queuing/apkUpdate?appType=2", {}).then(data => {
if(data.code==200){
let appver = data.data;
if (appver&&appver.appVersion != this.version) {
uni.showToast({
title: '有新的版本发布,程序已启动自动更新。新版本下载完成后将自动弹出安装程序。',
mask: false,
duration: 5000,
icon: "none"
});
var dtask = plus.downloader.createDownload(
appver.appUrl, {},
function(d, status) {
//下载完成
if (status == 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(d
.filename), {}, {}, function(error) {
uni.showToast({
title: '安装失败',
mask: false,
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败',
mask: false,
duration: 1500
});
}
});
try {
dtask.start(); // 开启下载的任务
var prg = 0;
var showLoading = plus.nativeUI.showWaiting(
"正在下载"); //创建一个showWaiting对象
dtask.addEventListener('statechanged', function(
task,
status
) {
// 给下载任务设置一个监听 并根据状态 做操作
switch (task.state) {
case 1:
showLoading.setTitle("正在下载");
break;
case 2:
showLoading.setTitle("已连接到服务器");
break;
case 3:
prg = parseInt(
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
);
showLoading.setTitle(" 正在下载" + prg + "% ");
break;
case 4:
plus.nativeUI.closeWaiting();
//下载完成
break;
}
});
} catch (err) {
plus.nativeUI.closeWaiting();
uni.showToast({
title: '更新失败-03',
mask: false,
duration: 1500
});
}
}
}
})
},
}
uniapp,apk自动更新
最新推荐文章于 2024-03-18 10:03:54 发布