赞
踩
return this.request({ …config, method: ‘POST’ });
}
delete<T = any>(config: HttpRequestConfig): Promise {
return this.request({ …config, method: ‘DELETE’ });
}
patch<T = any>(config: HttpRequestConfig): Promise {
return this.request({ …config, method: ‘PATCH’ });
}
}
export function errorHandler(error: any) {
if (error instanceof AxiosError) {
showToast(error.message)
} else if (error != undefined && error.response != undefined && error.response.status) {
switch (error.response.status) {
// 401: 未登录
// 未登录则跳转登录页面,并携带当前页面的路径
// 在登录成功后返回当前页面,这一步需要在登录页操作。
case 401:
break;
// 403 token过期
// 登录过期对用户进行提示
// 清除本地token和清空vuex中token对象
// 跳转登录页面
case 403:
showToast(“登录过期,请重新登录”)
// 清除token
// localStorage.removeItem(‘token’);
break;
// 404请求不存在
case 404:
showToast(“网络请求不存在”)
break;
// 其他错误,直接抛出错误提示
default:
showToast(error.response.data.message)
}
}
}
export default AxiosHttpRequest
//AxiosRequest.ets
import {AxiosHttpRequest,errorHandler} from ‘./AxiosHttp’
import { AxiosError, AxiosRequestHeaders } from ‘@ohos/axios’;
import { LogUtils } from ‘…/utils/LogUtils’;
import showToast from ‘…/utils/ToastUtils’;
import { hideLoadingDialog, showLoadingDialog } from ‘…/utils/DialogUtils’;
import { StorageUtils } from ‘…/utils/StorageUtils’;
import { StorageKeys } from ‘…/constants/StorageKeys’;
import { JsonUtils } from ‘…/utils/JsonUtils’;
import { Router } from ‘…/route/Router’;
import { RoutePath } from ‘…/route/RoutePath’;
/**
${config.method}
);${config.url}
);Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。