import axios from 'axios' import Cookie from 'js-cookie' const request = axios.create({ baseURL: import.meta.env.VITE_API2_PREFIX, }) import { ElNotification } from 'element-plus' // 用于中止web请求 // const abortController = new AbortController() request.interceptors.request.use( (config) => { const token = Cookie.get('indent-token') console.log(token, 'token') if (token) config.headers.Authorization = `Bearer ${token}` const crmID = Cookie.get('indent-crm-id') console.log(crmID, 'indent crm id') if (crmID) config.headers.crm_id = crmID || '' const crmFullName = Cookie.get('indent-crm-fullname') console.log(crmFullName, 'indent crm fullname') if (crmFullName) config.headers.crm_full_name = crmFullName || '' return config }, (error) => { return Promise.reject(error) }, ) request.interceptors.response.use( (response) => { const { data } = response // 状态码200,code=1 成功 if (data.code != 1) { if (data instanceof Blob) { // 文件流 return data } else { ElNotification({ message: data.msg, type: 'error', duration: 3 * 1000, }) } return Promise.reject(new Error(data || 'Error')) } else { return data } }, (error) => { console.log('error: ', error.response) const { data } = error.response if (typeof data.msg === 'undefined') { ElNotification({ message: 'Error:请联系管理员', type: 'error', duration: 5 * 1000, }) } if (axios.isCancel(error)) { // 取消请求的情况下,终端Promise调用链 return new Promise(() => {}) } else { return Promise.reject(error) } }, ) export default request