161 lines
5.1 KiB
JavaScript
161 lines
5.1 KiB
JavaScript
|
|
import { EaseSDK, EMClient } from "../index";
|
||
|
|
import { useMessageStore } from "@/stores/message";
|
||
|
|
import { useConversationStore } from "@/stores/conversation";
|
||
|
|
import { getEMKey } from "@/EaseIM/utils";
|
||
|
|
import { sendMessageApi } from "@/utils/Huanxin.js";
|
||
|
|
const emMessages = () => {
|
||
|
|
const messageStore = useMessageStore();
|
||
|
|
const conversationStore = useConversationStore();
|
||
|
|
|
||
|
|
const reportMessages = (params) => {
|
||
|
|
const { reportType, reportReason, messageId } = params;
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
EMClient.reportMessage({
|
||
|
|
reportType: reportType, // 举报类型
|
||
|
|
reportReason: reportReason, // 举报原因。
|
||
|
|
messageId: messageId, // 上报消息id
|
||
|
|
})
|
||
|
|
.then((res) => {
|
||
|
|
resolve(res);
|
||
|
|
})
|
||
|
|
.catch((error) => {
|
||
|
|
reject(error);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const fetchHistoryMessagesFromServer = (params) => {
|
||
|
|
console.log(">>>>>开始获取历史消息", params);
|
||
|
|
const { targetId, cursor, chatType } = params;
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
let options = {
|
||
|
|
// 对方的用户 ID 或者群组 ID 或聊天室 ID。
|
||
|
|
targetId: targetId,
|
||
|
|
// 每页期望获取的消息条数。取值范围为 [1,50],默认值为 20。
|
||
|
|
pageSize: 20,
|
||
|
|
// 查询的起始消息 ID。若该参数设置为 `-1`、`null` 或空字符串,从最新消息开始。
|
||
|
|
cursor: cursor || -1,
|
||
|
|
// 会话类型:(默认) `singleChat`:单聊;`groupChat`:群聊。
|
||
|
|
chatType: chatType,
|
||
|
|
// 消息搜索方向:(默认)`up`:按服务器收到消息的时间的逆序获取;`down`:按服务器收到消息的时间的正序获取。
|
||
|
|
searchDirection: "up",
|
||
|
|
};
|
||
|
|
console.log(">>>>>获取历史消息参数", options);
|
||
|
|
EMClient.getHistoryMessages(options)
|
||
|
|
.then((res) => {
|
||
|
|
console.log(">>>>>获取历史消息成功", res);
|
||
|
|
resolve(res);
|
||
|
|
})
|
||
|
|
.catch((e) => {
|
||
|
|
console.log(">>>>>获取历史消息失败", e);
|
||
|
|
// 获取失败。
|
||
|
|
reject(e);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
};
|
||
|
|
const sendDisplayMessages = (messageBody) => {
|
||
|
|
messageBody.from = EMClient.user;
|
||
|
|
const key = getEMKey(
|
||
|
|
EMClient.user,
|
||
|
|
messageBody.from,
|
||
|
|
messageBody.to,
|
||
|
|
messageBody.chatType
|
||
|
|
);
|
||
|
|
return new Promise((resolve, reject) => {
|
||
|
|
const msg = EaseSDK.message.create(messageBody);
|
||
|
|
console.log(msg,messageBody);
|
||
|
|
if (messageBody.type == "video") {
|
||
|
|
msg.url = messageBody.url;
|
||
|
|
msg.thumb_uuid = messageBody.thumbId;
|
||
|
|
}
|
||
|
|
if(messageBody.type == "audio") {
|
||
|
|
msg.url = messageBody.body.url;
|
||
|
|
msg.length = messageBody.body.length;
|
||
|
|
}
|
||
|
|
console.log(">>>>构建的消息msg", msg);
|
||
|
|
// EMClient.send(msg)
|
||
|
|
// .then((res) => {
|
||
|
|
sendMessages(msg);
|
||
|
|
// console.log(">>>>>发送成功1111111", res);
|
||
|
|
resolve(msg);
|
||
|
|
// msg.id = res.serverMsgId;
|
||
|
|
messageStore.updateMessageCollection(key, msg);
|
||
|
|
conversationStore.updateConversationLastMessage(key, msg);
|
||
|
|
// })
|
||
|
|
// .catch((err) => {
|
||
|
|
// reject(err);
|
||
|
|
// console.log(">>>>>发送失败", err);
|
||
|
|
// });
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
// 接口发送消息
|
||
|
|
const sendMessages = (msg) => {
|
||
|
|
console.log(">>>>接口发送消息", msg);
|
||
|
|
// IMAGE: 'img',
|
||
|
|
// TEXT: 'txt',
|
||
|
|
// LOCATION: 'location',
|
||
|
|
// VIDEO: 'video',
|
||
|
|
// AUDIO: 'audio',
|
||
|
|
// EMOJI: 'emoji',
|
||
|
|
// FILE: 'file',
|
||
|
|
// CUSTOM: 'custom',
|
||
|
|
let send_type = "";
|
||
|
|
let message = "";
|
||
|
|
let uuid = "";
|
||
|
|
let url = "";
|
||
|
|
let thumb_uuid = "";
|
||
|
|
let length = '';
|
||
|
|
switch (msg.type) {
|
||
|
|
case "txt": // 文本消息
|
||
|
|
send_type = "text";
|
||
|
|
message = msg.msg;
|
||
|
|
break;
|
||
|
|
case "img": // 图片消息
|
||
|
|
send_type = "image";
|
||
|
|
url = msg.url;
|
||
|
|
uuid = msg.url.substring(msg.url.lastIndexOf("/") + 1);
|
||
|
|
break;
|
||
|
|
case "audio": // 语音消息
|
||
|
|
send_type = "audio";
|
||
|
|
url = msg.body.url.substring(0, msg.body.url.lastIndexOf("/"));
|
||
|
|
uuid = msg.body.url.substring(msg.body.url.lastIndexOf("/") + 1);
|
||
|
|
length = msg.body.length;
|
||
|
|
break;
|
||
|
|
case "video": // 视频消息
|
||
|
|
send_type = "video";
|
||
|
|
url = msg.url.substring(0, msg.url.lastIndexOf("/"));
|
||
|
|
uuid = msg.url.substring(msg.url.lastIndexOf("/") + 1);
|
||
|
|
thumb_uuid = msg.thumb_uuid;
|
||
|
|
break;
|
||
|
|
case "file": // 文件消息
|
||
|
|
send_type = "file";
|
||
|
|
break;
|
||
|
|
case "emoji": // 表情消息
|
||
|
|
send_type = "text";
|
||
|
|
message = msg.msg;
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
let params = {
|
||
|
|
to_user_id: conversationStore.userId,
|
||
|
|
send_type: send_type,
|
||
|
|
message: message,
|
||
|
|
uuid: uuid,
|
||
|
|
url: url,
|
||
|
|
thumb_uuid: thumb_uuid,
|
||
|
|
length: length,
|
||
|
|
};
|
||
|
|
console.log(">>>>>开始发送消息1231231", params);
|
||
|
|
sendMessageApi(params)
|
||
|
|
.then((res) => {
|
||
|
|
console.log(">>>>>发送消息成功", res);
|
||
|
|
})
|
||
|
|
.catch((e) => {});
|
||
|
|
};
|
||
|
|
return {
|
||
|
|
reportMessages,
|
||
|
|
fetchHistoryMessagesFromServer,
|
||
|
|
sendDisplayMessages,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
export default emMessages;
|