当前位置: 首页 > article >正文

【小程序】基础API之系统API接口汇总

ty.env

环境变量

属性

USER_DATA_PATH string

文件系统中的用户目录路径 (本地路径),当操作文件时需使用此目录。

// 写入一个文件
const fileManager = await ty.getFileSystemManager();
const fileRoot = ty.env.USER_DATA_PATH;
const filePath = `${fileRoot}/test.json`
await fileManager.writeFile({
  filePath: filePath,
  data: `{"test": "test"}`,
});
 
// 分享该文件
ty.share({
  title: '分享一个文件',
  message: '这是一个 JSON 文件,调用系统分享',
  type: 'More',
  filePath: filePath,
  contentType: 'file',
  success() {
    console.log('分享成功');
  },
  fail(err) {
    console.log('分享失败', err);
  },
});

其他文件操作方法请参考 FileSystemManager。

ty.getSystemInfo

获取系统信息

需引入BaseKit,且在>=1.2.10版本才可使用

参数

Object object

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

属性类型说明
is24Hourboolean
systemstring
brandstring
modelstring
platformstring
timezoneIdstring
pixelRationumber
screenWidthnumber
screenHeightnumber
windowWidthnumber
windowHeightnumber
statusBarHeightnumber
languagestring
safeAreaSafeArea
albumAuthorizedboolean
cameraAuthorizedboolean
locationAuthorizedboolean
microphoneAuthorizedboolean
notificationAuthorizedboolean
notificationAlertAuthorizedboolean
notificationBadgeAuthorizedboolean
notificationSoundAuthorizedboolean
bluetoothEnabledboolean
locationEnabledboolean
wifiEnabledboolean
themeThemes
deviceOrientationOrientation

SafeArea

属性类型说明
leftnumber安全区域左上角横坐标
rightnumber安全区域右下角横坐标
topnumber安全区域左上角纵坐标
bottomnumber安全区域右下角纵坐标
widthnumber安全区域的宽度,单位逻辑像素
heightnumber安全区域的高度,单位逻辑像素

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

函数定义示例

/**
 * 获取系统信息
 */
export function getSystemInfo(params?: {
  complete?: () => void;
  success?: (params: {
    is24Hour: boolean;
    system: string;
    brand: string;
    model: string;
    platform: string;
    timezoneId: string;
    pixelRatio: number;
    screenWidth: number;
    screenHeight: number;
    windowWidth: number;
    windowHeight: number;
    statusBarHeight: number;
    language: string;
    safeArea: SafeArea;
    albumAuthorized: boolean;
    cameraAuthorized: boolean;
    locationAuthorized: boolean;
    microphoneAuthorized: boolean;
    notificationAuthorized: boolean;
    notificationAlertAuthorized: boolean;
    notificationBadgeAuthorized: boolean;
    notificationSoundAuthorized: boolean;
    bluetoothEnabled: boolean;
    locationEnabled: boolean;
    wifiEnabled: boolean;
    theme?: Themes;
    deviceOrientation?: Orientation;
  }) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.getSystemInfoSync

获取系统信息同步方法

需引入BaseKit,且在>=1.2.10版本才可使用

ty.getSystemInfo 的同步版本

返回值

属性类型说明
is24Hourboolean
systemstring
brandstring
modelstring
platformstring
timezoneIdstring
pixelRationumber
screenWidthnumber
screenHeightnumber
windowWidthnumber
windowHeightnumber
statusBarHeightnumber
languagestring
safeAreaSafeArea
albumAuthorizedboolean
cameraAuthorizedboolean
locationAuthorizedboolean
microphoneAuthorizedboolean
notificationAuthorizedboolean
notificationAlertAuthorizedboolean
notificationBadgeAuthorizedboolean
notificationSoundAuthorizedboolean
bluetoothEnabledboolean
locationEnabledboolean
wifiEnabledboolean
themeThemes
deviceOrientationOrientation

SafeArea

属性类型说明
leftnumber安全区域左上角横坐标
rightnumber安全区域右下角横坐标
topnumber安全区域左上角纵坐标
bottomnumber安全区域右下角纵坐标
widthnumber安全区域的宽度,单位逻辑像素
heightnumber安全区域的高度,单位逻辑像素

函数定义示例

/**
 * 获取系统信息
 */
export function getSystemInfoSync(): {
  is24Hour: boolean;
  system: string;
  brand: string;
  model: string;
  platform: string;
  timezoneId: string;
  pixelRatio: number;
  screenWidth: number;
  screenHeight: number;
  windowWidth: number;
  windowHeight: number;
  statusBarHeight: number;
  language: string;
  safeArea: SafeArea;
  albumAuthorized: boolean;
  cameraAuthorized: boolean;
  locationAuthorized: boolean;
  microphoneAuthorized: boolean;
  notificationAuthorized: boolean;
  notificationAlertAuthorized: boolean;
  notificationBadgeAuthorized: boolean;
  notificationSoundAuthorized: boolean;
  bluetoothEnabled: boolean;
  locationEnabled: boolean;
  wifiEnabled: boolean;
  theme?: Themes;
  deviceOrientation?: Orientation;
};

ty.getSystemSetting

获取设备设置

需引入BaseKit,且在>=2.5.0版本才可使用

参数

Object object

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

属性类型说明
bluetoothEnabledboolean蓝牙的系统开关,默认 false
locationEnabledboolean地理位置的系统开关,默认 false
wifiEnabledbooleanWi-Fi 的系统开关,默认 false
deviceOrientationstring设备方向, 默认竖屏 竖屏 = "portrait", 横屏 = "landscape"

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

函数定义示例

/**
 * 获取设备设置
 */
export function getSystemSetting(params?: {
  complete?: () => void;
  success?: (params: {
    /** 蓝牙的系统开关,默认false */
    bluetoothEnabled?: boolean;
    /** 地理位置的系统开关,默认false */
    locationEnabled?: boolean;
    /** Wi-Fi 的系统开关,默认false */
    wifiEnabled?: boolean;
    /**
     * 设备方向, 默认竖屏
     * 竖屏 = "portrait", 横屏 = "landscape"
     */
    deviceOrientation?: string;
  }) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.getDeviceInfo

获取设备基础信息

需引入BaseKit,且在>=2.4.3版本才可使用

参数

Object object

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

属性类型说明
abistring应用二进制接口类型(仅 Android 支持)
brandstring设备品牌
modelstring设备型号。新机型刚推出一段时间会显示 unknown。
systemstring操作系统及版本
platformstring客户端平台

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

函数定义示例

/**
 * 获取设备基础信息
 */
export function getDeviceInfo(params?: {
  complete?: () => void;
  success?: (params: {
    /** 应用二进制接口类型(仅 Android 支持) */
    abi: string;
    /** 设备品牌 */
    brand: string;
    /** 设备型号。新机型刚推出一段时间会显示unknown。 */
    model: string;
    /** 操作系统及版本 */
    system: string;
    /** 客户端平台 */
    platform: string;
  }) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.openSystemBluetoothSetting

跳转系统蓝牙设置页 (仅 Android)

需引入BaseKit,且在>=2.4.3版本才可使用

参数

Object object

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 跳转系统蓝牙设置页 (仅Android)
 */
export function openSystemBluetoothSetting(params?: {
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.updateVolume

设置系统音量

错误码标注 入参值范围超出 errorCode = 6, The volume value is invalid, it should be [0 - 1]

需引入BaseKit,且在>=2.4.3版本才可使用

参数

Object object

属性类型默认值必填说明
valuenumber音量,阈值【0 - 1】
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 设置系统音量
 *
 *错误码标注
 *入参值范围超出 errorCode = 6, The volume value is invalid, it should be [0 - 1]
 */
export function updateVolume(params: {
  /** 音量,阈值【0 - 1】 */
  value: number;
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.handleShortcut

操作快捷方式,包括添加和移除, 仅 iOS

需引入BizKit,且在>=3.1.1版本才可使用。

参数

Object object

属性类型默认值必填说明
typenumber操作类型。0-添加、1-移除
sceneIdstring场景 ID
namestring场景名称
iconUrlstring场景 Logo
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

属性类型说明
operationStepnumber操作步骤,0-添加、1-移除、2-更新、3-取消
operationStatusboolean操作状态,YES,表示成功;NO,表示失败

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

函数定义示例

/**
 * 操作快捷方式,包括添加和移除, 仅 iOS
 */
export function handleShortcut(params: {
  /** 操作类型。0-添加、1-移除 */
  type: number;
  /** 场景 ID */
  sceneId: string;
  /** 场景名称 */
  name: string;
  /** 场景 Logo */
  iconUrl?: string;
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: {
    /** 操作步骤,0-添加、1-移除、2-更新、3-取消 */
    operationStep: number;
    /** 操作状态,YES,表示成功;NO,表示失败 */
    operationStatus: boolean;
  }) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.isAssociatedShortcut

获取是否关联 siri 状态, 仅 iOS

需引入BizKit,且在>=3.1.1版本才可使用。

参数

Object object

属性类型默认值必填说明
sceneIdstring场景 ID
namestring场景名称
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

属性类型说明
isAssociatedboolean是否已关联

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

函数定义示例

/**
 * 获取是否关联 Siri 状态, 仅 iOS
 */
export function isAssociatedShortcut(params: {
  /** 场景 ID */
  sceneId: string;
  /** 场景名称 */
  name?: string;
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: {
    /** 是否已关联 */
    isAssociated: boolean;
  }) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

ty.isSupportedShortcut

是否支持 Siri, 仅 iOS

需引入BizKit,且在>=3.1.1版本才可使用。

参数

Object object

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

属性类型说明
isSupportedboolean是否支持

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

函数定义示例

/**
 * 是否支持 Siri, 仅 iOS
 */
export function isSupportedShortcut(params?: {
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: {
    /** 是否支持 */
    isSupported: boolean;
  }) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

 👉 立即开发。


http://www.kler.cn/a/231185.html

相关文章:

  • Nacos1.X源码解读(待完善)
  • 什么是网络渗透,应当如何防护?
  • 计算机网络(第六版)复习提纲26
  • SolidWorks学习笔记——入门知识1
  • Springboot简单设计两级缓存
  • openssl自签名CA根证书、服务端和客户端证书生成并模拟单向/双向证书验证
  • 【iOS ARKit】人形遮挡
  • 【文本到上下文 #10】探索地平线:GPT 和 NLP 中大型语言模型的未来
  • Mac电脑清空特别大型旧文件如何一键清理?
  • ubuntu22.04@laptop OpenCV Get Started: 004_cropping_image
  • BFS——C++
  • HTTP协议笔记
  • 最大子数组和[中等]
  • 课时17:本地变量_命令变量
  • 2024-02-08 思考-楚门的世界
  • 07:Kubectl 命令详解|K8S资源对象管理|K8S集群管理(重难点)
  • 6-3、T型加减速单片机程序【51单片机+L298N步进电机系列教程】
  • 9.4 OpenGL帧缓冲:纹理和帧缓冲之间的反馈循环
  • huggingface学习|用dreambooth和lora对stable diffusion模型进行微调
  • 【JS逆向六】(上)逆向模拟生成某网站的【sig】和【payload】的值 仅供学习