|
| 1 | +import axios from 'axios' |
| 2 | +import { getToken } from '@/utils/auth' |
| 3 | + |
| 4 | +// create an axios instance |
| 5 | +const request = axios.create({ |
| 6 | + // baseURL: process.env.VUE_APP_URL, |
| 7 | + baseURL: 'https://easy-mock.bookset.io/mock/5e90379d00bfc566e5bb1acb/example', |
| 8 | + // withCredentials: true, // 跨域请求时发送cookie |
| 9 | + timeout: 60000 |
| 10 | +}) |
| 11 | + |
| 12 | +const TOKEN = getToken() // 获取token |
| 13 | + |
| 14 | +// 请求拦截器 |
| 15 | +request.interceptors.request.use( |
| 16 | + config => { |
| 17 | + if (TOKEN) { |
| 18 | + // let each request carry token |
| 19 | + // ['X-Token'] is a custom headers key |
| 20 | + // please modify it according to the actual situation |
| 21 | + config.headers['Authorization'] = TOKEN |
| 22 | + } |
| 23 | + |
| 24 | + // 扩展管理--expands:[] |
| 25 | + if (config.params && config.params.expandList) { |
| 26 | + const expandList = config.params.expandList |
| 27 | + const expands = {} |
| 28 | + if (expandList && expandList.length) { |
| 29 | + expandList.map((item, index) => { |
| 30 | + expands[`expands[${index}]`] = item |
| 31 | + }) |
| 32 | + config.params = { |
| 33 | + ...config.params, |
| 34 | + ...expands |
| 35 | + } |
| 36 | + } |
| 37 | + delete config.params.expandList |
| 38 | + } |
| 39 | + |
| 40 | + return config |
| 41 | + }, |
| 42 | + error => { |
| 43 | + // do something with request error |
| 44 | + console.log(error) |
| 45 | + return Promise.reject(error) |
| 46 | + } |
| 47 | +) |
| 48 | + |
| 49 | +// 响应拦截器 |
| 50 | +request.interceptors.response.use( |
| 51 | + response => { |
| 52 | + // const res = response.data |
| 53 | + |
| 54 | + // // if the custom code is not 20000, it is judged as an error. |
| 55 | + // if (res.code !== 20000) { |
| 56 | + // Message({ |
| 57 | + // message: res.message || 'Error', |
| 58 | + // type: 'error', |
| 59 | + // duration: 5 * 1000 |
| 60 | + // }) |
| 61 | + |
| 62 | + // // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; |
| 63 | + // if (res.code === 50008 || res.code === 50012 || res.code === 50014) { |
| 64 | + // // to re-login |
| 65 | + // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { |
| 66 | + // confirmButtonText: 'Re-Login', |
| 67 | + // cancelButtonText: 'Cancel', |
| 68 | + // type: 'warning' |
| 69 | + // }).then(() => { |
| 70 | + // store.dispatch('user/resetToken').then(() => { |
| 71 | + // location.reload() |
| 72 | + // }) |
| 73 | + // }) |
| 74 | + // } |
| 75 | + // return Promise.reject(new Error(res.message || 'Error')) |
| 76 | + // } else { |
| 77 | + // return res |
| 78 | + // } |
| 79 | + return response.data |
| 80 | + }, |
| 81 | + error => { |
| 82 | + const data = error.response.data |
| 83 | + const status = error.response.status |
| 84 | + |
| 85 | + // 对不同状态码进行管理 |
| 86 | + if (status === 401) { |
| 87 | + console.log('登录已过期'); |
| 88 | + } else if (status === 500) { |
| 89 | + console.log('服务器错误'); |
| 90 | + } |
| 91 | + return Promise.reject(data.error) |
| 92 | + } |
| 93 | +) |
| 94 | + |
| 95 | +export default request |
| 96 | + |
0 commit comments