拦截器
在 ucsDefineConfig.uts 文件中(或自建全局引入uts文件中均可),设置全局请求拦截器
请求之前
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
// ucsDefineConfig.uts
import { http, UcsRequestOptions } from "@/uni_modules/ucs-request";
// 请求之前
http.useBeforeRequestInterceptor(async (options : UcsRequestOptions) => {
// 请求前的逻辑...
return options;
});请求成功
ts
// ucsDefineConfig.uts
import { http } from "@/uni_modules/ucs-request";
// 请求成功
http.useSuccessRequestInterceptor(async (response) => {
console.log(response);
// 请求成功逻辑...
return response;
})请求失败
ts
// ucsDefineConfig.uts
import { http } from "@/uni_modules/ucs-request";
// 请求失败
http.useFailRequestInterceptor(async (fail) => {
console.log(fail)
// 请求失败逻辑...
return fail;
})请求完成(成功或失败)
ts
// ucsDefineConfig.uts
import { http } from "@/uni_modules/ucs-request";
// 请求完成(成功或失败)
http.useCompleteRequestInterceptor(async (complete) => {
console.log(complete)
// 请求完成逻辑...
return complete;
})