import axios from "axios";
let downloadLoadingInstance;
import { Message, Loading } from "element-ui";
let baseUrl =
  location.origin +
  process.env.VUE_APP_BASE_API2 +
  "/common/downloadmino/download?filename=";

/**处理文件下载 */
export function uploadFile(url, fileName) {
  downloadLoadingInstance = Loading.service({
    text: "正在下载数据，请稍候",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)",
  });
  axios({
    url: baseUrl + url, // 下载链接地址
    method: "GET",
    headers: {
      Authentication: localStorage.getItem("MSSM-LIAONING__TOKEN"),
    },
    responseType: "blob", // 响应类型为 blob
  })
    .then((res) => {
      const blob = new Blob([res.data]);
      // 将 Blob 对象转换为 URL 字符串
      this.fileUrl = URL.createObjectURL(blob);
      // 创建一个隐藏的 a 标签，用于下载文件
      const link = document.createElement("a");
      link.href = this.fileUrl;
      link.download = fileName; // 下载文件的名字
      link.style.display = "none";
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      // 释放 URL 对象
      URL.revokeObjectURL(this.fileUrl);
      downloadLoadingInstance.close();
    })
    .catch((error) => {
      Message.error("下载文件出现错误，请联系管理员！");
      downloadLoadingInstance.close();
    });
}
