import request from '@/axios';
import { ElLoading, ElMessage } from 'element-plus';

/**
 * 文件流返回
 * @param url 接口地址
 * @param params 接口参数
 */
export const exportBlob = (url, params, options = {}) => {
  const { feedback = false, loadingText = '导出中', successText = '导出完成' } = options;
  const loading = feedback
    ? ElLoading.service({
        lock: true,
        text: loadingText,
        background: 'rgba(255, 255, 255, 0.7)',
      })
    : null;
  return request({
    url: url,
    params: params,
    method: 'get',
    responseType: 'blob',
  })
    .then(res => {
      if (feedback) {
        ElMessage.success(successText);
      }
      return res;
    })
    .finally(() => {
      if (loading) {
        loading.close();
      }
    });
};

export const importBlob = (url, file) => {
  const formData = new FormData();
  formData.append('file', file);
  return request({
    url,
    method: 'post',
    data: formData,
    responseType: 'blob',
    timeout: 60000,
  });
};
