import Axios from 'axios';


const fetch = async config => {
    const {
        baseUrl, method, params, independent, url, ...other
    } = config;
    const timestamp = new Date().getTime();
    // 创建axios实例
    const axiosIns = Axios.create();
    const headers = {};
    let options = {
        headers,
        baseURL: baseUrl,
        timeout: 1 * 60 * 1000,
        url,
        ...other
    };
    if (method === 'post') {
        options = Object.assign(
            options,
            {
                method: 'post',
                data: params
            }
        );
    } else {
        options = Object.assign(
            options,
            {
                method: 'get',
                params: {
                    ...params,
                    timestamp
                }
            }
        );
    }

    const response = await axiosIns(options).then(res => {
        const { code } = res.data;
        if (String(code) !== '0') { // code不为0视为错误
            console.error(res);
            return false;
        }
        return res.data;
    }).catch(error => {
        if (!Axios.isCancel(error)) {
            console.error('系统异常');
            return false;
        }
        return false;
    });
    return response;
};

export default fetch;
