import deepMerge from "../function/deepMerge";
import validate from "../function/test";
class Request {
// 设置全局默认配置
setConfig(customConfig) {
// 深度合并对象,否则会造成对象深层属性丢失
this.config = deepMerge(this.config, customConfig);
}
// 主要请求部分
request(options = {}) {
// 检查请求拦截
if (this.interceptor.request && typeof this.interceptor.request === 'function') {
let tmpConfig = {};
let interceptorRequest = this.interceptor.request(options);
if (interceptorRequest === false) {
// 返回一个处于pending状态中的Promise,来取消原promise,避免进入then()回调
return new Promise(()=>{});
}
this.options = interceptorRequest;
}
options.dataType = options.dataType || this.config.dataType;
options.responseType = options.responseType || this.config.responseType;
options.url = options.url || '';
options.params = options.params || {};
options.header = Object.assign({}, this.config.header, options.header);
options.method = options.method || this
uview 学习之-网络异常处理
最新推荐文章于 2025-08-02 22:14:04 发布
本文介绍了一种基于uni-app的小程序请求封装方案,通过自定义请求类实现对不同HTTP方法的支持,并利用拦截器进行请求与响应的数据处理。此外,还实现了请求加载提示及错误处理等功能。

最低0.47元/天 解锁文章
1234

被折叠的 条评论
为什么被折叠?



