axios.ts
// 引入网络请求库 https://github.com/axios/axios import axios from 'axios' import { message } from 'choerodon-ui'; // import store from '../store' // import router from '../router' // 请求列表 const requestList: any = [] // 取消列表 const CancelToken = axios.CancelToken let sources: any = {} // axios.defaults.timeout = 10000 axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8' axios.defaults.baseURL = process.env.BASE_URL || 'http://localhost:7001/' axios.interceptors.request.use((config) => { const request = JSON.stringify(config.url) + JSON.stringify(config.data) config.cancelToken = new CancelToken((cancel) => { sources[request] = cancel }) if(requestList.includes(request)){ sources[request]('取消重复请求') }else{ requestList.push(request) // store.dispatch('changeGlobalState', {loading: true}) } // const token = store.getters.userInfo.token // if (token) { // config.headers.token = token // } return config }, function (error) { return Promise.reject(error) }) axios.interceptors.response.use(function (response) { const request = JSON.stringify(response.config.url) + JSON.stringify(response.config.data) requestList.splice(requestList.findIndex((item: any) => item === request), 1) if (requestList.length === 0) { // store.dispatch('changeGlobalState', {loading: false}) } if (response.data.code === 900401) { message.error('认证失效,请重新登录!', 1000) // router.push('/login') } return response }, function (error) { if (axios.isCancel(error)) { requestList.length = 0 // store.dispatch('changeGlobalState', {loading: false}) throw new axios.Cancel('cancel request') } else { message.error('网络请求失败', 1000) } return Promise.reject(error) }) const request = function (url: any, params: any, config: any, method: string) { return new Promise((resolve, reject) => { // @ts-ignore axios[method](url, params, Object.assign({}, config)).then((response: any) => { resolve(response.data) }, (err: any) => { if (err.Cancel) { console.log(err) } else { reject(err) } }).catch((err: any) => { reject(err) }) }) } const post = (url: any, params: any, config = {}) => { return request(url, params, config, 'post') } const get = (url: any, params: any, config = {}) => { return request(url, params, config, 'get') } export {sources, post, get}
.