下面是代码
import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
import store from '@/store'
import { getToken } from '@/utils/auth'
import Qs from 'qs'
// create an axios instance
const service = axios.create({
baseURL: projectConfig.VUE_APP_BASE_API, // 'http://localhost/', // process.env.VUE_APP_BASE_API, // url = base url + request url
withCredentials: true, // send cookies when cross-domain requests
timeout: 500000 // request timeout
})
const jsonService = axios.create({
baseURL: projectConfig.VUE_APP_BASE_API, // 'http://localhost/', // process.env.VUE_APP_BASE_API, // url = base url + request url
withCredentials: true, // send cookies when cross-domain requests
timeout: 500000 // request timeout
})
jsonService.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
config.headers['Authorization'] = getToken()
}
config.headers['Content-Type'] = 'application/json;charset=UTF-8'
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
jsonService.interceptors.response.use(
/**
* If you want to get http information such as headers or status
* Please return response => response
*/
response => {
const res = response.data
// if the custom code is not 20000, it is judged as an error.
if (!res.success) {
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
if (res.code === 2001 || res.code === 50012 || res.code === 50014) {
// to re-login
MessageBox.confirm('您已注销,您可以取消停留在此页面,或重新登录', '您已注销', {
confirmButtonText: '重新登陆',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/logout')
location.reload()
})
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
},
error => {
console.log('err' + error) // for debug
Message({
message: error,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
)
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (store.getters.token) {
config.headers['Authorization'] = getToken()
}
config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
config.transformRequest = function(data) { // 在请求之前对data传参进行格式转换
data = Qs.stringify(data)
return data
}
return config
},
error => {
// do something with request error
console.log(error) // for debug
return Promise.reject(error)
}
)
// response interceptor
service.interceptors.response.use(
/**
* Determine the request status by custom code
* Here is just an example
* You can also judge the status by HTTP Status Code
*/
/**
* If you want to get http information such as headers or status
* Please return response => response
*/
response => {
const res = response.data
// if the custom code is not 20000, it is judged as an error.
if (!res.success) {
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.code === 2001 || res.code === 50012 || res.code === 50014) {
// to re-login
MessageBox.confirm('您已注销,您可以取消停留在此页面,或重新登录', '您已注销', {
confirmButtonText: '重新登陆',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
store.dispatch('user/logout')
location.reload()
})
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
},
error => {
console.log('err' + error) // for debug
Message({
message: error,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
}
)
export function get(url, params) {
return new Promise((resolve, reject) => {
service.get(url, {
params: params
}).then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
export function jsonPost(url, params) {
return new Promise((resolve, reject) => {
jsonService.post(url, params)
.then(res => {
resolve(res)
}).catch(err => {
reject(err)
})
})
}
export function post(url, params) {
return new Promise((resolve, reject) => {
service.post(url, params)
.then(res => {
resolve(res)
})
.catch(err => {
reject(err)
})
})
}
export default service
封装接口列表
引入使用
异步使用
同步使用