Skip to content

拦截器

ucsDefineConfig.uts 文件中(或自建全局引入uts文件中均可),设置全局请求拦截器

请求之前

UcsRequestOptions属性值

参数类型描述兼容性
dataany请求的参数
headerUTSJSONObject请求的 header
requestTimeoutnumber请求超时时间,单位 ms
uploadFileTimeoutnumber上传超时时间,单位 ms
downloadFileTimeoutnumber下载超时时间,单位 ms
withCredentialsboolean跨域请求时是否携带凭证(cookies)微信小程序 / Web
firstIpv4booleanDNS解析时优先使用ipv4微信小程序
enableChunkedboolean开启 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;
})