import axios from 'axios';
import FileSaver from 'file-saver';
import errorCode from '@/utils/errorCode';
import { blobValidate } from '@/utils/ruoyi';
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
import { downloadByGet, globalHeaders } from '@/utils/request';

const baseURL = import.meta.env.VITE_APP_BASE_API;
let downloadLoadingInstance: LoadingInstance;
export default {
  async oss(ossId: string | number) {
    const url = '/resource/oss/download/' + ossId;
    return downloadByGet(url);
  },
  async zip(url: string, name: string) {
    const fullUrl = url;
    return downloadByGet(fullUrl, name, 'application/zip');
  },
  async printErrMsg(data: any) {
    const resText = await data.text();
    const rspObj = JSON.parse(resText);
    const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
    ElMessage.error(errMsg);
  },
  /**
   * 业务附件下载
   * @param url 附件下载地址
   * @param attId 附件ID
   * @param busCode 业务类型
   */
  async attach(url: string, attId: string | number, busCode: string) {
    const fullUrl = baseURL + url + '?attId=' + attId + '&busCode=' + busCode;

    try {
      await downloadByGet(fullUrl);
    } catch (error) {
      // 如果下载失败，显示特定的错误信息
      ElMessage.error('未找到该文件！');
      throw error;
    }
  }
};
