import Taro from '@tarojs/api' ; import { MethodHandler } from '../../utils/handler' ; import { temporarilyNotSupport } from '../../utils' ; function getConnection ( ) { return navigator . connection || navigator . mozConnection || navigator . webkitConnection || navigator . msConnection ; } export const getNetworkType = ( options = { } ) => { const connection = getConnection ( ) ; const { success , fail , complete } = options ; const handle = new MethodHandler < Taro . getNetworkType . SuccessCallbackResult > ( { name : 'getNetworkType' , success , fail , complete } ) ; let networkType = 'unknown' ; if ( ! connection ) { return handle . success ( { networkType } ) ; } if ( ! isNaN ( Number ( connection . type ) ) ) { switch ( connection . type ) { case connection . WIFI : networkType = 'wifi' ; break ; case connection . CELL_3G : networkType = '3g' ; break ; case connection . CELL_2G : networkType = '2g' ; break ; default : networkType = 'unknown' ; } } else if ( connection . type ) { networkType = connection . type ; } else if ( connection . effectiveType ) { networkType = connection . effectiveType ; } return handle . success ( { networkType } ) ; } ; export const onNetworkStatusChange = ( cb ) => { const connection = getConnection ( ) ; if ( connection ) { connection . addEventListener ( 'change' , function ( ) { getNetworkType ( ) . then ( res => { const { networkType } = res ; const isConnected = networkType !== 'none' ; const obj = { isConnected , networkType } ; cb ( obj ) ; } ) ; } ) ; } } ; export const offNetworkStatusChange = temporarilyNotSupport ( 'offNetworkStatusChange' ) ; export const getLocalIPAddress = temporarilyNotSupport ( 'getLocalIPAddress' ) ;