refine
This commit is contained in:
parent
a5c07ea83d
commit
30d63c568a
|
@ -4,10 +4,10 @@ import { isString, isArray } from '@/utils/validate'
|
||||||
import settings from '@/settings'
|
import settings from '@/settings'
|
||||||
|
|
||||||
// you can set in settings.js
|
// you can set in settings.js
|
||||||
// errorLog:'production' | ['production','development']
|
// errorLog:'production' | ['production', 'development']
|
||||||
const { errorLog: needErrorLog } = settings
|
const { errorLog: needErrorLog } = settings
|
||||||
|
|
||||||
function checkNeed(arg) {
|
function checkNeed() {
|
||||||
const env = process.env.NODE_ENV
|
const env = process.env.NODE_ENV
|
||||||
if (isString(needErrorLog)) {
|
if (isString(needErrorLog)) {
|
||||||
return env === needErrorLog
|
return env === needErrorLog
|
||||||
|
|
|
@ -5,23 +5,25 @@ import { getToken } from '@/utils/auth'
|
||||||
|
|
||||||
// create an axios instance
|
// create an axios instance
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
|
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
|
||||||
withCredentials: true, // 跨域请求时发送 cookies
|
withCredentials: true, // send cookies when cross-domain requests
|
||||||
timeout: 5000 // request timeout
|
timeout: 5000 // request timeout
|
||||||
})
|
})
|
||||||
|
|
||||||
// request interceptor
|
// request interceptor
|
||||||
service.interceptors.request.use(
|
service.interceptors.request.use(
|
||||||
config => {
|
config => {
|
||||||
// Do something before request is sent
|
// do something before request is sent
|
||||||
|
|
||||||
if (store.getters.token) {
|
if (store.getters.token) {
|
||||||
// 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
|
// let each request carry token --['X-Token'] as a custom key.
|
||||||
|
// please modify it according to the actual situation.
|
||||||
config.headers['X-Token'] = getToken()
|
config.headers['X-Token'] = getToken()
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
// Do something with request error
|
// do something with request error
|
||||||
console.log(error) // for debug
|
console.log(error) // for debug
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
}
|
}
|
||||||
|
@ -33,31 +35,33 @@ service.interceptors.response.use(
|
||||||
* If you want to get information such as headers or status
|
* If you want to get information such as headers or status
|
||||||
* Please return response => response
|
* Please return response => response
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下面的注释为通过在response里,自定义code来标示请求状态
|
* Determine the request status by custom code
|
||||||
* 当code返回如下情况则说明权限有问题,登出并返回到登录页
|
* Here is just an example
|
||||||
* 如想通过 XMLHttpRequest 来状态码标识 逻辑可写在下面error中
|
* You can also judge the status by HTTP Status Code.
|
||||||
* 以下代码均为样例,请结合自生需求加以修改,若不需要,则可删除
|
|
||||||
*/
|
*/
|
||||||
response => {
|
response => {
|
||||||
const res = response.data
|
const res = response.data
|
||||||
|
|
||||||
|
// if the custom code is not 20000, it is judged as an error.
|
||||||
if (res.code !== 20000) {
|
if (res.code !== 20000) {
|
||||||
Message({
|
Message({
|
||||||
message: res.message || 'error',
|
message: res.message || 'error',
|
||||||
type: 'error',
|
type: 'error',
|
||||||
duration: 5 * 1000
|
duration: 5 * 1000
|
||||||
})
|
})
|
||||||
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了;
|
|
||||||
|
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
||||||
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
|
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
|
||||||
// 请自行在引入 MessageBox
|
// to re-login
|
||||||
// import { Message, MessageBox } from 'element-ui'
|
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: '取消',
|
|
||||||
type: 'warning'
|
type: 'warning'
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
store.dispatch('user/resetToken').then(() => {
|
store.dispatch('user/resetToken').then(() => {
|
||||||
location.reload() // 为了重新实例化vue-router对象 避免bug
|
location.reload()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue