|
|
@@ -0,0 +1,108 @@
|
|
|
+import { IBestToast } from '@ibestservices/ibest-ui';
|
|
|
+import axios, {
|
|
|
+ AxiosError,
|
|
|
+ AxiosHeaders,
|
|
|
+ AxiosInstance,
|
|
|
+ AxiosRequestConfig,
|
|
|
+ AxiosResponse,
|
|
|
+ InternalAxiosRequestConfig
|
|
|
+} from '@ohos/axios';
|
|
|
+
|
|
|
+export class YTRequest {
|
|
|
+ static instance: AxiosInstance = axios
|
|
|
+
|
|
|
+ static init() {
|
|
|
+ YTRequest.instance = axios.create({
|
|
|
+ baseURL: '',
|
|
|
+ timeout: 10000
|
|
|
+ })
|
|
|
+ // 添加请求拦截器
|
|
|
+ YTRequest.instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
|
+
|
|
|
+ // // 对请求数据做点什么 - 为请求参数添加 token
|
|
|
+ // if (AppStorage.get<string>(AppStorageKeyCollect.TOKEN)) {
|
|
|
+ // config.headers.Authorization = AppStorage.get<string>(AppStorageKeyCollect.TOKEN)
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // // 为请求参数添加 oaid
|
|
|
+ // if ((config.method == 'get' || config.method == 'delete') && config.url) {
|
|
|
+ // // if(config?.params) {
|
|
|
+ // // config.params.oaid = await ytOaidUtils.getOaid()
|
|
|
+ // // }
|
|
|
+ // } else {
|
|
|
+ // // if(config?.data) {
|
|
|
+ // // config.data.oaid = await ytOaidUtils.getOaid()
|
|
|
+ // // }
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ //
|
|
|
+ console.info('请求参数', config.data, config.params)
|
|
|
+
|
|
|
+ return config;
|
|
|
+ }, (error: AxiosError) => {
|
|
|
+ // 对请求错误做些什么
|
|
|
+
|
|
|
+ return Promise.reject(error);
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ // 添加响应拦截器
|
|
|
+ YTRequest.instance.interceptors.response.use((response: AxiosResponse) => {
|
|
|
+ // 对响应数据做点什么
|
|
|
+ console.info('response:', response)
|
|
|
+ // 对响应错误做点什么
|
|
|
+ // if (response.data.code == 401) {
|
|
|
+ // if (AppStorage.get<string>(AppStorageKeyCollect.TOKEN)) {
|
|
|
+ // IBestToast.show("登录过期,请重新登录")
|
|
|
+ // } else {
|
|
|
+ // IBestToast.show("请先登录哦")
|
|
|
+ // }
|
|
|
+ // userInfo.logout()
|
|
|
+ // yTRouter.router2LoginPage()
|
|
|
+ // return Promise.reject('401');
|
|
|
+ // }
|
|
|
+ // if (response.data.code == 500) {
|
|
|
+ // IBestToast.show({ message: response.data.msg, type: "fail" })
|
|
|
+ // return Promise.reject(response.data.msg);
|
|
|
+ // }
|
|
|
+
|
|
|
+ console.info('响应数据', JSON.stringify(response))
|
|
|
+ return response.data;
|
|
|
+
|
|
|
+ }, (error: AxiosError) => {
|
|
|
+ console.error('error', JSON.stringify(error))
|
|
|
+ setTimeout(() => {
|
|
|
+ IBestToast.hide()
|
|
|
+ setTimeout(() => {
|
|
|
+ IBestToast.show({ message: '请求超时,请检查网络' })
|
|
|
+ }, 100)
|
|
|
+ }, 1000)
|
|
|
+ return Promise.reject(error);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ static get<T>(url: string, params?: ESObject,
|
|
|
+ headers?: Record<string, string>) {
|
|
|
+ return YTRequest.instance.get<null, T, null>(url, { params, headers })
|
|
|
+ }
|
|
|
+
|
|
|
+ static post<T, D = ESObject>(url: string, data?: D, params?: ESObject,
|
|
|
+ headers?: AxiosHeaders) {
|
|
|
+ return YTRequest.instance.post<null, T, D>(url, data, { params, headers })
|
|
|
+ }
|
|
|
+
|
|
|
+ static delete<T>(url: string, params?: ESObject, headers?: AxiosHeaders) {
|
|
|
+ return YTRequest.instance.delete<null, T, null>(url, { params, headers })
|
|
|
+ }
|
|
|
+
|
|
|
+ static put<T, D = ESObject>(url: string, data?: D, params?: ESObject, headers?: AxiosHeaders) {
|
|
|
+ return YTRequest.instance.put<null, T, D>(url, data, { params, headers })
|
|
|
+ }
|
|
|
+
|
|
|
+ static upPost<T, D = ESObject>(url: string, data: D, configs?: AxiosRequestConfig<D>) {
|
|
|
+ return YTRequest.instance.post<string, T, D>(url, data, configs)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|