//http.ts
/* eslint-disable  @typescript-eslint/no-explicit-any */
import axios from 'axios'
import NProgress from 'nprogress'
import { useStore } from 'store/main'
import { ElMessage } from "element-plus";
import router from "@/router"
const store = useStore()
// const CancelToken = axios.CancelToken
// const source = CancelToken.source()
const baseUrl = import.meta.env.VITE_API_URL
const VITE_token = import.meta.env.VITE_token
const VITE_LocalFlag = import.meta.env.VITE_LocalFlag

const VITE_ENV = import.meta.env.VITE_ENV

const service = axios.create({
    baseURL: baseUrl,
    timeout: 60000
})

service.interceptors.request.use(
    (config) => {
        config.headers['Authorization'] = store.token
        return config
    },
    (error) => {
        return error
    }
)
// 响应拦截
service.interceptors.response.use(
    (res) => {
        const { data } = res
        if (data.code == '200') {
            return data
        } else if (data.code == '403') {
            ElMessage.error('请重新登录')
            router.push('/login')
        } else if( data.code == '401' ){
            ElMessage.error(data.msg)
            router.push('/login')
        }
        else {
            return data
        }
    },
    (error) => {

        console.log('error-异常', error)
    }
)

export function get(url: string, params: any) {
    return new Promise((resolve, reject) => {
        NProgress.start()
        service
            .get(url, { params })
            .then((res: any) => {
                NProgress.done()
                if (res.code == '200') {
                    resolve(res)
                } else {
                    reject(res)
                }
            })
            .catch((err) => {
                NProgress.done()
                reject(err.data)
            })
    })
}
export function post(url: string, params: any) {
    return new Promise((resolve, reject) => {
        NProgress.start()
        service
            .post(url, params, {
                headers: { 'Content-Type': 'application/json; charset=utf-8' }
            })
            .then((res: any) => {
                NProgress.done()
                if (res.code == '200') {
                    resolve(res)
                } else {
                    reject(res)
                }
            })
            .catch((err) => {
                NProgress.done()
                reject(err)
            })
    })
}
export function upload(url: string, params: any) {
    const formData = new FormData()
    for (const key in params) {
        if (Object.prototype.hasOwnProperty.call(params, key)) {
            formData.append(key, params[key])
        }
    }
    return new Promise((resolve, reject) => {
        NProgress.start()
        service
            .post(url, formData, {
                headers: { 'Content-Type': 'multipart/form-data' }
            })
            .then((res: any) => {
                NProgress.done()
                if (res.code == '200') {
                    resolve(res)
                } else {
                    reject(res)
                }
            })
            .catch((err) => {
                NProgress.done()
                reject(err.data)
            })
    })
}
export function download(url: string, params: any) {
    return new Promise((resolve, reject) => {
        NProgress.start()
        service
            .post(url, params, {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                responseType: 'blob'
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err) => {
                NProgress.done()
                reject(err.data)
            })
    })
}

export function put(url: string, params: any) {
    return new Promise((resolve, reject) => {
        NProgress.start()
        service
            .put(url, params, {
                headers: { 'Content-Type': 'application/json; charset=utf-8' }
            })
            .then((res: any) => {
                NProgress.done()
                if (res.code == '200') {
                    resolve(res)
                } else {
                    reject(res)
                }
            })
            .catch((err) => {
                NProgress.done()
                reject(err)
            })
    })
}

export function detele(url: string, params: any) {
    return new Promise((resolve, reject) => {
        NProgress.start()
        service
            .delete(url, { params })
            .then((res: any) => {
                NProgress.done()
                if (res.code == '200') {
                    resolve(res)
                } else {
                    reject(res)
                }
            })
            .catch((err) => {
                NProgress.done()
                reject(err.data)
            })
    })
}
