import axios from 'axios'
|
import { MessageBox, Message } from 'element-ui'
|
import store from '@/store'
|
import { getToken } from '@/utils/auth'
|
const baseUrl = 'http://47.99.64.149:8081/'
|
// const baseUrl = 'http://120.26.43.34:8081/api/'
|
// const baseUrl = 'http://192.168.0.42:8081/' // lzj
|
// const baseUrl = 'http://192.168.0.33:8081/' // yy
|
// const baseUrl = 'http://192.168.0.25:8081' // jj
|
// const baseUrl = 'http://192.168.0.28:8081/'// cjl
|
// const baseUrl = 'http://192.168.0.11:8081/' // new swb
|
// const baseUrl = 'http://47.96.171.62:8081/'//测试环境
|
// const baseUrl = 'http://121.43.179.139:8080'
|
// const baseUrl = 'http://192.168.0.33:8085/'
|
// const baseUrl = 'http://192.168.0.33:8081/'
|
|
// create an axios instance
|
const service = axios.create({
|
// baseURL: 'http://47.99.64.149:8080/screen_api_v2/', // url = base url + request url
|
baseURL: baseUrl,
|
// baseURL: 'http://192.168.0.195:8081/',
|
// baseURL: ' http://www.7drlb7.com:8081/',
|
// withCredentials: true, // send cookies when cross-domain requests
|
timeout: 50000 // request timeout
|
})
|
const reqServe = axios.create({ baseurl: 'http://121.43.179.139:8080', timeout: '5000' })
|
// const reqServe1 = axios.create({ baseurl: 'http://192.168.55.1:8081', timeout: '5000' })
|
|
// instance1这里用到的参数有 baseurl,timeout,method,url instance1.get('/userinfo').then(res=>{ console.log(res) }) //instance2这里用到的参数有 baseurl,timeout,method,url,params,并且对timeout进行了修改 instance1.get('/orderlist',{ timeout:'5000' params:{} }).then(res=>{ console.log(res) })
|
// 请求拦截器
|
service.interceptors.request.use(
|
config => {
|
// 在发送请求之前做 第一次不走if,直接登录返回config(token),第二次会拿token去比对是否正确
|
if (store.getters.token) {
|
// 让每个请求携带令牌
|
// ['X-Token'] 是自定义令牌
|
// please modify it according to the actual situation
|
config.headers['token'] = getToken()
|
config.headers['Authorization'] = getToken()
|
}
|
return config
|
},
|
error => {
|
// do something with request error
|
console.log(error) // for debug
|
return Promise.reject(error)
|
}
|
)
|
|
// response interceptor 响应拦截器,对接收的数据进行判断。
|
service.interceptors.response.use(
|
response => {
|
const res = response.data
|
|
// if the custom code is not 20000, it is judged as an error.
|
if (res.accountId === -1) {
|
Message({
|
message: res.msg || 'Error',
|
type: 'error',
|
duration: 5 * 1000
|
})
|
|
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
|
// to re-login
|
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
|
confirmButtonText: 'Re-Login',
|
cancelButtonText: 'Cancel',
|
type: 'warning'
|
}).then(() => {
|
store.dispatch('user/resetToken').then(() => {
|
location.reload()
|
})
|
})
|
}
|
return Promise.reject(new Error(res.message || 'Error'))
|
} else {
|
if (getToken()) {
|
if (res.code === -3) {
|
store.dispatch('user/resetToken').then(() => {
|
Message({
|
message: res.message,
|
type: 'warning'
|
})
|
setTimeout(() => {
|
location.reload()
|
}, 2000)
|
})
|
}
|
}
|
return res
|
}
|
},
|
error => {
|
console.log('err' + error) // for debug
|
Message({
|
message: error.message,
|
type: 'error',
|
duration: 5 * 1000
|
})
|
return Promise.reject(error)
|
}
|
)
|
|
export default { service, baseUrl, reqServe }
|