import axios from 'axios';
import Router from '@/router/index';
import { showLoading, hideLoading } from '../utils/loading';
import {MessageBox} from 'element-ui';

const service = axios.create({
  baseURL: process.env.VUE_APP_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 50000 // request timeout
})


// 登录请求方法
const reqNull = (method, url, params) => {
  return service({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json',
    },
    withCredentials: true,
    data: params
  }).then(res => {
    if(res.data.code===403 || res.data.code===401){
      localStorage.clear();
    }else{
      return res.data;
    }
  }).catch(error => {
    console.log(error,"error11111111111111");

    MessageBox.alert("登录失败，请检查网络！", "提示", {
      confirmButtonText: "确定",
      callback: () => {
        window.location.reload();
      }
    }).then(r => this.load(false));
  });
};
// 通用公用方法
const req = (method, url, params) => {
  showLoading();
  return service({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json',
      'Authorization': localStorage.getItem("logintoken")
    },
    withCredentials: true,
    // responseType: 'blob',
    data: params
  }).then(res => {
    hideLoading();
    if(res.data.code===403 || res.data.code===401){
      localStorage.clear();
    }else{
      return res.data;
    }
  }).catch(error => {
    hideLoading();
    localStorage.clear();
    window.location.reload();
  });
};

const downloadFile = (method, url, params, filename) =>{
  return service({
    method: method,
    url: url,
    headers: {
      'Authorization': localStorage.getItem("logintoken")
    },
    responseType: 'blob',
    data: params
  }).then((response) => {
    // 创建一个新的 Blob 对象，并使用响应数据
    const blob = new Blob([response.data]);
    const link = document.createElement('a'); // 创建一个新的 a 标签
    link.href = window.URL.createObjectURL(blob); // 创建下载链接
    link.download = filename; // 设置文件名
    link.click(); // 触发点击，实现下载
    window.URL.revokeObjectURL(link.href); // 释放 URL 对象
  }).catch((error) => {
    console.error('下载文件失败:', error); // 错误处理
  }).catch(error => {
    hideLoading();
    localStorage.clear();
    window.location.reload();
  });
};

const openFile = (method, url, params, filename, fileType) =>{
  return service({
    method: method,
    url: url,
    headers: {
      'Authorization': localStorage.getItem("logintoken")
    },
    responseType: 'blob',
    data: params
  }).then((response) => {
    // 创建一个新的 Blob 对象，并使用响应数据
    let options ={}
    if(fileType === 'pdf' ||  fileType === 'PDF'){
      options.type = 'application/pdf'
    }
    if(fileType === 'png' ||  fileType === 'PNG' ||
      fileType === 'jpg' ||  fileType === 'JPG' ||
      fileType === 'jpeg' ||  fileType === 'JPEG'){
      options.type = 'image/'+fileType
    }
    if(fileType == 'mp4' || fileType == 'MP4' ||
      fileType == 'mp3' || fileType == 'MP3' ||
      fileType == 'avi' || fileType == 'AVI' ||
      fileType == 'wmv' || fileType == 'WMV' ||
      fileType == 'rmvb' || fileType == 'RMVB' ||
      fileType == 'rm' || fileType == 'RM' ){
      options.type = 'video/'+fileType
    }
    const blob = new Blob([response.data], options);
    const url = URL.createObjectURL(blob); // 创建URL
    window.open(url);
  }).catch((error) => {
    console.error('下载文件失败:', error); // 错误处理
  }).catch(error => {
    hideLoading();
    localStorage.clear();
    window.location.reload();
  });
};

export {
  reqNull,
  req,
  downloadFile,
  openFile
}
