# uni.getNetworkType(options) GitCodeGitHub

获取网络类型

# getNetworkType 兼容性

Web 微信小程序 Android iOS HarmonyOS
4.0 4.41 3.9 4.11 4.61

# 参数

名称 类型 必填 默认值 兼容性 描述
options GetNetworkTypeOptions -
-
名称 类型 必备 默认值 兼容性 描述
success (result: GetNetworkTypeSuccess) => void null
接口调用成功的回调函数
fail (result: GetNetworkTypeFail) => void null
接口调用失败的回调函数
complete (result: any) => void null
接口调用结束的回调函数(调用成功、失败都会执行)

# GetNetworkTypeSuccess 的属性值

名称 类型 必备 默认值 兼容性 描述
networkType string -
网络类型
hasSystemProxy boolean -
需要基础库: 2.22.1

设备是否使用了网络代理
signalStrength number -
信号强弱,单位 dbm
weakNet boolean -
需要基础库: 3.5.3

是否处于弱网环境

# GetNetworkTypeFail 的属性值

名称 类型 必备 默认值 兼容性 描述
errSubject string -
-
统一错误主题(模块)名称
errCode number -
-
统一错误码
errMsg string -
-
统一错误描述信息
data any -
-
错误信息中包含的数据
cause Error -
-
源错误信息,可以包含多个错误,详见SourceError
name string -
-
message string -
-

# 示例

示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见

扫码体验(手机浏览器跳转到App直达页)

示例

<template>
  <page-head :title="title"></page-head>
  <view class="uni-padding-wrap uni-common-mt">
    <view class="uni-container">
      <view class="uni-center">网络状态</view>
      <view v-if="hasNetworkType == false">
        <view class="uni-center uni-common-mt">未获取</view>
        <view class="uni-center uni-common-mt">请点击下面按钮获取网络状态</view>
      </view>
      <view v-if="hasNetworkType == true">
        <view class="uni-center uni-common-mt">{{networkType}}</view>
      </view>
    </view>
    <view class="uni-btn-v uni-common-mt">
      <button type="primary" @tap="getNetworkType">获取设备网络状态</button>
      <button class="uni-common-mt" @tap="clear">清空</button>
    </view>
  </view>
</template>
<script>
  export default {
    data() {
      return {
        title: 'getNetworkType',
        hasNetworkType: false,
        networkType: '',
        connectedWifi: '',
        //自动化测试例专用
        jest_result: false,
      }
    },
    onLoad() {
    },
    onUnload: function () {
      this.networkType = '';
      this.hasNetworkType = false;
    },
    methods: {
      getNetworkType: function () {
        uni.getNetworkType({
          success: (res) => {
            console.log(res)
            this.hasNetworkType = true;
            this.networkType = res.networkType
          },
          fail: () => {
            uni.showModal({
              content: '获取失败!',
              showCancel: false
            })
          }
        })
      },
      clear: function () {
        this.hasNetworkType = false;
        this.networkType = '';
        this.connectedWifi = '';
      },
      //自动化测试例专用
      jest_getNetworkType() {
        uni.getNetworkType({
          success: () => {
            this.jest_result = true;
          },
          fail: () => {
            this.jest_result = false;
          }
        })
      }
    }
  }
</script>

<style>

</style>

# 参见

# 通用类型

# GeneralCallbackResult

名称 类型 必备 默认值 兼容性 描述
errMsg string -
错误信息