设备能力 Device
Device API 提供访问设备硬件和系统信息的能力。
在线试用
点击按钮后在此展示 JSON 或说明
导入
ts
import woo from 'mini-sdk'
// 或
import { device } from 'mini-sdk'设备信息
getSystemInfo
获取系统信息。
ts
const info = await woo.getSystemInfo()返回值 GetSystemInfoResult:
| 字段 | 类型 | 说明 |
|---|---|---|
platform | string | 操作系统(ios / android / simulator) |
system | string | 系统版本 |
brand | string | 设备品牌 |
model | string | 设备型号 |
screenWidth | number | 屏幕宽度 (px) |
screenHeight | number | 屏幕高度 (px) |
windowWidth | number | 可用窗口宽度 |
windowHeight | number | 可用窗口高度 |
statusBarHeight | number | 状态栏高度 |
language | string | 系统语言 |
version | string | 宿主 App 版本 |
SDKVersion | string | SDK 版本 |
getDeviceId
获取设备唯一标识。
ts
const { deviceId } = await woo.getDeviceId()getDeviceInfo
获取设备基础信息。
ts
const info = await woo.getDeviceInfo()getAppInfo
获取当前小程序应用信息。
ts
const info = await woo.getAppInfo()
// { appId, appName, env: 'simulator' | 'production', ... }位置
getLocation
获取当前地理位置。
ts
const location = await woo.getLocation()
console.log(location.latitude, location.longitude)权限提示
调用 getLocation 会请求用户的位置权限。如用户拒绝,将抛出 BridgeErrorCode.PermissionDenied 错误。
振动
vibrateShort / vibrateLong
触发设备振动。
ts
await woo.vibrateShort() // 短振(15ms)
await woo.vibrateLong() // 长振(400ms)网络
getNetworkType
获取当前网络类型。
ts
const { networkType } = await woo.getNetworkType()
// 'wifi' | '4g' | '3g' | '2g' | 'none' | 'unknown'扫码
scanCode
调起扫码界面。
ts
const result = await woo.scanCode({
scanType: ['qrCode', 'barCode'],
})
console.log(result.result) // 扫码结果
console.log(result.scanType) // 码类型图片
chooseImage
从相册选择图片。
ts
const result = await woo.chooseImage({
count: 3, // 最多选择 3 张
sourceType: ['album', 'camera'],
})
console.log(result.tempFilePaths) // 临时文件路径数组takePhoto
拍照。
ts
const result = await woo.takePhoto({
quality: 'high',
})
console.log(result.tempFilePath) // 照片临时路径文件上传
uploadFile
上传文件到服务器。
ts
const result = await woo.uploadFile({
url: 'https://api.example.com/upload',
filePath: tempFilePath,
name: 'file',
header: { Authorization: 'Bearer xxx' },
formData: { type: 'avatar' },
})地图
openMap
打开系统地图导航。
ts
await woo.openMap({
latitude: 39.908860,
longitude: 116.397390,
name: '天安门',
address: '北京市东城区',
})通信
makePhoneCall
拨打电话。
ts
await woo.makePhoneCall({ phoneNumber: '10086' })屏幕
setScreenOrientation / getScreenOrientation
设置/获取屏幕方向。
ts
await woo.setScreenOrientation({ orientation: 'landscape' })
const { orientation } = await woo.getScreenOrientation()onScreenOrientationChange
监听屏幕方向变化。
ts
woo.onScreenOrientationChange((result) => {
console.log('屏幕方向:', result.orientation)
})setScreenBrightness / getScreenBrightness
设置/获取屏幕亮度。
ts
await woo.setScreenBrightness({ value: 0.8 }) // 0~1
const { value } = await woo.getScreenBrightness()keepScreenOn
保持屏幕常亮。
ts
await woo.keepScreenOn({ keepScreenOn: true })跨应用
navigateToMiniApp
跳转到其他小程序。
ts
await woo.navigateToMiniApp({
appId: 'target-app-id',
path: '/pages/index/index',
extraData: { from: 'my-app' },
})openWallet
打开钱包。
ts
await woo.openWallet({ type: 'payment' })API 速查表
| 方法 | 说明 | 签名 |
|---|---|---|
getSystemInfo | 获取系统信息 | () → Promise<SystemInfo> |
getSystemInfoSync | 同步获取系统信息 | () → SystemInfo |
getNetworkType | 获取当前网络类型 | () → Promise<{ networkType }> |
onNetworkStatusChange | 监听网络状态变化 | (callback) → void |
makePhoneCall | 拨打电话 | ({ phoneNumber }) → Promise<void> |
scanCode | 扫码 | ({ scanType? }) → Promise<{ result, scanType }> |
getLocation | 获取地理位置 | ({ type? }) → Promise<{ latitude, longitude, ... }> |
vibrateLong / vibrateShort | 长/短振动 | () → Promise<void> |
getBatteryInfo | 获取电池信息 | () → Promise<{ level, isCharging }> |
getScreenBrightness | 获取屏幕亮度 | () → Promise<{ value }> |
setScreenBrightness | 设置屏幕亮度 | ({ value }) → Promise<void> |
openWallet | 打开钱包 | ({ type }) → Promise<void> |