|
@@ -1,6 +1,9 @@
|
|
|
+import { getToken, removeToken } from './../utils/auth';
|
|
|
import { extend } from 'umi-request'
|
|
|
import { ElMessage } from 'element-plus'
|
|
|
import cleanDeep from 'clean-deep'
|
|
|
+import router from '../router';
|
|
|
+import { showLoading, hideLoading } from '../utils/loading'
|
|
|
// import { setLogout } from 'src/actions/users'
|
|
|
// import store from 'src/store'
|
|
|
|
|
@@ -23,7 +26,11 @@ const request = extend({
|
|
|
})
|
|
|
|
|
|
request.interceptors.request.use((url, options: any) => {
|
|
|
- const Authorization = (localStorage.getItem('Authorization') || '')
|
|
|
+ let hideLoading = options.hideLoading || false
|
|
|
+ if(!hideLoading) {
|
|
|
+ showLoading()
|
|
|
+ }
|
|
|
+ const Authorization = getToken()
|
|
|
const tenantId = (localStorage.getItem('tenantId') || '')
|
|
|
const organId = (localStorage.getItem('organId') || '')
|
|
|
const authHeaders: any = {}
|
|
@@ -50,6 +57,9 @@ request.interceptors.request.use((url, options: any) => {
|
|
|
})
|
|
|
|
|
|
request.interceptors.response.use(async (res, options) => {
|
|
|
+ setTimeout(() => {
|
|
|
+ hideLoading()
|
|
|
+ }, 200)
|
|
|
const url = new URL(res.url)
|
|
|
if (res.status > 299 || res.status < 200) {
|
|
|
const msg = '服务器错误,状态码' + res.status
|
|
@@ -59,11 +69,15 @@ request.interceptors.response.use(async (res, options) => {
|
|
|
const data = await res.clone().json()
|
|
|
if (data.code !== 200 && data.errCode !== 0) {
|
|
|
const msg = data.msg || '处理失败,请重试'
|
|
|
- // const state: any = store.getState()
|
|
|
- // if (data.code === 401 && state.user.status === 'login' && url.pathname !== '/api-auth/exit') {
|
|
|
- // const { dispatch }: any = store
|
|
|
- // dispatch(setLogout())
|
|
|
- // }
|
|
|
+ if(data.code === 401 || data.code === 403) {
|
|
|
+ ElMessage.error(`登录过期,请重新登录!`)
|
|
|
+ removeToken()
|
|
|
+ router.push('/login')
|
|
|
+ }
|
|
|
+ if(data.code === 404) {
|
|
|
+ ElMessage.error(`请求资源不存在!`)
|
|
|
+ router.push('/404')
|
|
|
+ }
|
|
|
if (!(data.code === 403 || data.code === 401)) {
|
|
|
ElMessage.error(msg)
|
|
|
}
|