import { Interceptors } from './interceptors';
import { ElMessage } from "element-plus";
import {downloadFile} from "@/utils/util";
export class HttpRequest {
  public axios: any
  constructor() {
    // 获取axios实例
    this.axios = new Interceptors().getInterceptors();
  }
  public get = ({url, params, config = {}}: { [index: string]: any }) => {
    return new Promise((resolve, reject) => {
      let paramUrl = url
      if (params) {
        if (typeof params === 'object') {
          let str = ''
          Object.entries(params).map(([k, v]: any, i) => {
            if (i > 0) {
              str += '&'
            }
            str += `${k}=${encodeURIComponent(v)}`
          })
          paramUrl += `?${str}`
        } else {
          paramUrl += `?${params}`
        }
      }
      this.axios.get(paramUrl, {
        headers: {
          Authorization: sessionStorage.getItem('sg_token')
        },
        ...config
      }).then((res: any) => {
        resolve(res.data)
      }).catch((err) => {
        reject(err);
      })
    })
  }
  public post = ({url, params, config = {}}: { [index: string]: any }) => {
    return new Promise((resolve, reject) => {
      this.axios.post(url, params, {
        headers: {
          Authorization: sessionStorage.getItem('sg_token'),
        },
        ...config
      }).then((res: any) => {
        resolve(res.data)
      }).catch((err) => {
        reject(err);
      })
    })
  }
  public put = ({url, params, config = {}}: { [index: string]: any }) => {
    return new Promise((resolve, reject) => {
      this.axios.put(url, params, {
        headers: {
          Authorization: sessionStorage.getItem('sg_token'),
        },
        ...config
      }).then((res: any) => {
        resolve(res.data)
      }).catch((err) => {
        reject(err);
      })
    })
  }
  public delete = ({url, params, config = {}}: { [index: string]: any }) => {
    return new Promise((resolve, reject) => {
      let paramUrl = url
      if (params) {
        if (typeof params === 'object') {
          let str = ''
          Object.entries(params).map(([k, v]: any, i) => {
            if (i > 0) {
              str += '&'
            }
            str += `${k}=${encodeURIComponent(v)}`
          })
          paramUrl += `?${str}`
        } else {
          paramUrl += `?${params}`
        }
      }
      this.axios.delete(paramUrl, {
        headers: {
          Authorization: sessionStorage.getItem('sg_token')
        },
        ...config
      }).then((res: any) => {
        resolve(res.data)
      }).catch((err) => {
        reject(err);
      })
    })
  }
  public download = ({url, params, fileName, config = {}}: { [index: string]: any }) => {
    return new Promise((resolve, reject) => {
      let paramUrl = url
      if (params) {
        if (typeof params === 'object') {
          let str = ''
          Object.entries(params).map(([k, v]: any, i) => {
            if (i > 0) {
              str += '&'
            }
            str += `${k}=${encodeURIComponent(v)}`
          })
          paramUrl += `?${str}`
        } else {
          paramUrl += `?${params}`
        }
      }
      this.axios.get(paramUrl, {
        headers: {
          Authorization: sessionStorage.getItem('sg_token'),
        },
        responseType: 'arraybuffer',
        ...config
      }).then((res: any) => {
        downloadFile(res, fileName)
        resolve(res)
      }).catch((err) => {
        reject(err);
      })
    })
  }
}

export default HttpRequest
