|
|
@@ -1,12 +1,14 @@ |
|
|
|
/* |
|
|
|
* @Date: 2021-12-08 15:59:46 |
|
|
|
* @LastEditors: JinxuChen |
|
|
|
* @LastEditTime: 2022-01-11 14:25:43 |
|
|
|
* @LastEditors: JinxChen |
|
|
|
* @LastEditTime: 2022-01-12 09:18:37 |
|
|
|
* @FilePath: \GpsCardAdmin\src\utils\request.js |
|
|
|
* @description: |
|
|
|
*/ |
|
|
|
import axios from 'axios' |
|
|
|
import { Message } from 'element-ui' |
|
|
|
import { |
|
|
|
Message |
|
|
|
} from 'element-ui' |
|
|
|
import store from '@/store' |
|
|
|
/* import { getToken } from '@/utils/auth' */ |
|
|
|
|
|
|
@@ -14,7 +16,7 @@ import store from '@/store' |
|
|
|
const service = axios.create({ |
|
|
|
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url |
|
|
|
// withCredentials: true, // send cookies when cross-domain requests |
|
|
|
/* timeout: 5000 */// request timeout |
|
|
|
/* timeout: 5000 */ // request timeout |
|
|
|
}) |
|
|
|
|
|
|
|
// request interceptor |
|
|
@@ -42,7 +44,7 @@ service.interceptors.response.use( |
|
|
|
/** |
|
|
|
* If you want to get http information such as headers or status |
|
|
|
* Please return response => response |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
/** |
|
|
|
* Determine the request status by custom code |
|
|
@@ -53,30 +55,36 @@ service.interceptors.response.use( |
|
|
|
const res = response.data |
|
|
|
|
|
|
|
// if the custom code is not 20000, it is judged as an error. |
|
|
|
if (res.code !== 0) { |
|
|
|
/* if (res.code !== 0) { |
|
|
|
Message({ |
|
|
|
message: res.message || '出错了,请联系管理员!', |
|
|
|
type: 'error', |
|
|
|
duration: 3 * 1000 |
|
|
|
}) |
|
|
|
// 106 token验证过期; |
|
|
|
return Promise.reject(new Error(res.message)) |
|
|
|
} else if (res.code === 106) { |
|
|
|
// to re-login |
|
|
|
setTimeout(() => { |
|
|
|
store.dispatch('user/resetToken').then(() => { |
|
|
|
Message({ |
|
|
|
message: 'token过期,请重新登录', |
|
|
|
type: 'error', |
|
|
|
duration: 1500 |
|
|
|
}) |
|
|
|
location.reload() |
|
|
|
}) |
|
|
|
}, 1500) |
|
|
|
return Promise.reject(new Error('token过期,请重新登录')) |
|
|
|
} |
|
|
|
else { |
|
|
|
} else */ |
|
|
|
if (res.code === 106) { |
|
|
|
// to re-login |
|
|
|
setTimeout(() => { |
|
|
|
store.dispatch('user/resetToken').then(() => { |
|
|
|
Message({ |
|
|
|
message: 'token过期,请重新登录', |
|
|
|
type: 'error', |
|
|
|
duration: 1500 |
|
|
|
}) |
|
|
|
location.reload() |
|
|
|
}) |
|
|
|
}, 1500) |
|
|
|
return Promise.reject(new Error('token过期,请重新登录')) |
|
|
|
} else if (res.code === 0) { |
|
|
|
return res |
|
|
|
} else { |
|
|
|
Message({ |
|
|
|
message: res.message || '出错了,请联系管理员!', |
|
|
|
type: 'error', |
|
|
|
duration: 3 * 1000 |
|
|
|
}) |
|
|
|
return Promise.reject(new Error(res.message)) |
|
|
|
} |
|
|
|
}, |
|
|
|
error => { |
|
|
@@ -86,7 +94,7 @@ service.interceptors.response.use( |
|
|
|
type: 'error', |
|
|
|
duration: 3 * 1000 |
|
|
|
}) |
|
|
|
/* return Promise.reject(error) */ |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
) |
|
|
|
|
|
|
|