|
|
@@ -54,63 +54,130 @@ httpN.interceptors.request.use( |
|
|
|
return Promise.reject(error) |
|
|
|
} |
|
|
|
) |
|
|
|
httpN.interceptors.response.use( |
|
|
|
(response) => { |
|
|
|
// httpN.interceptors.response.use( |
|
|
|
// (response) => { |
|
|
|
// if (response.data.code === 401002) { |
|
|
|
// ElMessage.error(response.data.message) |
|
|
|
// redirectLogin(); |
|
|
|
// } else if (response.data.code === 401001) { |
|
|
|
// redirectLogin() |
|
|
|
// } else if ( |
|
|
|
// response.data.code === 403003 || |
|
|
|
// response.data.code === 403001 || |
|
|
|
// response.data.code === 403004 |
|
|
|
// ) { |
|
|
|
// ElMessage.error(response.data.message) |
|
|
|
// } |
|
|
|
// return response |
|
|
|
// }, |
|
|
|
// (error) => { |
|
|
|
// const status = getValueByKeys(error, 'response.status', 500) |
|
|
|
// const httpCodeLabel: IObject<string> = { |
|
|
|
// 400: '请求参数错误', |
|
|
|
// 401: '未授权,请登录', |
|
|
|
// 403: '拒绝访问', |
|
|
|
// 404: `请求地址出错: ${getValueByKeys(error, 'response.config.url', '')}`, |
|
|
|
// 408: '请求超时', |
|
|
|
// 500: 'API接口报500错误', |
|
|
|
// 501: '服务未实现', |
|
|
|
// 502: '网关错误', |
|
|
|
// 503: '服务不可用', |
|
|
|
// 504: '网关超时', |
|
|
|
// 505: 'HTTP版本不受支持', |
|
|
|
// } |
|
|
|
// if (error && error.response) { |
|
|
|
// console.error('请求错误', error.response.data) |
|
|
|
// } |
|
|
|
// if (status === 401) { |
|
|
|
// redirectLogin() |
|
|
|
// } |
|
|
|
// return Promise.reject(new Error(httpCodeLabel[status] || '接口错误')) |
|
|
|
// } |
|
|
|
// ) |
|
|
|
// |
|
|
|
|
|
|
|
if (response.data.code === 401002) { |
|
|
|
ElMessage.error(response.data.message) |
|
|
|
redirectLogin(); |
|
|
|
// redirectLogin() |
|
|
|
// // 刷新token |
|
|
|
// BaseService.post('/iaw/portal/phoneLogin', {}).then((res: any) => { |
|
|
|
// if (res && res.code === 0) { |
|
|
|
// setCache('refreshCacheAccessToken', res.data, false) |
|
|
|
// ElMessage.warning('token更新,请重试!') |
|
|
|
// } else { |
|
|
|
// ElMessage.error(response.data.message) |
|
|
|
// redirectLogin() |
|
|
|
// } |
|
|
|
// return |
|
|
|
// }) |
|
|
|
} else if (response.data.code === 401001) { |
|
|
|
redirectLogin() |
|
|
|
} else if ( |
|
|
|
response.data.code === 403003 || |
|
|
|
response.data.code === 403001 || |
|
|
|
response.data.code === 403004 |
|
|
|
) { |
|
|
|
ElMessage.error(response.data.message) |
|
|
|
} |
|
|
|
return response |
|
|
|
}, |
|
|
|
(error) => { |
|
|
|
const status = getValueByKeys(error, 'response.status', 500) |
|
|
|
const httpCodeLabel: IObject<string> = { |
|
|
|
400: '请求参数错误', |
|
|
|
401: '未授权,请登录', |
|
|
|
403: '拒绝访问', |
|
|
|
404: `请求地址出错: ${getValueByKeys(error, 'response.config.url', '')}`, |
|
|
|
408: '请求超时', |
|
|
|
500: 'API接口报500错误', |
|
|
|
501: '服务未实现', |
|
|
|
502: '网关错误', |
|
|
|
503: '服务不可用', |
|
|
|
504: '网关超时', |
|
|
|
505: 'HTTP版本不受支持', |
|
|
|
} |
|
|
|
if (error && error.response) { |
|
|
|
console.error('请求错误', error.response.data) |
|
|
|
} |
|
|
|
if (status === 401) { |
|
|
|
redirectLogin() |
|
|
|
|
|
|
|
// 防抖标志位(默认允许显示错误) |
|
|
|
let canShowError = true; |
|
|
|
|
|
|
|
// 恢复错误显示权限(防抖时间结束后允许再次显示) |
|
|
|
const resetErrorPermission = () => { |
|
|
|
canShowError = true; |
|
|
|
}; |
|
|
|
|
|
|
|
httpN.interceptors.response.use( |
|
|
|
(response) => { |
|
|
|
// console.log('响应N==>', response) |
|
|
|
const code = response.data.code || 200; |
|
|
|
const msg = response.data.message; |
|
|
|
// console.log('响应N==>', code,":", msg) |
|
|
|
// console.log('canShowError',canShowError) |
|
|
|
// 处理 401002 错误(token已过期) |
|
|
|
if (code === 401002) { |
|
|
|
if (canShowError) { |
|
|
|
canShowError = false; |
|
|
|
console.log('处理 401002 错误') |
|
|
|
ElMessage.error(msg); |
|
|
|
setTimeout(resetErrorPermission, 5000); // 1秒后允许再次显示错误 |
|
|
|
} |
|
|
|
redirectLogin(); |
|
|
|
} |
|
|
|
// 处理 401001 错误(直接跳转登录,不显示错误) |
|
|
|
else if (code === 401001) { |
|
|
|
redirectLogin(); |
|
|
|
} |
|
|
|
// 处理 403xxx 系列错误(显示错误) |
|
|
|
else if ( |
|
|
|
code === 403003 || |
|
|
|
code === 403001 || |
|
|
|
code === 403004 |
|
|
|
) { |
|
|
|
if (canShowError) { |
|
|
|
canShowError = false; |
|
|
|
ElMessage.error(msg); |
|
|
|
setTimeout(resetErrorPermission, 1000); // 1秒后允许再次显示错误 |
|
|
|
} |
|
|
|
} |
|
|
|
// 其他非 200 错误(添加防抖) |
|
|
|
else if (code !== 200) { |
|
|
|
if (canShowError) { |
|
|
|
canShowError = false; |
|
|
|
ElMessage.error(msg); |
|
|
|
setTimeout(resetErrorPermission, 1000); // 1秒后允许再次显示错误 |
|
|
|
} |
|
|
|
return Promise.reject("error"); |
|
|
|
}else { |
|
|
|
return response; |
|
|
|
} |
|
|
|
}, |
|
|
|
(error) => { |
|
|
|
console.log("error==>", error) |
|
|
|
const status = getValueByKeys(error, "response.status", 500); |
|
|
|
const httpCodeLabel: IObject<string> = { |
|
|
|
400: "请求参数错误", |
|
|
|
401: "未授权,请登录", |
|
|
|
403: "拒绝访问", |
|
|
|
404: `请求地址出错: ${getValueByKeys(error, "response.config.url", "")}`, |
|
|
|
408: "请求超时", |
|
|
|
500: "API接口报500错误", |
|
|
|
501: "服务未实现", |
|
|
|
502: "网关错误", |
|
|
|
503: "服务不可用", |
|
|
|
504: "网关超时", |
|
|
|
505: "HTTP版本不受支持" |
|
|
|
}; |
|
|
|
if (error && error.response) { |
|
|
|
console.error("请求错误", error.response.data); |
|
|
|
} |
|
|
|
if (status === 401) { |
|
|
|
redirectLogin(); |
|
|
|
} |
|
|
|
return Promise.reject(new Error(httpCodeLabel[status] || "接口错误")); |
|
|
|
} |
|
|
|
return Promise.reject(new Error(httpCodeLabel[status] || '接口错误')) |
|
|
|
} |
|
|
|
) |
|
|
|
); |
|
|
|
|
|
|
|
const redirectLogin = () => { |
|
|
|
console.log('redirectLogin'); |
|
|
|
|
|
|
|
// console.log('redirectLogin'); |
|
|
|
router.push('/login') |
|
|
|
return |
|
|
|
} |
|
|
@@ -119,26 +186,10 @@ export default (o: AxiosRequestConfig): Promise<IHttpResponse> => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
httpN(o) |
|
|
|
.then((res: any) => { |
|
|
|
//登录失效 |
|
|
|
// if(res.data && res.data.statusCode === 401){ |
|
|
|
// router.push("/login"); |
|
|
|
// ElMessage.error("登录失效,请重新登录!") |
|
|
|
// return; |
|
|
|
// } else if (res.data) { |
|
|
|
// //解密 |
|
|
|
// if(res?.data && res.data.statusCode === 0 && res.data.bizContent && (import.meta.env.VITE_APP_BASE_URL.includes('https://qtzl.etcjz.cn') || import.meta.env.VITE_APP_BASE_URL.includes('http://100.65.11.2'))) { |
|
|
|
// res.data.bizContent = sm4.decryptData_CBC(res.data.bizContent, sm4Key); |
|
|
|
// if(res && res.data) { |
|
|
|
// return resolve(res.data); |
|
|
|
// } else { |
|
|
|
// ElMessage.error("接口返回解密数据,解密失败!") |
|
|
|
// } |
|
|
|
// } else { |
|
|
|
// return resolve(res.data); |
|
|
|
// } |
|
|
|
res.data.statusCode = res.data.code |
|
|
|
return resolve(res.data) |
|
|
|
// } |
|
|
|
if(res){ |
|
|
|
res.data.statusCode = res.data.code |
|
|
|
return resolve(res.data) |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch(reject) |
|
|
|
}) |