export const downLoadByblob = (blob, fileName) => {
  const url = URL.createObjectURL(blob);
  const link = document.createElement("a");
  link.href = url;
  link.download = fileName;
  link.style.display = "none";
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
  URL.revokeObjectURL(url);
};
export const downLoadByUrl = (url, rowData, filename, timeflag = true) => {
  return new Promise((resolve, reject) => {
    try {
      // 这里加 ?v= 能绕过 跨域？？？？
      if (timeflag && !url.includes("?v=")) {
        url = url + "?v=" + new Date().getTime();
      }

      fetch(url, { mode: "cors" }).then((res) => {
        res.blob().then((blob) => {
          const blobUrl = window.URL.createObjectURL(blob);
          const a = document.createElement("a");
          a.href = blobUrl;
          if (Array.isArray(filename)) {
            let newName = "";
            filename.forEach((fi) => {
              newName = newName + " " + (rowData[fi] || fi);
            });
            filename = newName;
          }
          // a.download必须有值
          a.download = filename || new Date().getTime();
          a.click();
          window.URL.revokeObjectURL(blobUrl);
          resolve();
        });
      });
    } catch (e) {
      reject(e);
    }
  });
};
