12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- import {ApiResponse} from "@/api/types";
- import {AxiosError, AxiosResponse} from "axios";
- import {BusinessErrCode, businessErrCodeMsgKV, ErrCode, errCodeMsgKV} from "@/utils/request/errcode";
- import 'vant/es/notify/style'
- import { showNotify } from 'vant';
- export function handleInnerCodeErr(respData:ApiResponse) {
- if(respData.code != 200) {
-
- const outMsgInfo = businessErrCodeMsgKV[respData.code as BusinessErrCode]
- if(outMsgInfo) {
-
- showNotify({ type: 'warning', message: respData.msg });
- }else {
- showNotify({ type: 'warning', message:'服务器发生了错误,请重试' });
- }
- return false
- }
- return true
- }
- export const handleNormalResponse = async (resp:AxiosResponse<ApiResponse>) => {
-
- if (resp?.data?.code) {
- switch (resp.data.code) {
- case ErrCode.UNAUTHORIZED:
-
- break
- }
- }
-
- if (resp.config.maskingErrorInterceptors) return true
-
- return handleInnerCodeErr(resp.data)
- }
- export const handleHttpError = (err:AxiosError) => {
- if(err.code === 'ECONNABORTED') {
- return Promise.reject(err)
- }
-
- const msg = errCodeMsgKV[err.response?.status as ErrCode]
- if(err.code === 'ECONNABORTED' && err.message.includes('timeout')) {
- showNotify({ type: 'warning', message: '请求超时,请重试!' });
- }else if(err.message === 'Network Error') {
- showNotify({ type: 'warning', message: '服务器错误或网络错误, 请稍后再试!' });
- }else if(msg) {
- showNotify({ type: 'warning', message: msg });
- }else {
- showNotify({ type: 'warning', message: '服务器发出了未知错误!' });
- }
- return Promise.reject(err)
- }
|