# 文件消息
方法说明中并非所有的方法都会写在文档中,具体请前往相关文件查看代码逻辑。
# 文件位置
TIP
文件位于 src\views\_uc-chat\msg-chat\content-send\send-file\index.js
sendGif
# 使用方法
代码可以参考 src\views\_uc-chat\msg-chat\content-send\contentSend.vue
// 引入文件上传模块
import { sendFile } from "@/views/_uc-chat/msg-chat/content-send/send-file/index.js";
/**
*
* @param {*} file 文件
* @param {*} param1 传 { currentUserInfo, currentChatList }
* @param {*} time 延时上传时间,一下子加很多文件,没有延时的话会有性能问题,比如选项卡切换不动
* @param {*} callback 回调
*/
sendFile(file, { currentUserInfo, currentChatList }, (time = 0), callback);
# 依赖文件
import { UCMessage } from "@/control/message/messageSender.js";
import { UC_File } from "@/control/uc-file/ucFile.js";
# 代码分析
实例化消息体
let Content = {
fileUrl: file.path, // 文件路径,服务器上的文件ID
name: file.name, // 文件名
size: file.size, // 文件大小
type: file.type, // 文件类型
path: file.path, // 文件路径
state: FileStatus.PREUPLOAD,
extra: JSON.stringify({
userId: currentUserInfo.memberid,
userName: currentUserInfo.name,
toId: currentChatList.targetId,
toName: currentChatList.conversationTitle,
from_c: "PC",
pcMAC: ZX_CLIENT.getMAC(),
}),
user: {
id: currentUserInfo.memberid,
name: currentUserInfo.name,
portrait: "",
},
};
const fileMessage = new UCMessage("FileMessage", Content);
向页面中添加消息体
fileMessage.content.localPath = file.path;
fileMessage.preAppend({
conversationType: currentChatList.conversationType,
targetId: currentChatList.targetId,
targetName: currentChatList.conversationTitle,
});
实例化文件上传并上传
let uploadFile = new UC_File(fileMessage);
uploadFile.upload(file, (status, data) => {
console.log(status, data);
if (status == "success") {
console.log("上传完成后返回的数据", data);
let fileInfo = JSON.parse(data);
console.log("格式化上传完成后返回的数据", fileInfo, fileMessage);
const fileURL = fileInfo.atts[0].fileUrl;
fileMessage.content.fileUrl = fileURL;
// 解决添加消息体后已经向数据库写入了,但是发送后不更新fileURL的问题
ZX_CLIENT.ImMsg.updateChatMessage(
{
messageId: fileMessage.messageId,
},
{
content: fileMessage.content,
}
);
// 上传完成后发送文件消息
fileMessage.sent({
conversationType: currentChatList.conversationType,
targetId: currentChatList.targetId,
needAppend: false,
targetName: currentChatList.conversationTitle,
});
// 返回文件上传成功
callback && callback("finish");
}
});