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.
76 lines
2.2 KiB
76 lines
2.2 KiB
import Vue from 'vue'
|
|
import axios from 'axios'
|
|
import notification from 'ant-design-vue/es/notification'
|
|
import { VueAxios } from 'utils'
|
|
// import { ACCESS_TOKEN } from '@/store/mutation-types'
|
|
|
|
// 创建 axios 实例
|
|
const service = axios.create({
|
|
baseURL: process.env.NODE_ENV === 'production' ? 'http://api-test.qniao.cn/user-centre' : 'http://47.106.79.88:9000/user-centre',
|
|
timeout: 15000 // 请求超时时间
|
|
})
|
|
|
|
const err = (error) => {
|
|
console.log(error)
|
|
if (error.response) {
|
|
const data = error.response
|
|
// const token = Vue.ls.get(ACCESS_TOKEN)
|
|
// const token = JSON.parse(localStorage.getItem('pro__Access-Token') || '{}').value
|
|
const token = localStorage.getItem('pro__Access-Token')
|
|
console.log('token get from testmicro: ', token)
|
|
if (error.response.status === 403) {
|
|
notification.error({
|
|
message: '您暂时没有权限!',
|
|
description: data.message
|
|
})
|
|
}
|
|
if (error.response.status === 500) {
|
|
notification.error({
|
|
message: '服务器错误!',
|
|
description: data.message
|
|
})
|
|
}
|
|
if (error.response.status === 401 && !(data.result && data.code === 666)) {
|
|
notification.error({
|
|
message: '登录已失效',
|
|
description: '会话失效,请重新登录!'
|
|
})
|
|
if (token) {
|
|
// Vue.ls.remove(ACCESS_TOKEN)
|
|
localStorage.removeItem('pro__Access-Token')
|
|
window.location = service.baseURL
|
|
}
|
|
}
|
|
}
|
|
return Promise.reject(error)
|
|
}
|
|
|
|
// request interceptor
|
|
service.interceptors.request.use(config => {
|
|
// const token = Vue.ls.get(ACCESS_TOKEN)
|
|
// const token = JSON.parse(localStorage.getItem('pro__Access-Token') || '{}').value
|
|
const token = localStorage.getItem('pro__Access-Token')
|
|
console.log('token get from testmicro: ', token)
|
|
const _token = (JSON.parse(token) || {}).value
|
|
if (_token) {
|
|
config.headers['user-token'] = _token // 让每个请求携带自定义 token 请根据实际情况自行修改
|
|
}
|
|
return config
|
|
}, err)
|
|
|
|
// response interceptor
|
|
service.interceptors.response.use((response) => {
|
|
return response.data
|
|
}, err)
|
|
|
|
const installer = {
|
|
vm: {},
|
|
install (Vue) {
|
|
Vue.use(VueAxios, service)
|
|
}
|
|
}
|
|
|
|
export {
|
|
installer as VueAxios,
|
|
service as axios
|
|
}
|