You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1018 B
55 lines
1018 B
import Vue from 'vue'
|
|
import axios from 'axios'
|
|
|
|
|
|
|
|
// ---------------------------------------------- 封装 post 和 get
|
|
|
|
const instance = axios.create({
|
|
baseURL: 'http://127.0.0.1:9090/',
|
|
timeout: 10000 * 30,
|
|
withCredentials: true,
|
|
headers: {
|
|
'Content-Type': 'application/json; charset=utf-8'
|
|
}
|
|
})
|
|
|
|
/**
|
|
* 请求拦截
|
|
*/
|
|
instance.interceptors.request.use(config => {
|
|
config.headers['token'] =Vue.cookie.get('token') // 请求头带上token
|
|
return config
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
/**
|
|
* 响应拦截
|
|
*/
|
|
instance.interceptors.response.use(response => {
|
|
if (response.data && response.data.code === 401) { // 401, token失效
|
|
clearLoginInfo()
|
|
router.push({ name: 'login' })
|
|
}
|
|
return response
|
|
}, error => {
|
|
return Promise.reject(error)
|
|
})
|
|
|
|
|
|
|
|
|
|
export const createAPI = (url, method, data) => {
|
|
let config = {}
|
|
if (method === 'get') {
|
|
config.params = data
|
|
} else {
|
|
config.data = data
|
|
}
|
|
return instance({
|
|
url,
|
|
method,
|
|
...config
|
|
})
|
|
}
|