//监听小程序初始化
onLaunch(e) {
let _this = this;
//判断网络
_this.updateNetwork();
// 小程序主动更新
_this.updateManager();
},
//封装接口get,post中
fail(res) {
//判断网络
_this.updateNetwork();
},
updateNetwork() {
let _this = this;
// 初次加载判断网络情况
wx.getNetworkType({
success(res) {
let networkType = res.networkType
if (networkType === 'none') {
_this.showError('无网络连接');
return false;
}
}
});
// 监听网络状态变化
wx.onNetworkStatusChange(function(res) {
if (!res.isConnected) {
_this.showError('网络已断开');
return false;
}
});
},
/**
* 小程序主动更新
*/
updateManager() {
if (!wx.canIUse('getUpdateManager')) {
return false;
}
let updateManager = wx.getUpdateManager();
updateManager.onCheckForUpdate(function(res) {
// 请求完新版本信息的回调
// console.log(res.hasUpdate)
});
updateManager.onUpdateReady(function() {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,即将重启应用',
showCancel: false,
success(res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
});
});
updateManager.onUpdateFailed(function() {
// 新的版本下载失败
wx.showModal({
title: '更新提示',
content: '新版本下载失败',
showCancel: false
})
});
},