import axios from 'axios' import { Message } from 'element-ui' const service = axios.create({ target: `http://182.140.209.168:8888`, withCredentials: false, // 当跨域请求时发送cookie timeout: 30000, // request timeout }) // 请求拦截 service.interceptors.request.use( config => { // const token = localStorage.getItem('token') || 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImxvZ2luX3VzZXJfa2V5IjoiZWFjMDUyNWMtZjk4Mi00OWY3LWI5MTYtOTZlMmM0NzNjYjg1In0.zUMiAEKOfuto9pX4r3sKK7rfRDm-YRfSERi-0bRoBnvZKB7mApG2SdYPKRIiB6IyX7Fwz7o8IC9D3Svl2v5gMw' // if (token) { // // 添加token到请求头 // config.headers['Authorization'] = token // } if (config.method == 'get') { if (!config.params) config.params = {}; config.params = { ...config.params, } } if (config.method == 'post') { if (!config.data) config.data = {}; config.data = { ...config.data, } } return config }, error => { return Promise.reject(error) } ) // 响应拦截 service.interceptors.response.use( response => { if (!response) { return Promise.reject(new Error('请求超时')) } const res = response; if (res.status === 200) { if (!res.data) { EditionPayDialog({ show: true }); return Promise.reject(new Error('暂无数据')) } if (res.data.code == 500) { Message({ message: res.data.message || 'Error', type: 'error', duration: 3000 }) return Promise.reject(new Error(res.data.message)) } return res.data } else { Message({ message: res.message || 'Error', type: 'error', duration: 3000 }) return Promise.reject(new Error(res.message || 'Error')) } }, error => { console.log('err' + error) Message({ message: error.message, type: 'error', duration: 5000 }) return Promise.reject(error) } ) export default service