消息相关功能

更新时间:2025-12-16 06:08:49下载pdf

概述

MessageLib提供了完整的消息管理功能,包括消息查询、删除、推送设置等。

快速开始

导入模块

import { TSmartMessage } from '@thingsmart/messagelib';
import { MessageBean, MessageListBean, MessageHasNew, MessageType } from '@thingsmart/messagelib';

获取实例

const messageInstance = TSmartMessage.getInstance();

API接口说明

1. 查询消息列表

1.1 查询所有消息列表

接口说明

async getMessageList(): Promise<TSmartAtopResponse<Array<MessageBean>>>

参数说明

无参数

返回值

Promise<TSmartAtopResponse<Array<MessageBean>>>

数据模型 - MessageBean

字段 类型 描述
dateTime string 日期和时间
icon string 消息图标 URL
msgTypeContent string 消息类型名称
msgContent string 消息内容
msgType number 消息类型
msgSrcId string 设备 ID,只有告警消息才有该字段
id string 消息 ID

示例代码

try {
  const response = await TSmartMessage.getInstance().getMessageList();
  if (response.success) {
    const messageList = response.result;
    console.log('消息列表:', messageList);
  } else {
    console.error('获取消息列表失败:', response.errorMsg);
  }
} catch (error) {
  console.error('网络错误:', error);
}

1.2 分页查询消息列表

接口说明

async getMessageListByPage(requestParams: MessageListParams): Promise<TSmartAtopResponse<MessageListBean>>

参数说明

参数 类型 说明
requestParams MessageListParams 分页查询参数

MessageListParams 参数结构

字段 类型 描述
offset number 已请求到的消息总数
limit number 每页请求数据数

返回值

Promise<TSmartAtopResponse<MessageListBean>>

示例代码

const params: MessageListParams = {
  offset: 0,
  limit: 20
};

try {
  const response = await TSmartMessage.getInstance().getMessageListByPage(params);
  if (response.success) {
    const messageListBean = response.result;
    console.log('分页消息列表:', messageListBean);
  }
} catch (error) {
  console.error('获取分页消息列表失败:', error);
}

2. 根据消息类型分页查询消息(推荐)

接口说明

async getMessageListByMsgType(requestParams: MsgListByMsgTypeParams): Promise<TSmartAtopResponse<MessageListBean>>

参数说明

参数 类型 说明
requestParams MsgListByMsgTypeParams 按类型查询参数

MsgListByMsgTypeParams 参数结构

字段 类型 描述
offset number 已请求到的消息总数
limit number 每页请求数据数
msgType MessageType 消息类型枚举值

MessageType 枚举值

说明
MessageType.ALARM 告警消息 (1)
MessageType.FAMILY 家庭消息 (2)
MessageType.NOTIFICATION 通知消息 (3)

示例代码

const params: MsgListByMsgTypeParams = {
  offset: 0,
  limit: 20,
  msgType: MessageType.ALARM
};

try {
  const response = await TSmartMessage.getInstance().getMessageListByMsgType(params);
  if (response.success) {
    const messageListBean = response.result;
    console.log('告警消息列表:', messageListBean);
  }
} catch (error) {
  console.error('获取告警消息失败:', error);
}

3. 根据消息源ID分页查询消息

接口说明

async getMessageListByMsgSrcId(requestParams: MsgListBySrcIdParams): Promise<TSmartAtopResponse<MessageListBean>>

参数说明

参数 类型 说明
requestParams MsgListBySrcIdParams 按消息源查询参数

MsgListBySrcIdParams 参数结构

字段 类型 描述
offset number 偏移从第N条数据开始查询
limit number 每页的消息数量
msgType MessageType 消息类型,目前只支持告警类型
msgSrcId string 消息源ID
encryptImage boolean 是否加密图片

示例代码

const params: MsgListBySrcIdParams = {
  offset: 0,
  limit: 20,
  msgType: MessageType.ALARM,
  msgSrcId: "your_device_id",
  encryptImage: false
};

try {
  const response = await TSmartMessage.getInstance().getMessageListByMsgSrcId(params);
  if (response.success) {
    const messageListBean = response.result;
    console.log('设备告警消息:', messageListBean);
  }
} catch (error) {
  console.error('获取设备告警消息失败:', error);
}

4. 删除消息

4.1 批量删除消息

接口说明

async deleteMessages(msgIdList: Array<string>): Promise<TSmartAtopResponse<boolean>>

参数说明

参数 类型 说明
msgIdList Array 要删除的消息ID列表

示例代码

const messageIds = ["msg_id_1", "msg_id_2", "msg_id_3"];

try {
  const response = await TSmartMessage.getInstance().deleteMessages(messageIds);
  if (response.success && response.result) {
    console.log('删除消息成功');
  } else {
    console.error('删除消息失败:', response.errorMsg);
  }
} catch (error) {
  console.error('删除消息网络错误:', error);
}

4.2 批量删除特定类型的消息

接口说明

async deleteMessageByType(messageType: MessageType, msgIdList: Array<string> | null, msgSrcIdList: Array<string> | null): Promise<TSmartAtopResponse<boolean>>

参数说明

参数 类型 说明
messageType MessageType 消息类型
msgIdList Array | null 要删除的消息ID组
msgSrcIdList Array | null 消息源ID组,传null或空表示不删除告警消息

示例代码

// 删除告警消息列表中的消息
const srcIds = ["src_id_1", "src_id_2"];
try {
  const response = await TSmartMessage.getInstance().deleteMessageByType(
    MessageType.ALARM, 
    null, 
    srcIds
  );
  if (response.success && response.result) {
    console.log('删除告警消息成功');
  }
} catch (error) {
  console.error('删除告警消息失败:', error);
}

// 删除告警消息详情中的消息
const msgIds = ["msg_id_1", "msg_id_2"];
try {
  const response = await TSmartMessage.getInstance().deleteMessageByType(
    MessageType.ALARM, 
    msgIds, 
    null
  );
  if (response.success && response.result) {
    console.log('删除告警消息详情成功');
  }
} catch (error) {
  console.error('删除告警消息详情失败:', error);
}

5. 检查新消息

接口说明

async requestMessageNew(): Promise<TSmartAtopResponse<MessageHasNew>>

参数说明

无参数

返回值

Promise<TSmartAtopResponse<MessageHasNew>>

MessageHasNew 数据模型

字段 类型 描述
alarm boolean 是否有新的告警消息
family boolean 是否有新的家庭消息
notification boolean 是否有新的通知消息

示例代码

try {
  const response = await TSmartMessage.getInstance().requestMessageNew();
  if (response.success) {
    const hasNew = response.result;
    if (hasNew.alarm) {
      console.log('有新的告警消息');
    }
    if (hasNew.family) {
      console.log('有新的家庭消息');
    }
    if (hasNew.notification) {
      console.log('有新的通知消息');
    }
  }
} catch (error) {
  console.error('检查新消息失败:', error);
}

6. 消息推送管理

6.1 获取推送开关状态

接口说明

async getPushSwitchStatusByType(messageType: number): Promise<TSmartAtopResponse<boolean>>

参数说明

参数 类型 说明
messageType number 消息类型

示例代码

try {
  const response = await TSmartMessage.getInstance().getPushSwitchStatusByType(1);
  if (response.success) {
    const isEnabled = response.result;
    console.log('推送开关状态:', isEnabled ? '开启' : '关闭');
  }
} catch (error) {
  console.error('获取推送开关状态失败:', error);
}

6.2 设置推送开关状态

接口说明

async setPushSwitchStatusByType(messageType: number, isClose: boolean): Promise<TSmartAtopResponse<boolean>>

参数说明

参数 类型 说明
messageType number 消息类型
isClose boolean 是否关闭推送

示例代码

try {
  const response = await TSmartMessage.getInstance().setPushSwitchStatusByType(1, false);
  if (response.success && response.result) {
    console.log('推送开关设置成功');
  }
} catch (error) {
  console.error('设置推送开关失败:', error);
}

6.3 获取支持推送的设备列表

接口说明

async getSupportPushDeviceList(homeId: number): Promise<TSmartAtopResponse<Array<PushDeviceBean>>>

参数说明

参数 类型 说明
homeId number 家庭ID

示例代码

try {
  const response = await TSmartMessage.getInstance().getSupportPushDeviceList(12345);
  if (response.success) {
    const deviceList = response.result;
    console.log('支持推送的设备:', deviceList);
  }
} catch (error) {
  console.error('获取推送设备列表失败:', error);
}

7. 高级功能

7.1 消息批量已读

接口说明

async readMessageList(messageType: MessageType, msgIdList: Array<string>): Promise<TSmartAtopResponse<boolean>>

参数说明

参数 类型 说明
messageType MessageType 消息类型,目前只支持告警类型
msgIdList Array 消息ID列表

示例代码

const msgIds = ["msg_id_1", "msg_id_2"];
try {
  const response = await TSmartMessage.getInstance().readMessageList(MessageType.ALARM, msgIds);
  if (response.success && response.result) {
    console.log('批量标记已读成功');
  }
} catch (error) {
  console.error('批量标记已读失败:', error);
}

7.2 消息全部已读

接口说明

async readAllMessage(messageType: MessageType): Promise<TSmartAtopResponse<boolean>>

参数说明

参数 类型 说明
messageType MessageType 消息类型,目前只支持告警类型

示例代码

try {
  const response = await TSmartMessage.getInstance().readAllMessage(MessageType.ALARM);
  if (response.success && response.result) {
    console.log('全部标记已读成功');
  }
} catch (error) {
  console.error('全部标记已读失败:', error);
}

7.3 获取消息最大时间

接口说明

async getMessageMaxTime(): Promise<TSmartAtopResponse<number>>

参数说明

无参数

返回值

Promise<TSmartAtopResponse<number>> - 返回最新消息的时间戳

示例代码

try {
  const response = await TSmartMessage.getInstance().getMessageMaxTime();
  if (response.success) {
    const maxTime = response.result;
    console.log('最新消息时间:', new Date(maxTime));
  }
} catch (error) {
  console.error('获取最新消息时间失败:', error);
}

错误处理

所有API都返回TSmartAtopResponse格式的响应,包含以下字段:

interface TSmartAtopResponse<T> {
  success: boolean;      // 请求是否成功
  result: T;            // 响应数据
  errorCode: string;    // 错误码
  errorMsg: string;     // 错误信息
}

通用错误处理示例

try {
  const response = await TSmartMessage.getInstance().getMessageList();
  
  if (response.success) {
    // 请求成功,处理数据
    const data = response.result;
    console.log('成功获取数据:', data);
  } else {
    // 请求失败,处理错误
    console.error('请求失败:', response.errorCode, response.errorMsg);
    
    // 根据错误码进行具体处理
    switch (response.errorCode) {
      case 'NETWORK_ERROR':
        console.log('网络错误,请检查网络连接');
        break;
      case 'AUTH_FAILED':
        console.log('认证失败,请重新登录');
        break;
      default:
        console.log('未知错误:', response.errorMsg);
        break;
    }
  }
} catch (error) {
  // 网络异常或其他异常
  console.error('异常错误:', error);
}

最佳实践

1. 错误处理

始终检查响应的success字段,并根据错误码进行相应处理。

2. 分页查询

建议使用getMessageListByMsgType替代已废弃的方法,并合理设置分页参数:

const params = {
  offset: 0,
  limit: 20,  // 建议单页不超过50条
  msgType: MessageType.ALARM
};

3. 资源管理

及时处理异步操作,避免内存泄漏:

let messageRequest: Promise<any> | null = null;

async function loadMessages() {
  try {
    messageRequest = TSmartMessage.getInstance().getMessageList();
    const response = await messageRequest;
    // 处理响应
  } finally {
    messageRequest = null;
  }
}

4. 类型安全

充分利用TypeScript的类型检查:

// 推荐:使用明确的类型
const params: MsgListByMsgTypeParams = {
  offset: 0,
  limit: 20,
  msgType: MessageType.ALARM
};

// 避免:使用any类型
const params: any = { /* ... */ };