您的位置:首页 > 移动开发 > 微信开发

基于微信小程序的ajax拦截器(简易版的axios)

2018-01-16 17:02 573 查看
摘要: 微信小程序现在比较火,现在好的ajax请求框架都不能良好的支持,只能自己动手写一个,简易版的axios



import util from './util.js'
const request = (function () {
//系统参数;
let defaults = {
timeout: 5000,
reqState: false,
url: '',
header: '',
data: '',
method: '',
dataType: 'json',
success: (res) => { },
fail: (res) => { },
complete: function (res) { },
};
function xhrAdapter(config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
config.success = (res) => {
resolve(res);
};
config.fail = (res) => {
reject(res);
};
wx.request(config);
});

}
function dispatchRequest(config) {
var adapter = new xhrAdapter(config);
return adapter.then(function onAdapterResolution(response) {
return response;
}, function onAdapterRejection(reason) {
return Promise.reject(reason);
});
}
function InterceptorManager() {
this.handlers = [];
}
InterceptorManager.prototype.use = function use(fulfilled, rejected) {
this.handlers.push({
fulfilled: fulfilled,
rejected: rejected
});
return this.handlers.length - 1;
};
InterceptorManager.prototype.forEach = function forEach(fn) {
util.forEach(this.handlers, function forEachHandler(h) {
if (h !== null) {
fn(h);
}
});
};
function Axios(defaultConfig) {
this.defaults = defaultConfig;
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager()
};
};

util.forEach(['delete', 'get'], (method) => {
Axios.prototype[method] = function (url, config) {
return this.request(util.merge({
header: config || {}
} || {}, {
method: method,
url: url
}))
}
})
util.forEach(['post', 'put'], (method) => {
Axios.prototype[method] = function (url, data, config) {
return this.request(util.merge({
header: config ||{}
} || {}, {
method: method,
url: url,
data: data,
}))
}
})
Axios.prototype.request = function request(config) {
config.method = config.method.toLowerCase();
config = util.merge(defaults, this.defaults, config);
var chain = [dispatchRequest, undefined];
var promise = Promise.resolve(config);
this.interceptors.request.forEach(function unshiftRequestInterceptors(interceptor) {
chain.unshift(interceptor.fulfilled, interceptor.rejected);
});
this.interceptors.response.forEach(function pushResponseInterceptors(interceptor) {
chain.push(interceptor.fulfilled, interceptor.rejected);
});
while (chain.length) {
promise = promise.then(chain.shift(), chain.shift());
}
return promise;
}
function createInstance(defaultConfig) {
let context = new Axios(defaultConfig);
return context;
}
let axios = createInstance(defaults);
return axios;
})();

//  自定义请求拦截器
request.interceptors.request.use((data) => {
console.log(`url:${data.url} method:${data.method}`);
wx.showLoading({
title: '加载中',
})
return data;
},error =>{
return Promise.reject(error);
});

//  自定义返回拦截器
request.interceptors.response.use((data) => {
wx.hideLoading();
return data
}, error => {
return Promise.reject(error);
})
module.exports = request;


3ff8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微信小程序