import Vue from 'vue' import axios from 'axios' import store from '@/store' import router from '@/router' import notification from 'ant-design-vue/es/notification' import modal from 'ant-design-vue/es/modal' import { getAuthPriceCode } from './util' import { VueAxios } from './axios' import { getUserOsInfo, getBrowserType} from './util' //没有基地址 访问根目录下文件 const GETNOBASE = async (url, params) => { try { const data = await axios.get(url, { params: params, }); return data.data; } catch (error) { return error; } } // 创建 axios 实例 const service = axios.create({ baseURL: process.env.VUE_APP_API_BASE_URL, // api base_url timeout: 6000000 // 请求超时时间 }) const err = (error) => { console.log(error.response) const config = error.config // 网络连接出错 if (error.message.indexOf('Network Error') >= 0) { notification.destroy() notification.error({ message: '提示', description: '无网络连接,请检查网络是否连接正常' }) store.state.app.curActionPermission = '' store.commit('SET_loadingStatus', false) return Promise.reject(err) } // 业务错误提示 if (error.response) { const status = error.response.status const resData = error.response.data console.log(status,resData) if ((status == 503 || status == 500 || status == 404)) { notification.destroy() store.state.app.curActionPermission = '' if(resData instanceof Blob){ resData.text().then(data => { const res = JSON.parse(data) notification.error({ message: '提示', description: res.message }) }) }else{ notification.error({ message: '提示', description: resData.message || error.response.statusText || '服务器出错,请稍后再试!' }) } } store.commit('SET_loadingStatus', false) return Promise.reject(err) } // 超时无法访问服务 if (error.message.indexOf('timeout') >= 0) { notification.destroy() notification.error({ message: '提示', description: '网络连接超时,正在重连...' }) // If config does not exist or the retry option is not set, reject if (!config || !config.retry) return Promise.reject(err) // Set the variable for keeping track of the retry count config.__retryCount = Vue.ls.get(config.url + '__retryCount') || 0 // Check if we've maxed out the total number of retries if (config.__retryCount >= config.retry) { Vue.ls.remove(config.url + '__retryCount') notification.destroy() notification.error({ message: '提示', description: '网络重连失败,请检测网络连接是否正常' }) store.commit('SET_loadingStatus', false) // Reject with the error return Promise.reject(err) } // Increase the retry count config.__retryCount += 1 Vue.ls.set(config.url + '__retryCount', config.__retryCount) // Create new promise to handle exponential backoff config.url = config.url.replace('/api', '') const backoff = new Promise(function (resolve) { setTimeout(function () { resolve() }, config.retryDelay || 1) }) // Return the promise in which recalls axios to retry the request return backoff.then(function () { return service(config) }) } } // request interceptor service.interceptors.request.use(config => { console.log(store.state.app.curActionPermission) const authPrice = getAuthPriceCode(config, router, store) const token = store.getters.token if (token) { config.headers['access-token'] = token // 让每个请求携带自定义 token 请根据实际情况自行修改 } // 操作系统、浏览器信息 config.headers['os'] = getUserOsInfo() config.headers['bws'] = getBrowserType() // 分辨率 config.headers['sdr'] = window.screen.width+'X'+window.screen.height // 获取当前时间撮 const interFaceList = store.getters.interFaceList const curPath = config.url if(interFaceList){ const hasPath = interFaceList.find(item => { const hasSp = item.b.find(k => curPath.indexOf(k)>=0) return hasSp }) if(hasPath){ config.headers['biz-updatedate'] = hasPath.timeStr } } // 当前页面路径 const currentPage = router.history.current if(currentPage&¤tPage.meta&&config.headers['module']){ config.headers['module'] = encodeURIComponent(currentPage.meta.title||'') + ',' +config.headers['module'] } config.headers['auth-price'] = authPrice.join(',') // 价格权限 config.retry = 3 config.retryDelay = 1000 store.commit('SET_loadingStatus', true) return config }, err) // response interceptor service.interceptors.response.use((response) => { // console.log(response, 'response.data.status') const et = sessionStorage.getItem('errorTips') if ((response.data.status == '1001' || response.data.status == '1099') && !et) { sessionStorage.setItem('errorTips', 1) modal.destroyAll() modal.error({ title: '提示', content: response.data.message, onOk () { sessionStorage.setItem('errorTips', 0) store.state.app.curActionPermission = '' store.dispatch('Logout').then(() => { window.location.reload() }) } }) } if ((response.data.status == '401' || response.data.status == '500') && window.location.pathname != '/user/login') { store.state.app.curActionPermission = '' notification.destroy() notification.error({ message: '提示', description: response.data.message }) } if (response.data.status == '900' && window.location.pathname != '/user/login') { store.state.app.curActionPermission = '' notification.destroy() notification.error({ message: response.data.message, description: response.data.code }) } if(response.data.status!='200'){ store.state.app.curActionPermission = '' store.commit('SET_loadingStatus', false) } // 防重复提交时间戳 const interFaceList = store.getters.interFaceList const curPath = response.config.url if(interFaceList){ const hasPath = interFaceList.find(item => curPath.indexOf(item.a)>=0) if(hasPath){ hasPath.timeStr = response.data.data.updateDate } } return response.data }, err) const installer = { vm: {}, install (Vue) { Vue.use(VueAxios, service) } } export { installer as VueAxios, service as axios, GETNOBASE }