# 视频消息

方法说明中并非所有的方法都会写在文档中,具体请前往相关文件查看代码逻辑。

# 文件位置

TIP

文件位于 src\views\_uc-chat\msg-chat\content-send\send-video\index.js sendVideo

# 使用方法

代码可以参考 src\views\_uc-chat\msg-chat\content-send\contentSend.vue

// 引入视频上传模块
import { sendVideo } from "@/views/_uc-chat/msg-chat/content-send/send-video/index.js";

/**
 *
 * @param {*} file 文件
 * @param {*} param1 传 { currentUserInfo, currentChatList }
 * @param {*} time 延时上传时间,一下子加很多文件,没有延时的话会有性能问题,比如选项卡切换不动
 */
sendVideo(file, { currentUserInfo, currentChatList }, (time = 0), callback);

# 依赖文件

  • import { UCMessage } from "@/control/message/messageSender.js";
  • import { UC_File } from "@/control/uc-file/ucFile.js";

# 代码分析

获取视频信息

// 如果是XP直接按文件的方式发送视频
if (ZX_CLIENT.config.isXP) {
  callback && callback("error");
  return;
}

// 获取视频的base64缩略图
let base64 = await getVideoBase64(file.path, {
  maxHeight: 300,
  maxWidth: 150,
});

// 如果无法获取就返回错误,这时候会通过文件形式发送视频文件
if (base64 == undefined) {
  callback && callback("error");
  return;
}

// 获取视频信息
let videoInfo = await getVideoInfo(file.path);

// 如果拿不到视频信息,以文件形式发送视频文件
if (videoInfo == undefined) {
  callback && callback("error");
  return;
}

// 分析获取到的base64数据
let formatBase64 = base64.split("base64,")[1];

// 拿到的是个诡异的base64数据就跳过
if (formatBase64 == undefined) {
  callback && callback("error");
  return;
}

// 格式化视频时长
let formatDuration = videoInfo.duration * 1000;

实例化消息体

let Content = {
  name: file.name, // 文件名
  size: file.size, // 文件大小
  type: file.type, // 文件类型
  path: file.path, // 文件路径
  fileUrl: file.path, // 远程服务器上的文件路径
  timeDuration: formatDuration, // 时间
  base64: formatBase64, // base64缩略图
  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 videoMessage = new UCMessage("VideoMessage", Content);

向页面中添加消息体

videoMessage.content.localPath = file.path;
videoMessage.preAppend({
  conversationType: currentChatList.conversationType,
  targetId: currentChatList.targetId,
  targetName: currentChatList.conversationTitle,
});

实例化文件上传并上传

let uploadFile = new UC_File(videoMessage);
uploadFile.upload(file, (status, data) => {
  console.log(status, data);
  if (status == "success") {
    console.log("上传完成后返回的数据", data);
    let fileInfo = JSON.parse(data);
    console.log("格式化上传完成后返回的数据", fileInfo, videoMessage);
    const fileURL = fileInfo.atts[0].fileUrl;
    videoMessage.content.fileUrl = fileURL;

    // 解决添加消息体后已经向数据库写入了,但是发送后不更新fileURL的问题
    ZX_CLIENT.ImMsg.updateChatMessage(
      {
        messageId: videoMessage.messageId,
      },
      {
        content: videoMessage.content,
      }
    );

    // 向服务器上传完成后发送视频消息
    videoMessage.sent({
      conversationType: currentChatList.conversationType,
      targetId: currentChatList.targetId,
      needAppend: false,
      targetName: currentChatList.conversationTitle,
    });
  }
});