集成 SDK

更新时间:2025-12-30 08:12:14下载pdf

本文介绍如何在鸿蒙项目中,集成 MiniApp SDK,使之能够构建智能生活小程序,运行在您的 Smart App 上。

准备工作

在集成 MiniApp SDK 之前,您需要在 涂鸦开发者平台 上:

  • 注册开发者账号、创建产品、创建功能点等。
  • 创建智能生活 App SDK,然后获取激活 SDK 的密钥,以保证小程序功能的正常运行。具体操作流程,请参考 准备工作

前提条件

请确保您已经集成了涂鸦 智能生活 App SDK。具体集成方式,请参考 快速集成鸿蒙 App SDK

导入 SDK

导入 SDK 时,请按照以下步骤配置依赖:

  1. 配置 ohpm 的涂鸦环境

    编辑项目根目录的 .ohpmrc 文件,添加涂鸦 OHPM 源:

    registry=https://ohpm.openharmony.cn/ohpm/
    strict_ssl=false
    publish_registry=https://ohpm-repo.tuya.com/repos/ohpm
    fetch_timeout=60000
    max_concurrent=50
    retry_times=1
    retry_interval=1000
    publish_id=9BA32537EE
    
    # 涂鸦 OHPM 源
    @thingsmart:registry=https://ohpm-repo.tuya.com/repos/ohpm
    @rnoh:registry=https://ohpm-repo.tuya.com/repos/ohpm
    @tuya-oh:registry=https://ohpm-repo.tuya.com/repos/ohpm
    
    # 认证信息
    //ohpm-repo.tuya.com/repos/ohpm/:_auth="你的认证token"
    
  2. 配置插件

    插件可以从 npmjs 安装,也可以使用本地文件。在工程根目录的 oh-package.json5 中添加:

    {
      "devDependencies": {
        // 方式一:从 npmjs 安装(推荐)
        "@tuya-client/thing-hmbom-plugin": "^0.0.2",
        // 方式二:使用本地文件(将插件包放到 plugins/ 目录下,参考Demo)
        // "@tuya-harmony/thingHMBOMPlugin": "file:./plugins/tuya-harmony-thingHMBOMPlugin-1.0.0.tgz"
      }
    }
    

    在项目根目录的 hvigorfile.ts 中配置:

    import { thingBOMPlugin } from '@tuya-harmony/thingHMBOMPlugin';
    import { appTasks } from '@ohos/hvigor-ohos-plugin';
    
    export default {
        system: appTasks,
        plugins: [
            thingBOMPlugin()
        ]
    }
    
  3. 固定业务包版本

    sdk-requirements.json 是必要的文件,必须放到工程目录中(如 entry/sdk-requirements.json)。插件会根据此文件中的版本信息来安装依赖。

    编辑 entry/sdk-requirements.json,指定所需的业务包版本:

    {
      "tag": "feature/publish",
      "versions": {
        "@thingsmart/userlib": "1.1.18",
        "@thingsmart/channel": "1.1.62",
        "@thingsmart/theme": "1.1.4",
        // ... 其他业务包
      }
    }
    

    更多业务包版本信息,请参考示例项目中的 entry/sdk-requirements.json 文件。

  4. 配置业务组件

    在需要使用对应功能的 module 目录下(如 entry/),编辑 thing-biz-components.json 文件,添加项目使用的业务组件:

    {
      "components": {
        "@thingsmart/userlib": "1.1.18",
        "@thingsmart/dynamicinitializer": "0.9.2",
        "@thingsmart/dynamicreflect": "0.8.0",
        "@thingsmart/channel": "1.1.62",
        "@thingsmart/theme": "1.1.4",
        "@thingsmart/homelib": "1.0.5",
        "@thingsmart/scenelib": "1.1.0",
        "@thingsmart/messagelib": "1.1.3",
        "@thingsmart/thinglogmodule": "1.0.5",
        "@thingsmart/activatorlib": "1.1.1",
        "@thingsmart/activator": "1.1.14",
        "@thingsmart/device": "1.1.73",
        "@thingsmart/godzillapage": "1.5.8",
        "@thingsmart/thingrouter": "1.0.0",
        "@thingsmart/launchpipeline": "1.0.4",
        "@thingsmart/tunibasekit": "3.29.5",
        "@thingsmart/tuniminikit": "1.0.47",
        "@thingsmart/tunidevicekit": "1.1.4",
        "@thingsmart/tunibizkit": "1.0.38",
        "@thingsmart/tuniipckit": "1.2.30",
        "@thingsmart/tunimediakit": "1.1.1",
        "@thingsmart/tunihomekit": "1.0.11",
        "@thingsmart/mapkit": "1.0.17",
        "@thingsmart/thingpanelmodule": "1.0.28"
      }
    }
    
  5. SDK 初始化

    // 动态反射初始化
    thingDynamicReflect.init(getReflectMetaInfo(`${bundleName}/${moduleName}`));
    
    // Router 初始化
    ThingRouter.initialize()
    ThingRouter.getServiceMgr().setServiceModuleMap(servicesModuleMap());
    
    // 主题初始化
    ThingTheme.getInstance().init(this.context)
    
    // 初始化任务
    const initMetaInfo = thingDynamicReflect.filterMetaInfo(ThingInitializer.DEFAULT_GROUP);
    new ThingInitializer(initMetaInfo).execute(this.context);
    
    // 页面启动流水线
    AppLaunchPipeliner.getInstance().loadPipelineInfo()
    

    完整的初始化代码示例,请参考示例项目中的 entry/src/main/ets/app/AppAbilityStage.ets 文件。

打开小程序

在打开小程序之前,需要先在 entry/thing-biz-components.json 中导入 @thingsmart/godzillapage 组件。

使用 ThingRouter 来打开小程序和面板:

import { ThingRouter } from "@thingsmart/thingrouter";

// 打开小程序
ThingRouter.getInstance()
  .setParam({
    // 小程序 URL,格式为 godzilla://miniprogramId/pagePath?queryA=a
    'url': 'godzilla://miniprogramId/pagePath'  
  })
  .push("miniApp")

// 打开设备面板
ThingRouter.getInstance()
  .setParam({
    'devId': 'deviceId',  // 设备 ID
    'groupId': 'groupId',  // 设备组 ID(可选)
    /**
     * 透传到属性 initialProps 和 contextProps 上
     */
    // 'propsData': {} as Record<string, Object>  // 自定义参数(可选)
  })
  .push("panel")

引入能力包

为了使小程序拥有更多的可能性,涂鸦针对一些常用的能力做了包装,并且提供了不同的能力包以供您选择。只需要将需要的能力包集成到工程中,您就可以调用相应能力包中的 API,实现业务功能的开发。

能力包名称 描述 依赖包 说明
基础能力包 与腾讯微信小程序大部分 API 类似,包括上传、下载、录音、交互、文件操作等 API @thingsmart/tunibasekit 建议您默认集成基础能力包
业务能力包 提供了开发过程中一些常用涂鸦业务体系下的 API,包括查询多语言、打开控制面板等 @thingsmart/tunibizkit 建议您默认集成业务能力包
家庭管理能力包 包括以概念 家庭 为单位的信息查询等能力,是一些涂鸦业务管理的基础条件。家庭 是场景智能执行的最大单位 @thingsmart/tunihomekit 如需集成家庭管理能力包,您还需要额外集成 智能生活 App SDK 家庭管理
设备控制能力包 对设备控制相关能力的封装,能实现控制、监听、移除设备 @thingsmart/tunidevicekit 如有需求,请参考 智能生活 App SDK 设备控制
地图能力包 对地图相关能力的封装,包括获取位置、地理围栏等 @thingsmart/mapkit 等待开放中
媒体能力包 对视频和相机的封装,您在使用 videocamera 媒体组件时,需要集成媒体能力包 @thingsmart/tunimediakit 等待开放中
IPC 业务能力包 对 IPC 业务能力的封装,包含 IPC YUV 播放器、门铃呼叫、云存储签名等功能,适用于 IPC、门锁和扫地机等业务开发 @thingsmart/tuniipckit 等待开放中

entry/thing-biz-components.json 文件中添加对应的能力包配置:

{
  "components": {
    "@thingsmart/tunibasekit": "3.29.5",
    "@thingsmart/tunibizkit": "1.0.38",
    "@thingsmart/tunihomekit": "1.0.11",
    "@thingsmart/tunidevicekit": "1.1.4",
    "@thingsmart/mapkit": "1.0.17",
    "@thingsmart/tunimediakit": "1.1.1",
    "@thingsmart/tuniipckit": "1.2.30"
  }
}

更多详情,请访问 tuya-bizbundle-harmony-sdk-sample GitHub 项目