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’;
/**
- axios请求客户端创建
*/
const axiosClient = new AxiosHttpRequest({
baseURL: “/api”,
timeout: 10 * 1000,
checkResultCode: false,
headers: {
‘Content-Type’: ‘application/json’
} as AxiosRequestHeaders,
interceptorHooks: {
requestInterceptor: async (config) => {
// 在发送请求之前做一些处理,例如打印请求信息
LogUtils.debug(‘网络请求Request 请求方法:’,${config.method}
);
LogUtils.debug(‘网络请求Request 请求链接:’,${config.url}
);
LogUtils.debug(‘网络请求Request Par