Skip to content

API 接口参考文档

作者: 罗耀生 日期: 2026-01-10 版本: v0.3.0


1. 接口概述

1.1 基础信息

项目说明
基础路径/api/v1
协议HTTPS (生产) / HTTP (开发)
数据格式JSON
字符编码UTF-8

1.2 认证方式

JWT Token:

http
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...

API Key:

http
X-API-Key: sk_xxxxxxxxxxxxx
X-API-Secret: xxxxxxxxxxxxxxxx

2. 用户接口

2.1 用户注册

http
POST /api/v1/users/register
Content-Type: application/json

请求体:

json
{
  "username": "user@example.com",
  "password": "password123",
  "email": "user@example.com"
}

响应:

json
{
  "code": 200,
  "message": "success",
  "data": {
    "userId": 1,
    "username": "user@example.com",
    "email": "user@example.com"
  }
}

2.2 用户登录

http
POST /api/v1/users/login
Content-Type: application/json

请求体:

json
{
  "username": "user@example.com",
  "password": "password123"
}

响应:

json
{
  "code": 200,
  "message": "success",
  "data": {
    "user": {
      "userId": 1,
      "username": "user@example.com",
      "email": "user@example.com"
    },
    "token": "eyJhbGci...",
    "refreshToken": "eyJhbGci...",
    "expiresIn": 86400
  }
}

2.3 刷新 Token

http
POST /api/v1/users/refresh-token
Content-Type: application/json

请求体:

json
{
  "refreshToken": "eyJhbGci..."
}

2.4 获取用户信息

http
GET /api/v1/users/me
Authorization: Bearer <token>

2.5 创建 API Key

http
POST /api/v1/users/api-keys
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "description": "用于设备集成"
}

响应:

json
{
  "code": 200,
  "data": {
    "keyId": "ak_xxx",
    "apiKey": "sk_xxx...",
    "description": "用于设备集成"
  }
}

2.6 查询 API Keys

http
GET /api/v1/users/api-keys
Authorization: Bearer <token>

2.7 删除 API Key

http
DELETE /api/v1/users/api-keys/:keyId
Authorization: Bearer <token>

3. 产品接口

3.1 创建产品

http
POST /api/v1/products
Authorization: Bearer <token>
Content-Type: application/json

请求体:

json
{
  "productKey": "SW-SERVO-002",
  "name": "智能开关 v2",
  "description": "ESP32 智能开关设备",
  "category": "switch",
  "controlMode": "toggle",
  "uiTemplate": "servo_switch",
  "iconName": "settings_remote",
  "iconColor": "#FF6B35",
  "manufacturer": "SmartLink",
  "model": "ESP32-Switch-v2"
}

字段说明:

字段类型必填说明
productKeystring产品标识,全局唯一
namestring产品名称
descriptionstring产品描述
categorystring产品类别
controlModestringtoggle/pulse/dimmer/readonly/generic
uiTemplatestringUI 模板名称
iconNamestringMaterial Icons 图标名
iconColorstring图标颜色 (HEX)
manufacturerstring制造商
modelstring硬件型号

响应:

json
{
  "code": 200,
  "data": {
    "productId": 2,
    "productKey": "SW-SERVO-002",
    "name": "智能开关 v2"
  }
}

3.2 产品列表

http
GET /api/v1/products?category=switch&page=1&size=20
Authorization: Bearer <token>

查询参数:

参数类型必填说明
categorystring产品类别
pageint页码,默认 1
sizeint每页数量,默认 20

响应:

json
{
  "code": 200,
  "data": {
    "list": [
      {
        "productId": 1,
        "productKey": "SW-SERVO-001",
        "name": "Smart Servo Switch",
        "description": "ESP32 smart servo switch",
        "category": "switch",
        "controlMode": "toggle",
        "uiTemplate": "servo_switch",
        "iconName": "settings_remote",
        "iconColor": "#FF6B35",
        "manufacturer": "SmartLink",
        "model": "ESP32-Servo",
        "status": 1
      }
    ],
    "total": 10,
    "page": 1,
    "size": 20
  }
}

3.3 产品详情

http
GET /api/v1/products/:productKey
Authorization: Bearer <token>

响应: 同产品列表项

3.4 更新产品

http
PUT /api/v1/products/:productKey
Authorization: Bearer <token>
Content-Type: application/json

请求体: 同创建产品

3.5 删除产品

http
DELETE /api/v1/products/:productKey
Authorization: Bearer <token>

4. 设备接口

4.1 设备激活

http
POST /api/v1/devices/activate
Content-Type: application/json

认证: 无需认证 (设备端调用)

请求体:

json
{
  "productKey": "SW-SERVO-001",
  "deviceSN": "AABBCCDDEEFF",
  "firmwareVersion": "1.0.0",
  "chipModel": "ESP32"
}

响应:

json
{
  "code": 200,
  "message": "success",
  "data": {
    "deviceId": "dev_a1b2c3d4",
    "deviceSecret": "sk_abc123...",
    "mqtt": {
      "server": "192.168.1.100",
      "port": 1883,
      "portTLS": 8883,
      "username": "SW-SERVO-001&dev_a1b2c3d4",
      "password": "tk_xyz789...",
      "clientId": "SW-SERVO-001&dev_a1b2c3d4",
      "keepAlive": 60
    },
    "topics": {
      "propertyPost": "/sys/SW-SERVO-001/dev_a1b2c3d4/thing/event/property/post",
      "propertySet": "/sys/SW-SERVO-001/dev_a1b2c3d4/thing/service/property/set",
      "status": "/sys/SW-SERVO-001/dev_a1b2c3d4/status"
    }
  }
}

错误响应:

json
{
  "code": 400,
  "message": "invalid_product_key",
  "data": null
}

4.2 设备列表

http
GET /api/v1/devices?productKey=SW-SERVO-001&status=1&page=1&size=20
Authorization: Bearer <token>

查询参数:

参数类型必填说明
productKeystring产品标识
statusint设备状态 (0/1/2/3)
pageint页码,默认 1
sizeint每页数量,默认 20

响应:

json
{
  "code": 200,
  "data": {
    "list": [
      {
        "deviceId": "dev_a1b2c3d4",
        "deviceSN": "AABBCCDDEEFF",
        "productKey": "SW-SERVO-001",
        "projectId": "proj_default",
        "name": "客厅开关",
        "status": "online",
        "statusText": "在线",
        "firmwareVersion": "1.0.0",
        "chipModel": "ESP32",
        "lastOnlineAt": "2026-01-10T15:30:00Z",
        "activatedAt": "2026-01-10T10:00:00Z",
        "createdAt": "2026-01-10T10:00:00Z",
        "product": {
          "productId": 1,
          "productKey": "SW-SERVO-001",
          "name": "Smart Servo Switch",
          "uiTemplate": "servo_switch",
          "iconName": "settings_remote",
          "iconColor": "#FF6B35"
        }
      }
    ],
    "total": 1,
    "page": 1,
    "size": 20
  }
}

4.3 设备详情

http
GET /api/v1/devices/:deviceId
Authorization: Bearer <token>

响应: 同设备列表项

4.4 设备状态

http
GET /api/v1/devices/:deviceId/status
Authorization: Bearer <token>

响应:

json
{
  "code": 200,
  "data": {
    "deviceId": "dev_a1b2c3d4",
    "deviceName": "客厅开关",
    "online": true,
    "position": "up",
    "status": "online",
    "lastOnlineAt": "2026-01-10T15:30:00Z"
  }
}

4.5 设备控制

http
POST /api/v1/devices/:deviceId/control
Authorization: Bearer <token>
Content-Type: application/json

新协议请求:

json
{
  "action": "toggle",
  "position": "up"
}

json
{
  "action": "pulse",
  "duration": 500
}

旧协议请求 (兼容):

json
{
  "switch": true,
  "angle": 90
}

参数说明:

参数类型说明
actionstringtoggle/pulse/trigger
positionstringup/middle/down (toggle 模式)
durationint延迟时间毫秒 (pulse 模式)
switchbool开关状态 (旧协议)
angleint角度 0-180 (旧协议)

响应:

json
{
  "code": 200,
  "message": "success",
  "data": {
    "message": "command_sent"
  }
}

4.6 删除设备

http
DELETE /api/v1/devices/:deviceId
Authorization: Bearer <token>

5. MQTT 主题规范

5.1 主题格式

/sys/{ProductKey}/{DeviceID}/{action}

5.2 设备上报

主题: /sys/{ProductKey}/{DeviceID}/thing/event/property/post

方向: 设备 → 服务端

消息:

json
{
  "method": "thing.event.property.post",
  "id": "123456",
  "params": {
    "position": "up",
    "angle": 90,
    "wifi_rssi": -45
  },
  "time": 1704886800000
}

5.3 设备控制

主题: /sys/{ProductKey}/{DeviceID}/thing/service/property/set

方向: 服务端 → 设备

消息:

json
{
  "method": "thing.service.property.set",
  "id": "789012",
  "params": {
    "action": "toggle",
    "position": "up"
  },
  "time": 1704886800000
}

5.4 设备状态

主题: /sys/{ProductKey}/{DeviceID}/status

方向: 双向

在线消息:

json
{
  "status": "online",
  "time": 1704886800000
}

离线消息:

json
{
  "status": "offline",
  "time": 1704886800000
}

6. 错误码

6.1 错误响应格式

json
{
  "code": 400,
  "message": "error_message",
  "data": null
}

6.2 常见错误码

错误码说明
200成功
400请求参数错误
401未认证
403无权限
404资源不存在
500服务器错误

6.3 业务错误码

错误信息说明
invalid_product_key无效的产品标识
product_disabled产品已禁用
device_not_found设备不存在
device_not_activated设备未激活
device_disabled设备已禁用
invalid_password密码错误
invalid_client_idClientID 不匹配
no_control_params无控制参数

7. WebSocket 接口

7.1 连接

javascript
const ws = new WebSocket('ws://localhost:8083/mqtt');

// 认证
ws.send(JSON.stringify({
  action: 'auth',
  username: 'SW-SERVO-001&dev_a1b2c3d4',
  password: 'tk_xyz789...'
}));

7.2 订阅主题

javascript
ws.send(JSON.stringify({
  action: 'subscribe',
  topic: '/sys/SW-SERVO-001/dev_a1b2c3d4/thing/event/property/post'
}));

7.3 接收消息

javascript
ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  console.log(message.topic, message.payload);
};

修改历史

日期版本修改内容作者
2026-01-10v0.3.0初始版本罗耀生