import axios from 'axios'
import { getToken,removeToken } from '@/utils'
import router from '@/router'

const service = axios.create({
    baseURL: '/api',
    timeout: 40000,
})

service.interceptors.request.use((config) => {
    config.headers['Jifu-Token'] = getToken()
    return config
},(error) => {
    return Promise.reject(error)
})

service.interceptors.response.use((response) => {
    if(response.data.error == 1001) {
        removeToken()
        router.push('/login')
    }
    return response
},(error) => {
    return Promise.reject(error)
})

export const get = (url, params) => {
    return new Promise((resolve, reject) => {
        service.get(url, {params})
        .then(res => resolve(res))
        .catch(err => reject(err));
    });
}

export const post = (url, param) => {
    return new Promise((resolve, reject) => {
        service.post(url, param)
        .then(res => resolve(res))
        .catch(err => reject(err));
    });
}

export default service