Blame view
src/utils/request.js
3.64 KB
80a28914e init |
1 |
import axios from 'axios' |
60fa0be9d auto commit the c... |
2 3 4 5 |
import { MessageBox, Message } from 'element-ui' |
80a28914e init |
6 |
import store from '@/store' |
60fa0be9d auto commit the c... |
7 8 9 |
import { getToken } from '@/utils/auth' |
80a28914e init |
10 11 |
// create an axios instance |
a795fa7b1 auto commit the c... |
12 |
// 创建axios实例 |
80a28914e init |
13 |
const service = axios.create({ |
60fa0be9d auto commit the c... |
14 |
// baseURL: '', // url = base url + request url |
80a28914e init |
15 16 |
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests |
60fa0be9d auto commit the c... |
17 |
timeout: 5000, // request timeout |
c40d344e2 auto commit the c... |
18 19 20 |
// headers: { // 'Content-Type': 'application/x-www-form-urlencoded' // } |
80a28914e init |
21 22 23 |
}) // request interceptor |
a795fa7b1 auto commit the c... |
24 |
// request拦截器 |
80a28914e init |
25 26 27 |
service.interceptors.request.use( config => { // do something before request is sent |
60fa0be9d auto commit the c... |
28 |
// console.log('do something before request is sent') |
80a28914e init |
29 |
if (store.getters.token) { |
60fa0be9d auto commit the c... |
30 |
// console.log('[X-Token] is a custom headers key') |
80a28914e init |
31 32 33 |
// let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation |
60fa0be9d auto commit the c... |
34 |
config.headers['X-Token'] = getToken(); |
c40d344e2 auto commit the c... |
35 |
// config.headers['Content-Type'] = 'application/x-www-form-urlencoded'; |
60fa0be9d auto commit the c... |
36 37 |
// var token = getToken() // Object.assign(config.headers, { 'token': token }) |
80a28914e init |
38 39 40 41 |
} return config }, error => { |
60fa0be9d auto commit the c... |
42 |
// console.log('do something with request error') |
80a28914e init |
43 |
// do something with request error |
c40d344e2 auto commit the c... |
44 |
console.error('出现错误。在use里。======>', error) // for debug |
80a28914e init |
45 46 47 48 49 |
return Promise.reject(error) } ) // response interceptor |
a795fa7b1 auto commit the c... |
50 |
// respone拦截器 |
80a28914e init |
51 52 53 54 |
service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response |
60fa0be9d auto commit the c... |
55 |
*/ |
80a28914e init |
56 57 58 59 60 61 62 63 |
/** * Determine the request status by custom code * Here is just an example * You can also judge the status by HTTP Status Code */ response => { const res = response.data |
c40d344e2 auto commit the c... |
64 |
console.log('返回的数据-- step 1------>', res); |
a795fa7b1 auto commit the c... |
65 |
/** |
60fa0be9d auto commit the c... |
66 67 68 69 |
* 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 */ // console.log('if the custom code is not 20000, it is judged as an error.') |
80a28914e init |
70 71 72 73 74 75 |
if (res.code !== 20000) { Message({ message: res.message || 'Error', type: 'error', duration: 5 * 1000 }) |
a795fa7b1 auto commit the c... |
76 |
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; |
80a28914e init |
77 78 |
if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login |
a795fa7b1 auto commit the c... |
79 80 81 82 83 84 |
// MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { // confirmButtonText: 'Re-Login', confirmButtonText: '重新登录', // cancelButtonText: 'Cancel', cancelButtonText: '取消', |
80a28914e init |
85 86 87 |
type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { |
60fa0be9d auto commit the c... |
88 |
location.reload() // 为了重新实例化vue-router对象 避免bug |
80a28914e init |
89 90 91 92 93 |
}) }) } return Promise.reject(new Error(res.message || 'Error')) } else { |
60fa0be9d auto commit the c... |
94 |
// const token = res.data.token; |
c40d344e2 auto commit the c... |
95 96 97 |
console.log('进入20000号判断- step 2------->', res.data); const data = res.data; return data; |
80a28914e init |
98 99 100 |
} }, error => { |
c40d344e2 auto commit the c... |
101 102 103 104 105 106 |
console.error('===============发生错误!!!!!===============' + error.message); // for debug // Message({ // message: error.message, // type: 'error', // duration: 5 * 1000 // }) |
80a28914e init |
107 108 109 110 111 |
return Promise.reject(error) } ) export default service |