系统信息
通过 uni.getSystemInfo 获取设备信息
uni.getSystemInfo({success: res => {console.log(JSON.stringify(res))}});
返回数据
{errMsg:"getSystemInfo:ok",brand:"devtools", // 手机品牌model:"iPhone 6", // 手机型号system:"iOS 10.0.1", // 操作系统版本version:"6.6.3", // 引擎版本号pixelRatio:2, // 设备像素比screenWidth:375, // 屏幕宽度screenHeight:667, // 屏幕高度statusBarHeight:20, // 状态栏的高度language:"zh", // 应用设置的语言SDKVersion:"2.0.4", // 客户端基础库版本batteryLevel:100,benchmarkLevel:1,fontSizeSetting:16, // 用户字体大小设置platform:"devtools", // 客户端平台windowHeight:603, // 可使用窗口高度windowWidth:375 // 可使用窗口宽度}
以同步的方式获取:
let info = uni.getSystemInfoSync()
成功回调的参数包括:
| 参数 | 说明 | 平台差异说明 |
|---|---|---|
| brand | 手机品牌 | 微信小程序 |
| model | 手机型号 | |
| pixelRatio | 设备像素比 | |
| screenWidth | 屏幕宽度 | |
| screenHeight | 屏幕高度 | |
| windowWidth | 可使用窗口宽度 | |
| windowHeight | 可使用窗口高度 | |
| windowTop | 可使用窗口的顶部位置 | 5+App、H5 |
| windowBottom | 可使用窗口的底部位置 | 5+App、H5 |
| statusBarHeight | 状态栏的高度 | |
| language | 应用设置的语言 | |
| version | 引擎版本号 | 微信小程序、5+App |
| system | 操作系统版本 | |
| platform | 客户端平台 | |
| fontSizeSetting | 用户字体大小设置。以“我-设置-通用-字体大小”中的设置为准,单位:px | 微信小程序 |
| SDKVersion | 客户端基础库版本 | 微信小程序、5+App |
网络状态
通过 uni.getNetworkType 获取网络状态
uni.getNetworkType({success: state => {console.log(JSON.stringify(state)); // {"errMsg":"getNetworkType:ok","networkType":"4g"}},fail: err => {console.log(JSON.stringify(err));}})
success 回调将带出一个 networkType 对象
networkType 有效值
| 值 | 说明 | 平台支持度 |
|---|---|---|
| wifi | wifi 网络 | |
| 2g | 2g 网络 | |
| 3g | 3g 网络 | |
| 4g | 4g 网络 | |
| ethernet | 有线网络 | 5+App |
| unknown | Android 下不常见的网络类型 | |
| none | 无网络 |
监听网络变化
通过 uni.onNetworkStatusChange(CALLBACK) 可监听网络状态变化
uni.onNetworkStatusChange(res => {console.log(res.isConnected);console.log(res.networkType);});
CALLBACK 返回参数
- isConnected Boolean 当前是否有网络连接 头条小程序不支持
- networkType String 网络类型
剪贴板
设置剪贴板
uni.setClipboardData({data: 'hello',success: function () {console.log('success');}});
获取剪贴板数据
uni.getClipboardData({success: function (res) {console.log(res.data); // hello}});
加速度计
uni.startAccelerometer 开始监听,通过 uni.onAccelerometerChange 监听加速度数据, 频率:5次/秒,接口调用后会自动开始监听,可使用 uni.stopAccelerometer 停止监听。
<template><view><button @click="start">开始获取</button><button @click="stop">停止获取</button></view></template><script>export default {mounted () {uni.onAccelerometerChange(res => {console.log(JSON.stringify(res));// {"x":0.695175,"y":-0.466599,"z":9.795013,"errMsg":"enableAccelerometer:ok"}})},methods: {start () {uni.startAccelerometer();},stop () {uni.stopAccelerometer();}}}</script>
监听数据:
- x Number X 轴
- y Number Y 轴
- z Number Z 轴
罗盘
uni.startAccelerometer 开始监听,通过 uni.onCompassChange 监听罗盘数据, 频率:5次/秒,接口调用后会自动开始监听,可使用 uni.stopAccelerometer 停止监听。
<template><view><button @click="start">开始获取</button><button @click="stop">停止获取</button></view></template><script>export default {mounted () {uni.onCompassChange(function (res) {console.log(JSON.stringify(res));// {"direction":169.100098,"errMsg":"enableCompass:ok"}});},methods: {start () {uni.startCompass();},stop () {uni.stopCompass();}}}</script>
监听数据:
- direction Number 面对的方向度数
震动
uni.vibrateLong(); // 长震动(400ms)uni.vibrateShort(); // 短震动(15ms), 部分手机(比如iOS)不支持
屏幕亮度
获取屏幕亮度
uni.getScreenBrightness({success: res => {console.log(JSON.stringify(res));// {"errMsg":"getScreenBrightness:ok","value":0.08235294}}})
开启屏幕常亮
uni.setKeepScreenOn({keepScreenOn: true});
设置屏幕亮度
value 为屏幕亮度值,0为最暗,1为最亮。
<template><view><view><text>设置屏幕亮度</text></view><slider :value="light" @change="setBright" step="5" /></view></template><script>export default {data() {return {light: 10}},methods: {setBright (e) {this.light = e.detail.valueuni.setScreenBrightness({value: this.light / 100,success: () => {console.log('success');}});}}}</script>
拨打电话
uni.makePhoneCall({phoneNumber: '114' //仅为示例});
扫码
uni.scanCode({success: function (res) {console.log('条码类型:' + res.scanType);console.log('条码内容:' + res.result);}});
选项参数:
| 参数名 | 类型 | 必填 | 说明 | 平台差异说明 |
|---|---|---|---|---|
| onlyFromCamera | Boolean | 否 | 是否只能从相机扫码,不允许从相册选择图片 | 头条小程序不支持 |
| scanType | Array | 否 | 扫码类型,参数类型是数组,二维码是’qrCode’,一维码是’barCode’,DataMatrix是 ‘datamatrix’,pdf417是’pdf417’。 | 头条小程序不支持 |
成功的回调参数:
| 参数 | 说明 | 平台差异说明 |
|---|---|---|
| result | 所扫码的内容 | |
| scanType | 所扫码的类型 | 5+App、微信小程序、百度小程序 |
| charSet | 所扫码的字符集 | 5+App、微信小程序、百度小程序 |
| path | 当所扫的码为当前应用的合法二维码时,会返回此字段,内容为二维码携带的 path。 | 5+App、微信小程序、百度小程序 |
