diff --git a/lib/apis.js b/lib/apis.js index 028630125c71b16e498324bcc33faf9355dc3b42..e229cdce7ed014aaa9acaf51cfe6b8c4a2d09de1 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -73,6 +73,7 @@ const device = [ 'onMemoryWarning', 'getNetworkType', 'onNetworkStatusChange', + 'offNetworkStatusChange', 'onAccelerometerChange', 'offAccelerometerChange', 'startAccelerometer', diff --git a/lib/modules.json b/lib/modules.json index 42c953af51968e554fa107f8dcc7277f7486976f..a4d1d6dfa8d282e20e2e9da2d432a44cc01477e4 100644 --- a/lib/modules.json +++ b/lib/modules.json @@ -77,10 +77,12 @@ "uni.onMemoryWarning": true, "uni.getNetworkType": true, "uni.onNetworkStatusChange": true, + "uni.offNetworkStatusChange": true, "uni.onAccelerometerChange": true, "uni.startAccelerometer": true, "uni.stopAccelerometer": true, "uni.onCompassChange": true, + "uni.offCompassChange": true, "uni.startCompass": true, "uni.stopCompass": true, "uni.onGyroscopeChange": true, diff --git a/src/core/service/api/device/network.js b/src/core/service/api/device/network.js index 7be6d6c3766520a1961915cec6838b91690c7028..7b2603811b58d603b01a62d67a1f0c1320db209f 100644 --- a/src/core/service/api/device/network.js +++ b/src/core/service/api/device/network.js @@ -16,4 +16,14 @@ onMethod('onNetworkStatusChange', res => { export function onNetworkStatusChange (callbackId) { callbacks.push(callbackId) -} +} + +export function offNetworkStatusChange (callbackId) { + // 暂不支持移除所有监听 + if (callbackId) { + const index = callbacks.indexOf(callbackId) + if (index >= 0) { + callbacks.splice(index, 1) + } + } +} diff --git a/src/platforms/h5/service/api/device/network-info.js b/src/platforms/h5/service/api/device/network-info.js index 3f50737f20fb7304a55b2774ca5808b7a846053a..bd94693ab15caae6c612bdad414209beb4d27cd4 100644 --- a/src/platforms/h5/service/api/device/network-info.js +++ b/src/platforms/h5/service/api/device/network-info.js @@ -28,6 +28,25 @@ export function onNetworkStatusChange (callbackId) { } } +export function offNetworkStatusChange (callbackId) { + // 暂不支持移除所有监听 + if (callbackId) { + const index = callbackIds.indexOf(callbackId) + if (index >= 0) { + callbackIds.splice(index, 1) + } + } + if (!callbackIds.length) { + const connection = navigator.connection || navigator.webkitConnection + if (connection) { + connection.removeEventListener('change', changeHandler) + } else { + window.removeEventListener('offline', changeHandler) + window.removeEventListener('online', changeHandler) + } + } +} + export function getNetworkType () { const connection = navigator.connection || navigator.webkitConnection let networkType = 'unknown'