拦截器
来源于[创建实例]章节所创建的request实例作为演示,实际情况同学们根据所创建的实例来设置全局请求拦截器
请求之前
UcsRequestOptions属性值
| 参数 | 类型 | 描述 | 兼容性 |
|---|---|---|---|
| data | any | 请求的参数 | |
| header | UTSJSONObject | 请求的 header | |
| requestTimeout | number | 请求超时时间,单位 ms | |
| uploadFileTimeout | number | 上传超时时间,单位 ms | |
| downloadFileTimeout | number | 下载超时时间,单位 ms | |
| withCredentials | boolean | 跨域请求时是否携带凭证(cookies) | 微信小程序 / Web |
| firstIpv4 | boolean | DNS解析时优先使用ipv4 | 微信小程序 |
| enableChunked | boolean | 开启 transfer-encoding chunked。 |
ts
// 导出UcsRequestOptions类型
import { UcsRequestOptions } from "@/uni_modules/ucs-request";
// 请求之前
request.useBeforeRequestInterceptor(async (options : UcsRequestOptions) => {
// 请求前的逻辑...
return options;
});请求成功
ts
// 请求成功
request.useSuccessRequestInterceptor(async (response) => {
console.log(response);
// 请求成功逻辑...
return response;
})请求失败
ts
// 请求失败
request.useFailRequestInterceptor(async (fail) => {
console.log(fail)
// 请求失败逻辑...
return fail;
})请求完成(成功或失败)
ts
// 请求完成(成功或失败)
request.useCompleteRequestInterceptor(async (complete) => {
console.log(complete)
// 请求完成逻辑...
return complete;
})