// 工具函数 export default { // 网络状态获取 getNetwork() { uni.getNetworkType({ success(res) { if (res.networkType === 'none') { uni.$u.toast('网络连接不可用,请检查网络') } } }) }, // 网络状态监听 onNetwork() { uni.onNetworkStatusChange(res => { // 当前是否有网络连接 if (!res.isConnected) { uni.$u.toast('网络连接不可用,请检查网络') return uni.$u.route('/pages/mine/login') } // 网络类型 switch (res.networkType) { case 'unknown': uni.$u.toast('未知的网络状态') break; case 'wifi': uni.$u.toast('已切换至wifi网络') break; case '4g': uni.$u.toast('已切换至蜂窝移动网络,请注意流量消耗') break; case '3g': uni.$u.toast('已切换至蜂窝移动网络,请注意流量消耗') break; case '2g': uni.$u.toast('已切换至蜂窝移动网络,请注意流量消耗') break; case 'none': uni.$u.toast('当前无网络连接,请检查网络') break; } }) }, // 热更新 update() { // #ifdef APP-PLUS plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { uni.request({ url: 'http://www.example.com/update/', data: { version: widgetInfo.version, name: widgetInfo.name }, success: (result) => { var data = result.data; if (data.update && data.wgtUrl) { uni.downloadFile({ url: data.wgtUrl, success: (downloadResult) => { if (downloadResult.statusCode === 200) { plus.runtime.install(downloadResult.tempFilePath, { force: false }, function() { console.log('install success...'); plus.runtime.restart(); }, function(e) { console.error('install fail...'); }); } } }); } } }); }); // #endif }, } // 检测小程序是否更新 export const VersionUpdate = () => { // 判断应用的 getUpdateManager 是否在当前版本可用 console.log(uni.canIUse('getUpdateManager')); if (uni.canIUse('getUpdateManager')) { const updateManager = uni.getUpdateManager() // 向小程序后台请求完新版本信息 updateManager.onCheckForUpdate(function(res) { if (res.hasUpdate) { //小程序有新版本,静默下载新版本,新版本下载完成 updateManager.onUpdateReady(function() { //模态弹窗(确认、取消) uni.showModal({ title: '更新提示', content: '小程序已发布新版本,是否重启?', success: function(res) { //用户点击确定 if (res.confirm) { //当新版本下载完成,调用该方法会强制当前小程序应用上新版本并重启 updateManager.applyUpdate() } //用户点击取消 else if (res.cancel) { //强制用户更新,弹出第二次弹窗 uni.showModal({ title: '提示', content: '小程序已发布新版本,是否重启', showCancel: false, //隐藏取消按钮 success: function(res) { //第二次提示后,强制更新 if (res.confirm) { // 当新版本下载完成,调用该方法会强制当前小程序应用上新版本并重启 updateManager.applyUpdate() } else if (res.cancel) { //重新回到版本更新提示 VersionUpdate() } }, }) } }, }) }) // 当新版本下载失败 updateManager.onUpdateFailed(function() { uni.showModal({ title: '提示', content: '请您删除当前小程序,重新打开小程序', }) }) } }) } else { // 提示用户在最新版本的客户端上体验 uni.showModal({ title: '温馨提示', content: '当前微信版本过低,可能无法使用该功能,请升级到最新版本后重试。', }) } }