Files
im-uniapp/util/revokeMessage.js
2025-07-14 13:01:45 +08:00

82 lines
2.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import IMSDK, { MessageStatus } from "openim-uniapp-polyfill";
/**
* 撤回消息
* @param {string} conversationID - 会话ID
* @param {string} clientMsgID - 消息客户端ID
* @returns {Promise} 撤回结果
*/
export const revokeMessage = async ({conversationID, clientMsgID, revokerName, revokerId}) => {
try {
const result = await IMSDK.asyncApi(
IMSDK.IMMethods.RevokeMessage,
IMSDK.uuid(),
{
conversationID,
clientMsgID,
}
);
return result;
} catch (error) {
console.error("撤回消息失败:", error);
throw error;
}
};
/**
* 检查消息是否可以撤回
* @param {Object} message - 消息对象
* @param {string} currentUserID - 当前用户ID
* @returns {boolean} 是否可以撤回
*/
export const canRevokeMessage = (message, currentUserID) => {
console.log('检查消息是否可以撤回:', {
messageSendID: message.sendID,
currentUserID: currentUserID,
messageStatus: message.status,
contentType: message.contentType,
sendTime: message.sendTime,
now: Date.now()
});
// 只能撤回自己发送的消息
if (message.sendID !== currentUserID) {
console.log('不是自己发送的消息');
return false;
}
// 检查消息状态是否为成功
if (message.status !== MessageStatus.Succeed) {
console.log('消息状态不是成功:', message.status);
return false;
}
// 检查消息类型是否支持撤回
const revokableTypes = [
101, // 文本消息
102, // 图片消息
103, // 语音消息
104, // 视频消息
105, // 文件消息
106, // @消息
110, // 自定义消息
];
if (!revokableTypes.includes(message.contentType)) {
console.log('不支持撤回的消息类型:', message.contentType);
return false;
}
// 检查消息发送时间是否在2分钟内可配置
const now = Date.now();
const messageTime = message.sendTime;
const timeLimit = 2 * 60 * 1000; // 2分钟
if (now - messageTime > timeLimit) {
console.log('消息发送时间超过2分钟');
return false;
}
console.log('消息可以撤回');
return true;
};