|
@@ -1,7 +1,7 @@
|
|
|
import axios from 'axios'
|
|
|
import router from '@/router'
|
|
|
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
|
|
|
-import { getToken, setToken } from '@/utils/auth'
|
|
|
+import { getToken, setToken, removeToken } from '@/utils/auth'
|
|
|
import { AxiosConfigTy, AxiosReqTy, ObjTy } from '~/common'
|
|
|
import { useUserStore } from '@/store/user'
|
|
|
let reqConfig: any
|
|
@@ -46,64 +46,68 @@ service.interceptors.request.use(
|
|
|
)
|
|
|
// 响应拦截
|
|
|
service.interceptors.response.use(
|
|
|
- (res: any) => {
|
|
|
- if (reqConfig.afHLoading && loadingE) {
|
|
|
- loadingE.close()
|
|
|
- }
|
|
|
- // 如果是下载文件直接返回
|
|
|
- if (reqConfig.isDownLoadFile) {
|
|
|
- return res
|
|
|
- }
|
|
|
- const { flag, msg, isNeedUpdateToken, updateToken, code } = res.data
|
|
|
- //更新token保持登录状态
|
|
|
- // if (isNeedUpdateToken) {
|
|
|
- // setToken(updateToken)
|
|
|
- // }
|
|
|
- const successCode = '0,200,20000'
|
|
|
- if (successCode.includes(code)) {
|
|
|
- return res.data
|
|
|
- } else {
|
|
|
- if (code === 403) {
|
|
|
- ElMessageBox.confirm('请重新登录', {
|
|
|
- confirmButtonText: '重新登录',
|
|
|
- cancelButtonText: '取消',
|
|
|
- type: 'warning',
|
|
|
- }).then(() => {
|
|
|
- const userStore = useUserStore()
|
|
|
- userStore.resetState().then(() => {
|
|
|
- router.push({ path: '/login' })
|
|
|
- })
|
|
|
- })
|
|
|
- }
|
|
|
- if (reqConfig.isAlertErrorMsg) {
|
|
|
- ElMessage({
|
|
|
- message: msg,
|
|
|
- type: 'error',
|
|
|
- duration: 2 * 1000,
|
|
|
+ (response) => {
|
|
|
+ const res = response.data
|
|
|
+
|
|
|
+ // if the custom code is not 20000, it is judged as an error.
|
|
|
+ if (res.code != '0') {
|
|
|
+ ElMessage({
|
|
|
+ message: res.message || 'Error',
|
|
|
+ type: 'error',
|
|
|
+ duration: 5 * 1000,
|
|
|
+ })
|
|
|
+
|
|
|
+ // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
|
|
+ if (res.code === 500 || res.code === 50012 || res.code === 50014) {
|
|
|
+ // to re-login
|
|
|
+ ElMessageBox.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(async () => {
|
|
|
+ sessionStorage.removeItem('userName')
|
|
|
+ // store.dispatch("app/toggleOutflag", false);
|
|
|
+ await useUserStore().logout()
|
|
|
+ router.push(`/login`)
|
|
|
})
|
|
|
}
|
|
|
- //返回错误信息
|
|
|
- //如果未catch 走unhandledrejection进行收集
|
|
|
- //注:如果没有return 则,会放回到请求方法中.then ,返回的res为 undefined
|
|
|
- return Promise.reject(res.data)
|
|
|
+ return Promise.reject(new Error(res.message || 'Error'))
|
|
|
+ } else {
|
|
|
+ return res
|
|
|
}
|
|
|
},
|
|
|
- (err: any) => {
|
|
|
- /*http错误处理,处理跨域,404,401,500*/
|
|
|
- if (loadingE) loadingE.close()
|
|
|
- ElMessage({
|
|
|
- message: err,
|
|
|
- type: 'error',
|
|
|
- duration: 2 * 1000,
|
|
|
- })
|
|
|
- //如果是跨域
|
|
|
- //Network Error,cross origin
|
|
|
- const errObj: ObjTy = {
|
|
|
- msg: err.toString(),
|
|
|
- reqUrl: reqConfig.baseURL + reqConfig.url,
|
|
|
- params: reqConfig.isParams ? reqConfig.params : reqConfig.data,
|
|
|
+ (error) => {
|
|
|
+ const des500 = `${error}`.split(' ').includes('500')
|
|
|
+ if (des500) {
|
|
|
+ ElMessage({
|
|
|
+ message: '身份令牌过期或失效,即将重新登录',
|
|
|
+ type: 'error',
|
|
|
+ duration: 3 * 1000,
|
|
|
+ onClose: async () => {
|
|
|
+ sessionStorage.clear()
|
|
|
+ removeToken() // must remove token first
|
|
|
+ removeToken('codeToken')
|
|
|
+ removeToken('systemSet')
|
|
|
+ removeToken('userid')
|
|
|
+ // store.dispatch('app/toggleOutflag', false)
|
|
|
+ // await store.dispatch('user/resetToken')
|
|
|
+ useUserStore().resetState()
|
|
|
+ router.push(`/login`)
|
|
|
+ location.reload()
|
|
|
+ },
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ ElMessage({
|
|
|
+ message: error.message,
|
|
|
+ type: 'error',
|
|
|
+ duration: 5 * 1000,
|
|
|
+ })
|
|
|
}
|
|
|
- return Promise.reject(JSON.stringify(errObj))
|
|
|
+ return Promise.reject(error)
|
|
|
}
|
|
|
)
|
|
|
|