//http.ts
/* eslint-disable  @typescript-eslint/no-explicit-any */
import axios from 'axios'
import NProgress from 'nprogress'
import { useStore } from 'store/main'
const store = useStore()
// const CancelToken = axios.CancelToken
// const source = CancelToken.source()
const baseUrl = import.meta.env.VITE_API_URL
// const mode = import.meta.env.VITE_BUILD_MODE
const service = axios.create({
    baseURL: baseUrl,
    timeout: 60000
})

service.interceptors.request.use(
    (config) => {
       
        return config
    },
    (error) => {
        return error
    }
)
// 响应拦截
service.interceptors.response.use(
    (res) => {
        return res.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 == '0000') {
                    resolve(res.data)
                } else {
                    reject(res.data)
                }
            })
            .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()
                console.log('---------------------------', res)
                if (res.code == '0000') {
                    resolve(res.data)
                } 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 == '0000') {
                    resolve(res.data)
                } else {
                    reject(res.data)
                }
            })
            .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)
            })
    })
}
