# 文件夹消息

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

# 文件位置

TIP

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

# 使用方法

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

// 引入文件上传模块
import { sendFolder } from "@/views/_uc-chat/msg-chat/content-send/send-folder/index.js";

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

# 依赖文件

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

# 代码分析

使用ZX_CLIENT.getFolderInfo方法获取文件夹信息

ZX_CLIENT.getFolderInfo(
  folderPath,
  { maxNum: 666, maxsize: 1048576000 },
  (params, data) => {
    if (params == "start") {
      console.log("开始扫描文件夹", addFolderData);
    }

    if (params == "error") {
      console.log("错误");
    }

    if (params == "toomore") {
      console.log("文件过多");
    }

    if (params == "toobig") {
      console.log("文件过大");
    }

    if (params == "done") {
      console.log("扫描完成", data);
    }
  }
);

不同情况下不同的处理逻辑

// 开始扫描文件夹
if (params == "start") {
  // 向页面添加消息
  let addFolderData = {
    name: data,
    size: 0,
    dirName: data,
    dirPath: folderPath,
  };
  console.log("开始扫描文件夹", addFolderData);

  // 实例化消息体
  let Content = {
    fileUrl: addFolderData.dirPath,
    name: addFolderData.dirName,
    size: "",
    type: "Folder",
    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: "",
    },
  };

  // 像页面添加文件夹消息
  folderMessage = new UCMessage("FolderMessage", Content);
  folderMessage.preAppend({
    conversationType: currentChatList.conversationType,
    targetId: currentChatList.targetId,
    targetName: currentChatList.conversationTitle,
  });

  // 实例化文件对象
  let fileObject = {
    name: addFolderData.name,
    path: addFolderData.dirPath,
    size: addFolderData.size,
    type: "",
  };

  let newFileObject = Object.assign(
    {
      messageId: folderMessage.defaultMessageId,
      fileStatus: FileStatus.PREUPLOAD,
      fileProgress: 0,
      speed: "",
      time: Number(Date.now() - store.getters.getIMTimeDiff),
    },
    fileObject
  );

  store.dispatch("UcFile_addNewItem", {
    messageId: folderMessage.defaultMessageId,
    fileObject: newFileObject,
  });

  console.log("添加文件夹消息体", folderMessage, newFileObject);
}

if (params == "error") {
  console.log("错误");
  store.dispatch("UcFile_setFileStatus", {
    messageId: folderMessage.defaultMessageId,
    fileStatus: FileStatus.ZIPPING_FAILED,
  });
  return;
}

if (params == "toomore") {
  console.log("文件过多");
  store.dispatch("UcFile_setFileStatus", {
    messageId: folderMessage.defaultMessageId,
    fileStatus: FileStatus.CHECKING_TOOMORE,
  });
  return;
}

if (params == "toobig") {
  console.log("文件过大");
  store.dispatch("UcFile_setFileStatus", {
    messageId: folderMessage.defaultMessageId,
    fileStatus: FileStatus.CHECKING_TOOBIG,
  });
  callback && callback("error", "chat_folder_toobig");
  return;
}

if (params == "done") {
  // 文件夹扫描完成后,进行压缩上传到服务器上
  console.log("扫描完成", data);
  // 开始打包
  ZX_CLIENT.tarFolder(folderPath, (code, tarFileData) => {
    if (code == "error") {
      console.log("打包错误");
      return;
    }
    if (code == "done") {
      console.log("打包完成", tarFileData);
      let fileData = {
        name: tarFileData.dirName,
        path: tarFileData.path,
        size: tarFileData.size,
        type: "",
      };
      folderMessage.content.localPath = tarFileData.dirPath;
      // this.setMsgStatus(targetId, msgId, "msgFileState", "1_1");
      // 开始执行上传
      console.log("folderMessage", folderMessage);
      let uploadFile = new UC_File(folderMessage);
      uploadFile.upload(fileData, (status, data) => {
        console.log(status, data);
        if (status == "success") {
          console.log("上传完成后返回的数据", data);
          let fileInfo = JSON.parse(data);
          console.log("格式化上传完成后返回的数据", fileInfo);
          const fileURL = fileInfo.atts[0].fileUrl;
          folderMessage.content.fileUrl = fileURL;

          ZX_CLIENT.removeFolder(tarFileData.tempFolderPath, (data) => {
            console.log("临时文件夹移除成功", tarFileData.tempFolderPath);
          });
          // 解决添加消息体后已经向数据库写入了,但是发送后不更新fileURL的问题
          ZX_CLIENT.ImMsg.updateChatMessage(
            {
              messageId: folderMessage.messageId,
            },
            {
              content: folderMessage.content,
            }
          );

          // 将文件夹消息发送出去
          folderMessage.sent({
            conversationType: currentChatList.conversationType,
            targetId: currentChatList.targetId,
            targetName: currentChatList.conversationTitle,
          });
        }
      });
    }
  });
}