import { Injectable } from '@angular/core'; import { ToastController } from 'ionic-angular'; import { Network } from '@ionic-native/network' import { BaseLog as Log } from 'base-log' const log = new Log('Net'); let locked, checkLockTime = 10000; @Injectable() export class Net extends Network{ constructor(public network: Network,public toastCtrl: ToastController) { super(); } // == 检查网络 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; }, checkLockTime); } getType = () => { return this.network.type; } isBad = () => { return this.network.type == '2G'; } isBroken = () => { return this.network.type == 'none'; } toast = (message) => { let toast = this.toastCtrl.create({ message, duration: 3000, position: 'top' }); return toast.present(); } }