import { Injectable } from '@angular/core'; import { ToastController } from 'ionic-angular'; import { Network } from '@ionic-native/network' import { Log } from "../utils/log"; const log = new Log('Net'); let locked,lockTime = 10000; @Injectable() export class Net extends Network{ constructor(public network: Network,public toastCtrl: ToastController) { super(); } disconnectSub connectSub monitor = () => { this.disconnectSub = this.network.onDisconnect().subscribe(() => { }); this.connectSub = this.network.onConnect().subscribe(() => { }); } unMonitor = () => { this.disconnectSub.unsubscribe(); this.connectSub.unsubscribe(); } getType = () => { return this.network.type; } isBad = () => { return this.network.type == '2G'; } isBroken = () => { return this.network.type == 'none'; } check = () => { if(locked) return; locked = true; log.info('network type',this.network.type,true); if(this.isBroken()) this.toast('当前网络不可用,请检查您的网络设置'); if(this.isBad()) this.toast('当前网络不稳定,请检查您的网络'); setTimeout(()=>{ locked = false; },lockTime); } toast = (message) => { let toast = this.toastCtrl.create({ message, duration: 3000, position: 'top' }); return toast.present(); } }