|
|
|
@ -1,7 +1,7 @@ |
|
|
|
|
import axios from 'axios' |
|
|
|
|
import Util from '@/libs/util' |
|
|
|
|
import Setting from '@/setting' |
|
|
|
|
import store from '@/store' |
|
|
|
|
import Store from '@/store' |
|
|
|
|
import Router from '@/router' |
|
|
|
|
|
|
|
|
|
const service = axios.create({ |
|
|
|
@ -14,14 +14,17 @@ service.defaults.headers['X-Content-Type-Options'] = 'nosniff' |
|
|
|
|
service.defaults.headers['Content-Security-Policy'] = 'script-src "self"; object-src "none";style-src cdn.example.org third-party.org; child-src https:' |
|
|
|
|
// 请求拦截器
|
|
|
|
|
service.interceptors.request.use(config => { |
|
|
|
|
let token = Util.local.get(Setting.tokenKey); |
|
|
|
|
if (token) config.headers.token = token |
|
|
|
|
let token = Util.local.get(Setting.tokenKey) |
|
|
|
|
if (token) { |
|
|
|
|
config.headers.token = token |
|
|
|
|
Store.state.user.userId || Store.dispatch('user/logout') |
|
|
|
|
} |
|
|
|
|
return config; |
|
|
|
|
}, err => { |
|
|
|
|
Util.errorMsg({ |
|
|
|
|
message: "退出登陆", |
|
|
|
|
message: '退出登陆', |
|
|
|
|
onClose: function () { |
|
|
|
|
store.dispatch('user/logout') |
|
|
|
|
Store.dispatch('user/logout') |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
return Promise.reject(err); |
|
|
|
@ -37,7 +40,7 @@ service.interceptors.response.use( |
|
|
|
|
} else if (code === 401) { |
|
|
|
|
Util.errorMsg(res.msg) |
|
|
|
|
setTimeout(() => { |
|
|
|
|
store.dispatch('user/logout') |
|
|
|
|
Store.dispatch('user/logout') |
|
|
|
|
}, 1000) |
|
|
|
|
return Promise.reject(res) |
|
|
|
|
} else if (code == 300) { |
|
|
|
@ -61,14 +64,14 @@ service.interceptors.response.use( |
|
|
|
|
case 401: |
|
|
|
|
Util.errorMsg("登录过期,请重新登录") |
|
|
|
|
setTimeout(() => { |
|
|
|
|
store.dispatch('user/logout') |
|
|
|
|
Store.dispatch('user/logout') |
|
|
|
|
}, 1000) |
|
|
|
|
break |
|
|
|
|
// 403 token过期
|
|
|
|
|
case 403: |
|
|
|
|
Util.errorMsg("登录过期,请重新登录") |
|
|
|
|
setTimeout(() => { |
|
|
|
|
store.dispatch('user/logout') |
|
|
|
|
Store.dispatch('user/logout') |
|
|
|
|
}, 1000) |
|
|
|
|
break |
|
|
|
|
// 其他错误,直接抛出错误提示
|
|
|
|
|