[English](./README.md) | 简体中文

# @tuya-miniapp/cloud-api

[![latest](https://img.shields.io/npm/v/@tuya-miniapp/cloud-api/latest.svg)](https://www.npmjs.com/package/@tuya-miniapp/cloud-api) [![download](https://img.shields.io/npm/dt/@tuya-miniapp/cloud-api.svg)](https://www.npmjs.com/package/@tuya-miniapp/cloud-api)

> 小程序云能力接口通用包装库

## 安装

```sh
$ npm install @tuya-miniapp/cloud-api
# 或者
$ yarn add @tuya-miniapp/cloud-api
```

## 原理说明

该包通过统一主入口暴露各类云能力 API，并将底层的 `ty.apiRequestByHighway` 与 `ty.apiRequestByAtop` 请求封装为 Promise 风格方法。在调用具体接口前，请先确保已在小程序开发者平台完成对应云能力授权。

## 开发

```sh
# 实时编译Demo代码
yarn start:tuya
```

## 使用

### 基础用法

这个示例演示了如何从包主入口按需导入 API，并分别调用一个统计类接口和一个天气类接口。重点是按照每个方法定义的请求对象传参，并通过 `await` 获取异步返回结果。

```tsx
// 从主入口按需导入
import { getStatisticsConfig, getWeatherDailyHistory } from '@tuya-miniapp/cloud-api';

// 使用 API
const config = await getStatisticsConfig({ devId: 'vdevo166789063330437' });
const weather = await getWeatherDailyHistory({
  devId: '6cbed8370dd694e247uk9u',
  weatherFieldNames: 'pm25,temp',
  beginTime: 1698028537000,
  endTime: 1698287737698,
  ip: '',
});
```

### 充电桩

提供充电记录分页查询、充电电量统计和记录邮件导出。设备需开通对应能力，小程序需完成云能力授权配置。

> 测试前提：虚拟设备不能用于这些接口测试。请使用已完成对应高级能力打标并已开通该能力的真实设备，同时完成小程序云能力授权。

| API                             | 文档                                                                    |
| ------------------------------- | ----------------------------------------------------------------------- |
| `getChargePileRecordList`       | [充电记录](./docs/chargePile/getChargePileRecordList.zh.md)             |
| `getChargePileStatistics`       | [充电统计](./docs/chargePile/getChargePileStatistics.zh.md)             |
| `getChargePileStatisticsByPort` | [按枪充电统计](./docs/chargePile/getChargePileStatisticsByPort.zh.md)   |
| `exportChargePileRecords`       | [邮件导出](./docs/chargePile/exportChargePileRecords.zh.md)             |
| `getChargePileRecordListByPort` | [按端口查询记录](./docs/chargePile/getChargePileRecordListByPort.zh.md) |
| `exportChargePileRecordsByPort` | [按端口邮件导出](./docs/chargePile/exportChargePileRecordsByPort.zh.md) |

双枪记录和导出使用 `ByPort` 接口，省略端口号可操作全部记录。分枪统计使用 `getChargePileStatisticsByPort`，枪号 1/2 必填；`getChargePileStatistics`仍为设备级查询。

```typescript
import { getChargePileRecordList } from '@tuya-miniapp/cloud-api';
// 也支持从 '@tuya-miniapp/cloud-api/chargePile' 导入。

const result = await getChargePileRecordList({
  devId: 'your_device_id',
  pageNum: 1,
  pageSize: 20,
});
const records = result?.data ?? [];
```

### API 说明

#### `getStatisticsConfig`

获取设备已开通的智能计量统计配置。

```ts
getStatisticsConfig(data: { devId: string }): Promise<IStatisticsConfig[]>
```

| 参数    | 类型     | 必填 | 说明                                          |
| :------ | :------- | :--- | :-------------------------------------------- |
| `devId` | `string` | 是   | 设备 ID，用于查询该设备已开通的统计 DP 配置。 |

#### `getWeatherDailyHistory`

获取设备在指定时间范围内的逐日历史天气数据。

```ts
getWeatherDailyHistory(data: {
  devId: string;
  weatherFieldNames: string;
  beginTime: number;
  endTime: number;
  ip?: string;
}): Promise<Record<string, Weather>>
```

| 参数                | 类型     | 必填 | 说明                                                   |
| :------------------ | :------- | :--- | :----------------------------------------------------- |
| `devId`             | `string` | 是   | 设备 ID，用于确定天气查询目标。                        |
| `weatherFieldNames` | `string` | 是   | 天气字段名，多个字段用英文逗号分隔，例如 `pm25,temp`。 |
| `beginTime`         | `number` | 是   | 历史天气查询起始时间戳，单位为毫秒。                   |
| `endTime`           | `number` | 是   | 历史天气查询结束时间戳，单位为毫秒。                   |
| `ip`                | `string` | 否   | 可选 IP 地址参数，用于天气请求。                       |