Crunchy
2025-04-29 e5454b769d44a34af423bf87ac8a740bf8c20341
src/plugins/download.js
@@ -4,6 +4,7 @@
import { getToken } from "@/utils/auth";
import errorCode from "@/utils/errorCode";
import { blobValidate } from "@/utils/ruoyi";
import Vue from "vue";
const baseURL = process.env.VUE_APP_BASE_API;
let downloadLoadingInstance;
@@ -80,11 +81,35 @@
        downloadLoadingInstance.close();
      });
  },
  saveAs(text, name, opts) {
  async saveAs(text, name, opts) {
    if (typeof text === "string") {
      // 路径下载
      saveAs(text, name, opts);
      Message.success("数据导出成功");
      try {
        let state = /\.(jpg|jpeg|png|gif)$/i.test(text) // 判断是否为图片
        let url1 = ''
        if (state) {
          url1 = Vue.prototype.javaApi + '/img/' + text;
        } else {
          if (text.startsWith("/word/")) {
            url1 = Vue.prototype.javaApi + text
          } else if (text.startsWith("word/")) {
            url1 = Vue.prototype.javaApi + '/' + text
          } else {
            url1 = Vue.prototype.javaApi + '/word/' + text
          }
        }
        // 使用 fetch 获取文件
        const response = await fetch(url1);
        if (!response.ok) {
          throw new Error('文件下载失败: ' + response.statusText);
        }
        // 将文件转换为 Blob
        const blob = await response.blob();
        // 使用 saveAs 保存文件
        saveAs(blob, name);
        Message.success("数据导出成功");
      } catch (error) {
        Message.error(error);
      }
    } else {
      // 流下载
      blobToText(text)