import axios from "axios"
import { message } from "antd";

const prefix = '/api/v1/';

const request = axios.create({
    baseURL: `${prefix}`,
    timeout: 50000,
    method:"get",
})

request.interceptors.request.use(config => {
    if (config?.params?.current) {
        config.params.page = config.params.current
        delete config.params.current
    }
    if (config?.params?.pageSize) {
        config.params.page_size = config.params.pageSize
        delete config.params.pageSize
    }

    if (config?.data?.method) {
        delete config.data.method
    }
    return config
})



request.interceptors.response.use(response => {
    let res = response.data;
    if (!res.code) {
        if (res.data?.list) {
            return {
                success: true,
                data: res.data.list,
                total: res.data.total,
                ...res.data
            }
        }
    }

    if (res.code) {
        const error = res.message
        message.error(error)
        throw error
    }
    return res
}, async error => {
    if (error && error.response && error.response.status !== 200) {
        message.error("服务器异常")
        throw error
    }
})

export default request
